1、分享一个Unity中用于管理声音的声音管理器,适合于中小型项目,大项目就算了。
2、借鉴了很多的源码,最后修改完成,吸取百家之长,改为自己所用,哈哈。
3、源码奉上:
1 /* 2 * 3 * 开发时间:2018.11.20 4 * 5 * 功能:用来对项目中的所有音频做同一的管理 6 * 7 * 描述: 8 * 1、挂载该脚本的游戏物体上要挂载三个AudioSouce[可以修改脚本,动态挂] 9 * 2、建议大的背景音乐不要加入AudioClip[],对内存消耗大。而是哪里用到,单独使用PlayBackground函数播放 10 * 3、小的音效片段可以将其加入AudioClip[]中,可以很方便的管理,可以通过声音剪辑、声音剪辑名称来进行音乐的播放 11 * 4、该声音管理可以进行背景音乐的播放、音效的播放、背景音乐的音调改变、音效的音调改变、停止播放 12 * 13 */ 14 using UnityEngine; 15 using System.Collections; 16 using System.Collections.Generic; 17 18 namespace Kernal 19 { 20 public class AudioManager : MonoBehaviour 21 { 22 public AudioClip[] AudioClipArray; //剪辑数组 23 24 public static float AudioBackgroundVolumns = 1F; //背景音量 25 public static float AudioEffectVolumns = 1F; //音效音量 26 public static float AudioBackgroundPitch = 1f; //背景音乐的音调 27 public static float AudioEffectPitch = 1.0f; //音效的音调 28 29 30 private static Dictionary_DicAudioClipLib; //音频库,将声音名字和声音资源进行关联 31 32 private static AudioSource[] _AudioSourceArray; //音频源数组 33 34 private static AudioSource _AudioSource_BackgroundAudio; //背景音乐 35 private static AudioSource _AudioSource_AudioEffectA; //音效源A 36 private static AudioSource _AudioSource_AudioEffectB; //音效源B 37 //......可以按需求进行添加 38 39 /// 40 /// 音效库资源加载 41 /// 42 void Awake() 43 { 44 //音频库加载[初始化,将音乐剪辑和名字联系起来] 45 _DicAudioClipLib = new Dictionary(); 46 47 foreach (AudioClip audioClip in AudioClipArray) 48 { 49 _DicAudioClipLib.Add(audioClip.name, audioClip); 50 } 51 52 53 //处理音频源,也就是得到用来播放声音的音乐播放器 54 _AudioSourceArray = this.GetComponents (); 55 _AudioSource_BackgroundAudio = _AudioSourceArray[0]; //其中一个用来播放背景音乐 56 _AudioSource_AudioEffectA = _AudioSourceArray[1]; //其中一个用来播放音乐1 57 _AudioSource_AudioEffectB = _AudioSourceArray[2]; 58 59 60 //从数据持久化中得到音量数值 61 if (PlayerPrefs.GetFloat("AudioBackgroundVolumns") >= 0) 62 { 63 AudioBackgroundVolumns = PlayerPrefs.GetFloat("AudioBackgroundVolumns"); 64 _AudioSource_BackgroundAudio.volume = AudioBackgroundVolumns; 65 } 66 67 if (PlayerPrefs.GetFloat("AudioEffectVolumns") >= 0) 68 { 69 AudioEffectVolumns = PlayerPrefs.GetFloat("AudioEffectVolumns"); 70 _AudioSource_AudioEffectA.volume = AudioEffectVolumns; 71 _AudioSource_AudioEffectB.volume = AudioEffectVolumns; 72 } 73 74 //设置音乐的音效 75 if (PlayerPrefs.HasKey("AudioBackgroundPitch")) 76 { 77 AudioBackgroundPitch = PlayerPrefs.GetFloat("AudioBackgroundPitch"); 78 } 79 if (PlayerPrefs.HasKey("AudioEffectPitch")) 80 { 81 AudioEffectPitch = PlayerPrefs.GetFloat("AudioEffectPitch"); 82 } 83 84 _AudioSource_BackgroundAudio.pitch = AudioBackgroundPitch; 85 _AudioSource_AudioEffectA.pitch = AudioEffectPitch; 86 _AudioSource_AudioEffectB.pitch = AudioEffectPitch; 87 88 } 89 90 /// 91 /// 播放背景音乐 92 /// 传入的参数是背景音乐的AudioClip 93 /// 94 /// 音频剪辑 95 public static void PlayBackground(AudioClip audioClip) 96 { 97 //防止背景音乐的重复播放。 98 if (_AudioSource_BackgroundAudio.clip == audioClip) 99 {100 return;101 }102 103 //处理全局背景音乐音量104 _AudioSource_BackgroundAudio.volume = AudioBackgroundVolumns;105 _AudioSource_BackgroundAudio.pitch = AudioBackgroundPitch;106 if (audioClip)107 {108 _AudioSource_BackgroundAudio.loop = true; //背景音乐是循环播放的109 _AudioSource_BackgroundAudio.clip = audioClip;110 _AudioSource_BackgroundAudio.Play();111 }112 else113 {114 Debug.LogWarning("[AudioManager.cs/PlayBackground()] audioClip==null !");115 }116 }117 118 ///119 /// 播放背景音乐120 /// 传入的参数是声音片段的名字,要注意,其声音片段要加入声音数组中121 /// 122 /// 123 public static void PlayBackground(string strAudioName)124 {125 if (!string.IsNullOrEmpty(strAudioName))126 {127 PlayBackground(_DicAudioClipLib[strAudioName]);128 }129 else130 {131 Debug.LogWarning("[AudioManager.cs/PlayBackground()] strAudioName==null !");132 }133 }134 135 ///136 /// 播放音效_音频源A137 /// 138 /// 音频剪辑139 public static void PlayAudioEffectA(AudioClip audioClip)140 {141 //处理全局音效音量142 _AudioSource_AudioEffectA.volume = AudioEffectVolumns;143 _AudioSource_AudioEffectA.pitch = AudioEffectPitch;144 145 if (audioClip)146 {147 _AudioSource_AudioEffectA.clip = audioClip;148 _AudioSource_AudioEffectA.Play();149 }150 else151 {152 Debug.LogWarning("[AudioManager.cs/PlayAudioEffectA()] audioClip==null ! Please Check! ");153 }154 }155 ///156 /// 播放音效_音频源A157 /// 158 /// 音效名称159 public static void PlayAudioEffectA(string strAudioEffctName)160 {161 if (!string.IsNullOrEmpty(strAudioEffctName))162 {163 PlayAudioEffectA(_DicAudioClipLib[strAudioEffctName]);164 }165 else166 {167 Debug.LogWarning("[AudioManager.cs/PlayAudioEffectA()] strAudioEffctName==null ! Please Check! ");168 }169 }170 ///171 /// 播放音效_音频源B172 /// 173 /// 音频剪辑174 public static void PlayAudioEffectB(AudioClip audioClip)175 {176 //处理全局音效音量177 _AudioSource_AudioEffectB.volume = AudioEffectVolumns;178 _AudioSource_AudioEffectB.pitch = AudioEffectPitch;179 if (audioClip)180 {181 _AudioSource_AudioEffectB.clip = audioClip;182 _AudioSource_AudioEffectB.Play();183 }184 else185 {186 Debug.LogWarning("[AudioManager.cs/PlayAudioEffectB()] audioClip==null ! Please Check! ");187 }188 }189 190 ///191 /// 播放音效_音频源B192 /// 193 /// 音效名称194 public static void PlayAudioEffectB(string strAudioEffctName)195 {196 if (!string.IsNullOrEmpty(strAudioEffctName))197 {198 PlayAudioEffectB(_DicAudioClipLib[strAudioEffctName]);199 }200 else201 {202 Debug.LogWarning("[AudioManager.cs/PlayAudioEffectB()] strAudioEffctName==null ! Please Check! ");203 }204 }205 206 207 ///208 /// 停止播放音效A209 /// 210 public static void StopPlayAudioEffectA()211 {212 _AudioSource_AudioEffectA.Stop();213 }214 215 ///216 /// 停止播放音效B217 /// 218 public static void StopPlayAudioEffectB()219 {220 _AudioSource_AudioEffectB.Stop();221 }222 223 ///224 /// 停止播放背景音乐225 /// 226 public static void StopPlayAudioBackGround()227 {228 _AudioSource_BackgroundAudio.Stop();229 }230 231 ///232 /// 改变背景音乐音量233 /// 234 /// 235 public static void SetAudioBackgroundVolumns(float floAudioBGVolumns)236 {237 _AudioSource_BackgroundAudio.volume = floAudioBGVolumns;238 AudioBackgroundVolumns = floAudioBGVolumns;239 //数据持久化240 PlayerPrefs.SetFloat("AudioBackgroundVolumns", floAudioBGVolumns);241 }242 243 244 ///245 /// 改变音效音量246 /// 247 /// 248 public static void SetAudioEffectVolumns(float floAudioEffectVolumns)249 {250 _AudioSource_AudioEffectA.volume = floAudioEffectVolumns;251 _AudioSource_AudioEffectB.volume = floAudioEffectVolumns;252 AudioEffectVolumns = floAudioEffectVolumns;253 //数据持久化254 PlayerPrefs.SetFloat("AudioEffectVolumns", floAudioEffectVolumns);255 }256 257 ///258 /// 改变背景音乐的音调259 /// 260 /// 改变的音调值261 public static void SetAudioBackgroundPitch(float floAudioBGPitchs)262 {263 _AudioSource_BackgroundAudio.pitch = floAudioBGPitchs;264 265 //数据持久化266 PlayerPrefs.SetFloat("AudioBackgroundPitch", floAudioBGPitchs);267 }268 269 ///270 /// 改变音效的音调271 /// 272 /// 音效的音调值273 public static void SetAudioEffectPitch(float floAudioEffectPitchs)274 {275 _AudioSource_AudioEffectA.pitch = floAudioEffectPitchs;276 _AudioSource_AudioEffectB.pitch = floAudioEffectPitchs;277 278 //数据持久化279 PlayerPrefs.SetFloat("AudioEffectPitch", floAudioEffectPitchs);280 }281 282 }283 }