1 public class AudioManager : MonoBehaviour 2 { 3 4 private Dictionary<string, AudioSource> _audioPool = new Dictionary<string, AudioSource>(); 5 6 7 private static AudioManager _mInstance = null; 8 9 10 public static AudioManager Instance 11 { 12 get 13 { 14 if (_mInstance == null) 15 { 16 GameObject obj = new GameObject("AudioManager"); 17 _mInstance = obj.AddComponent<AudioManager>(); 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<AudioSource>(); 33 34 source.clip = clip; 35 _audioPool.Add(keyName, source); 36 } 37 else 38 { 39 // TODO: 测试 40 print(keyName + "已存在"); 41 } 42 } 43 44 /// <summary> 45 /// 播放处理 46 /// </summary> 47 /// <param name="audioKey">音频Key</param> 48 /// <param name="isLoop">是否循环播放:true:循环/false:不循环</param> 49 /// <param name="isAwake">是否一开始就自动播放:true:是/false:否</param> 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 /// <summary> 61 /// 停止处理 62 /// </summary> 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 }