using UnityEngine;
using System.Collections;
public delegate void AudioPlayEnd();
public class CustomAudioSource : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void Play(AudioSource audioSource, AudioClip clip, Transform emitter, float volume, float pitch, bool loop, AudioPlayEnd callback = null)
{
if (emitter == null)
{
StartCoroutine(PlayI(audioSource, clip, volume, pitch, loop, callback));
}
else
{
StartCoroutine(PlayII(clip, emitter, volume, pitch, loop, callback));
}
}
IEnumerator PlayI(AudioSource audioSource, AudioClip clip, float volume, float pitch, bool loop, AudioPlayEnd callback)
{
audioSource.clip = clip;
audioSource.volume = volume;
audioSource.pitch = pitch;
audioSource.loop = loop;
audioSource.Play();
yield return new WaitForSeconds(clip.length);
if (callback != null)
callback();
}
IEnumerator PlayII(AudioClip clip, Transform emitter, float volume, float pitch, bool loop, AudioPlayEnd callback = null)
{
GameObject go = new GameObject("Audio:" + clip.name);
go.transform.position = emitter.position;
go.transform.parent = emitter;
AudioSource source = go.AddComponent<AudioSource>();
source.clip = clip;
source.volume = volume;
source.pitch = pitch;
source.loop = loop;
source.Play();
if (!loop)
{
DestroyObject(go, clip.length);
yield return new WaitForSeconds(clip.length);
if (callback != null)
callback();
}
}
void OnDestoryed()
{
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AudioManager
{
private Dictionary<string, AudioClip> soundDicts = new Dictionary<string, AudioClip>();
private AudioSource _AudioSource;
private CustomAudioSource _