04、Unity_声音管理器

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<string, AudioClip> _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         /// <summary>
 40         /// 音效库资源加载
 41         /// </summary>
 42         void Awake()
 43         {
 44             //音频库加载[初始化,将音乐剪辑和名字联系起来]
 45             _DicAudioClipLib = new Dictionary<string, AudioClip>();
 46 
 47             foreach (AudioClip audioClip in AudioClipArray)
 48             {
 49                 _DicAudioClipLib.Add(audioClip.name, audioClip);
 50             }
 51 
 52 
 53             //处理音频源,也就是得到用来播放声音的音乐播放器
 54             _AudioSourceArray = this.GetComponents<AudioSource>();
 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         /// <summary>
 91         /// 播放背景音乐
 92         /// 传入的参数是背景音乐的AudioClip
 93         /// </summary>
 94         /// <param name="audioClip">音频剪辑</param>
 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             else
113             {
114                 Debug.LogWarning("[AudioManager.cs/PlayBackground()] audioClip==null !");
115             }
116         }
117 
118         /// <summary>
119         /// 播放背景音乐
120         /// 传入的参数是声音片段的名字,要注意,其声音片段要加入声音数组中
121         /// </summary>
122         /// <param name="strAudioName"></param>
123         public static void PlayBackground(string strAudioName)
124         {
125             if (!string.IsNullOrEmpty(strAudioName))
126             {
127                 PlayBackground(_DicAudioClipLib[strAudioName]);
128             }
129             else
130             {
131                 Debug.LogWarning("[AudioManager.cs/PlayBackground()] strAudioName==null !");
132             }
133         }
134 
135         /// <summary>
136         /// 播放音效_音频源A
137         /// </summary>
138         /// <param name="audioClip">音频剪辑</param>
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             else
151             {
152                 Debug.LogWarning("[AudioManager.cs/PlayAudioEffectA()] audioClip==null ! Please Check! ");
153             }
154         }
155         /// <summary>
156         /// 播放音效_音频源A
157         /// </summary>
158         /// <param name="strAudioEffctName">音效名称</param>
159         public static void PlayAudioEffectA(string strAudioEffctName)
160         {
161             if (!string.IsNullOrEmpty(strAudioEffctName))
162             {
163                 PlayAudioEffectA(_DicAudioClipLib[strAudioEffctName]);
164             }
165             else
166             {
167                 Debug.LogWarning("[AudioManager.cs/PlayAudioEffectA()] strAudioEffctName==null ! Please Check! ");
168             }
169         }
170         /// <summary>
171         /// 播放音效_音频源B
172         /// </summary>
173         /// <param name="audioClip">音频剪辑</param>
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             else
185             {
186                 Debug.LogWarning("[AudioManager.cs/PlayAudioEffectB()] audioClip==null ! Please Check! ");
187             }
188         }
189 
190         /// <summary>
191         /// 播放音效_音频源B
192         /// </summary>
193         /// <param name="strAudioEffctName">音效名称</param>
194         public static void PlayAudioEffectB(string strAudioEffctName)
195         {
196             if (!string.IsNullOrEmpty(strAudioEffctName))
197             {
198                 PlayAudioEffectB(_DicAudioClipLib[strAudioEffctName]);
199             }
200             else
201             {
202                 Debug.LogWarning("[AudioManager.cs/PlayAudioEffectB()] strAudioEffctName==null ! Please Check! ");
203             }
204         }
205 
206 
207         /// <summary>
208         /// 停止播放音效A
209         /// </summary>
210         public static void StopPlayAudioEffectA()
211         {
212             _AudioSource_AudioEffectA.Stop();
213         }
214 
215         /// <summary>
216         /// 停止播放音效B
217         /// </summary>
218         public static void StopPlayAudioEffectB()
219         {
220             _AudioSource_AudioEffectB.Stop();
221         }
222 
223         /// <summary>
224         /// 停止播放背景音乐
225         /// </summary>
226         public static void StopPlayAudioBackGround()
227         {
228             _AudioSource_BackgroundAudio.Stop();
229         }
230 
231         /// <summary>
232         /// 改变背景音乐音量
233         /// </summary>
234         /// <param name="floAudioBGVolumns"></param>
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         /// <summary>
245         /// 改变音效音量
246         /// </summary>
247         /// <param name="floAudioEffectVolumns"></param>
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         /// <summary>
258         /// 改变背景音乐的音调
259         /// </summary>
260         /// <param name="floAudioBGPichs">改变的音调值</param>
261         public static void SetAudioBackgroundPitch(float floAudioBGPitchs)
262         {
263             _AudioSource_BackgroundAudio.pitch = floAudioBGPitchs;
264 
265             //数据持久化
266             PlayerPrefs.SetFloat("AudioBackgroundPitch", floAudioBGPitchs);
267         }
268 
269         /// <summary>
270         /// 改变音效的音调
271         /// </summary>
272         /// <param name="floAudioEffectPitchs">音效的音调值</param>
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 }

 

 
 

 

 
 

 

 
 

 

 
 

 

 
 

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注