using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class MusicMgr : BaseManager { //唯一的背景音乐组件 private AudioSource bkMusic = null; //音乐大小 private float bkValue = 1; //音效依附对象 private GameObject soundObj = null; //音效列表 private List soundList = new List(); //音效大小 private float soundValue = 1; public MusicMgr() { MonoMgr.GetInstance().AddUpdateListener(Update); } private void Update() { for( int i = soundList.Count - 1; i >=0; --i ) { if(!soundList[i].isPlaying) { GameObject.Destroy(soundList[i]); soundList.RemoveAt(i); } } } /// /// 播放背景音乐 /// /// public void PlayBkMusic(string name) { if(bkMusic == null) { GameObject obj = new GameObject(); obj.name = "BkMusic"; bkMusic = obj.AddComponent(); } //异步加载背景音乐 加载完成后 播放 ResMgr.GetInstance().LoadAsync("Music/BK/" + name, (clip) => { bkMusic.clip = clip; bkMusic.loop = true; bkMusic.volume = bkValue; bkMusic.Play(); }); } /// /// 暂停背景音乐 /// public void PauseBKMusic() { if (bkMusic == null) return; bkMusic.Pause(); } /// /// 停止背景音乐 /// public void StopBKMusic() { if (bkMusic == null) return; bkMusic.Stop(); } /// /// 改变背景音乐 音量大小 /// /// public void ChangeBKValue(float v) { bkValue = v; if (bkMusic == null) return; bkMusic.volume = bkValue; } /// /// 播放音效 /// public void PlaySound(string name, bool isLoop, UnityAction callBack = null) { if(soundObj == null) { soundObj = new GameObject(); soundObj.name = "Sound"; } //当音效资源异步加载结束后 再添加一个音效 ResMgr.GetInstance().LoadAsync("Music/Sound/" + name, (clip) => { AudioSource source = soundObj.AddComponent(); source.clip = clip; source.loop = isLoop; source.volume = soundValue; source.Play(); soundList.Add(source); if(callBack != null) callBack(source); }); } /// /// 改变音效声音大小 /// /// public void ChangeSoundValue( float value ) { soundValue = value; for (int i = 0; i < soundList.Count; ++i) soundList[i].volume = value; } /// /// 停止音效 /// public void StopSound(AudioSource source) { if( soundList.Contains(source) ) { soundList.Remove(source); source.Stop(); GameObject.Destroy(source); } } }