1 public class AudioManager : MonoBehaviour 2 { 3 4 private Dictionary_audioPool = new Dictionary (); 5 6 7 private static AudioManager _mInstance = null; 8 9 10 public static AudioManager Instance11 {12 get13 {14 if (_mInstance == null)15 {16 GameObject obj = new GameObject("AudioManager");17 _mInstance = obj.AddComponent ();18 }19 return _mInstance;20 }21 }22 23 //设置音频存储key,实际音频名称(路径)24 public void AudioSet(string keyName, string path)25 {26 27 if (!_audioPool.ContainsKey(keyName))28 {29 30 31 AudioClip clip = Resources.Load("Audio/" + path) as AudioClip;32 AudioSource source = gameObject.AddComponent ();33 34 source.clip = clip;35 _audioPool.Add(keyName, source);36 }37 else38 {39 // TODO: 测试40 print(keyName + "已存在");41 }42 }43 44 /// 45 /// 播放处理46 /// 47 /// 音频Key48 /// 是否循环播放:true:循环/false:不循环49 /// 是否一开始就自动播放:true:是/false:否50 public void Play(string audioKey, bool isLoop = false, bool isAwake = false)51 {52 AudioSource source = _audioPool[audioKey];53 54 source.loop = isLoop;55 source.playOnAwake = isAwake;56 57 source.Play();58 }59 60 ///61 /// 停止处理62 /// 63 public void Stop(string key)64 {65 _audioPool[key].Stop();66 }67 68 69 70 //-----------------------常用音效71 //录像音效72 public void ClickBtnSoundEffect()73 {74 AudioSet ("ClickBtn", "click_Btn");75 Play ("ClickBtn");76 77 }78 79 //拍照音效80 public void ClickTakePhotoBtnSoundEffect()81 {82 AudioSet ("takePhotoSound", "camera-shutter");83 Play ("takePhotoSound",false,false);84 85 }86 87 }