using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class AudioObjectPool
{
private GameObject prefab;
private List<GameObject> pool;
public AudioObjectPool(GameObject prefab, int initialSize)
{
this.prefab = prefab;
this.pool = new List<GameObject>();
for (int i = 0; i < initialSize; i++)
{
AlllocateInstance();
}
}
public GameObject GetInstance()
{
if (pool.Count == 0)
{
AlllocateInstance();
}
GameObject instance = pool[0];
pool.RemoveAt(0);
instance.SetActive(true);
return instance;
}
public void ReleaseInstance(GameObject instance)
{
instance.SetActive(false);
pool.Add(instance);
}
private GameObject AlllocateInstance()
{
GameObject instance = (GameObject)GameObject.Instantiate(prefab);
instance.transform.SetParent(AudioMgr._instance.transform);
instance.SetActive(false);
pool.Add(instance);
return instance;
}
}
public class AudioMgr : MonoBehaviour
{
public static AudioMgr _instance;
public List<AudioClip> audioList;
public int initAudioPrefabCount = 5;
[HideInInspector]
public float tempVolume = 0;
private bool isMute = false;
public bool IsMute
{
set
{
isMute = value;
if (isMute)
{
tempVolume = AudioListener.volume;
AudioListener.volume = 0;
}
else
{
AudioListener.volume = tempVolume;
}
}
private get { return isMute; }
}
private float volumeScale = 1;
public float VolumeScale
{
set
{
volumeScale = Mathf.Clamp01(value);
if (!IsMute)
{
AudioListener.volume = value;
}
}
private get
{
return volumeScale;
}
}
private Dictionary<string, AudioClip> audioDic = new Dictionary<string, AudioClip>();
private AudioObjectPool audioObjectPool;
void Awake()
{
_instance = this;
DontDestroyOnLoad(this);
}
void Start()
{
SceneManager.sceneUnloaded += scene => {
StopAllCoroutines();
for (int i = 0; i < transform.childCount; i++)
{
if (transform.GetChild(i).hideFlags != HideFlags.HideInHierarchy)
{
InitAudioSource(transform.GetChild(i).GetComponent<AudioSource>());
audioObjectPool.ReleaseInstance(transform.GetChild(i).gameObject);
}
}
};
GameObject audioPrefab = new GameObject("AudioObjectPool");
audioPrefab.AddComponent<AudioSource>();
audioPrefab.GetComponent<AudioSource>().playOnAwake = false;
audioObjectPool = new AudioObjectPool(audioPrefab, initAudioPrefabCount);
audioPrefab.hideFlags = HideFlags.HideInHierarchy;
audioPrefab.transform.SetParent(this.transform);
foreach (AudioClip ac in audioList)
{
audioDic.Add(ac.name, ac);
}
}
public void PauseAudio(AudioSource audioSource)
{
audioSource.Pause();
}
public void ResumeAudio(AudioSource audioSource)
{
audioSource.UnPause();
}
public void StopAudio(AudioSource audioSource)
{
audioSource.Stop();
}
public void PlayAudio(AudioSource audioSource, string audioName, float volume = 1, bool isLoop = false)
{
if (IsMute)
{
return;
}
AudioClip audioClip;
if (audioDic.TryGetValue(audioName, out audioClip))
{
audioSource.loop = isLoop;
audioSource.clip = audioClip;
audioSource.volume = volume;
if (audioSource.isPlaying)
{
audioSource.Stop();
}
audioSource.Play();
}
}
public void PlayAudio(Vector3 audioPos, string audioName, float volume = 1, bool isLoop = false)
{
if (IsMute)
{
return;
}
AudioClip audioClip;
if (audioDic.TryGetValue(audioName, out audioClip))
{
GameObject audioGo = audioObjectPool.GetInstance();
audioGo.transform.position = audioPos;
audioGo.name = audioName;
AudioSource audioSource = audioGo.GetComponent<AudioSource>();
audioSource.clip = audioClip;
audioSource.volume = volume;
audioSource.loop = isLoop;
audioSource.Play();
StartCoroutine(DestroyAudioGo(audioSource, audioClip.length));
}
}
private void InitAudioSource(AudioSource audioSource)
{
if (audioSource == null)
{
return;
}
if (audioSource.isPlaying)
{
audioSource.Stop();
}
audioSource.playOnAwake = false;
audioSource.loop = false;
audioSource.volume = 1;
audioSource.clip = null;
audioSource.name = "AudioObjectPool";
}
IEnumerator DestroyAudioGo(AudioSource audioSource, float delayTime)
{
yield return new WaitForSeconds(delayTime);
InitAudioSource(audioSource);
audioObjectPool.ReleaseInstance(audioSource.gameObject);
}
void Destroy()
{
StopAllCoroutines();
}
}