Unity 音乐播放全局类
今天晚了LOL, 发现里面的声音系统做得很不错,于是最近就写了一份反正以后也用的到,2D音乐全局播放.
项目跟PoolManager对象池插件结合了.
解决的问题:
1. 已经播放的声音,可以马上暂停或者减少音量等等
2. 实现音乐全局控制
3. 音乐和音效分开控制
4. 音乐重叠播放
类如下:
AudioShot 音乐实体类
SoundController 全局音乐控制类
SoundPanel 音乐面板
代码如下:
AudioShot实体类:
using UnityEngine; using System.Collections; public class AudioShot : MonoBehaviour { public AudioSource audioSource; public string audioName; }
SoundController类:
using UnityEngine; using System.Collections; using PathologicalGames; using System.Collections.Generic; /* * 2D音乐播放全局控制类 * 如果有音乐已经在播放了,改变声音大小,或者停止播放 * 可控制已经在播放的音乐马上暂停,或改变当前的音量 */ public class SoundController : MonoBehaviour { private float fxVolume = 1; //音效声音大小 private float musicVolume = 1; //音乐声音大小 private bool isPlayFxVolume = true; //是否播放音效声音 private bool isPlayMusicVolume = true; //是否播放音乐声音 public AudioClip[] fxClip; public AudioClip[] musicClip; private SpawnPool audioSpwan; //音乐游戏物体池 private List<AudioSource> fxAudioList; //为了控制已经播放的音乐,需要记录他们的引用 private List<AudioSource> musicAudioList; public static SoundController instance; private int audioCount; //音乐下标值计时器 public void Awake() { audioSpwan = PoolManager.Pools["Audio"]; fxAudioList = new List<AudioSource>(); musicAudioList = new List<AudioSource>(); instance = this; } /// <summary> /// 播放音效不可重叠播放 /// </summary> /// <param name="name"></param> public void PlayFxSound(string name) { if (isPlayFxVolume) { for (int i = 0; i < fxClip.Length; i++) { if (name == fxClip[i].name) { audio.PlayOneShot(fxClip[i], fxVolume); return; } } } } /// <summary> /// 播放音效可重叠播放(NGUI播放方式,只能一次播放完整才能销毁,最好不要使用) /// </summary> /// <param name="name"></param> public void PlayFxSoundOverlap(string name) { if (fxClip == null) return; if (isPlayFxVolume) { for (int i = 0; i < fxClip.Length; i++) { if (name == fxClip[i].name) { //NGUITools.PlaySound(fxClipStatic[i], fxVolume); var gameObject = audioSpwan.Spawn("AudioShotGame"); gameObject.audio.clip = fxClip[i]; gameObject.audio.volume = fxVolume; gameObject.audio.Play(); AudioShot model = gameObject.GetComponent<AudioShot>(); if (string.IsNullOrEmpty(model.audioName)) { model.name = audioCount + ""; audioCount++; fxAudioList.Add(gameObject.audio); //添加到集合中 } return; } } } } /// <summary> /// 播放音乐 /// </summary> /// <param name="name"></param> public void PlayMusicSound(string name) { if(isPlayMusicVolume) { for (int i = 0; i < musicClip.Length; i++) { if (name == musicClip[i].name) { audio.PlayOneShot(musicClip[i], musicVolume); return; } } } } public void PlayMusicSoundOverlap(string name) { if (musicClip == null) return; if(isPlayMusicVolume) { for (int i = 0; i < musicClip.Length; i++) { if (name == musicClip[i].name) { var gameObject = audioSpwan.Spawn("AudioShotGame"); gameObject.audio.clip = musicClip[i]; gameObject.audio.volume = musicVolume; gameObject.audio.Play(); AudioShot model = gameObject.GetComponent<AudioShot>(); if (string.IsNullOrEmpty(model.audioName)) { model.audioName = audioCount + ""; audioCount++; //添加到集合中 musicAudioList.Add(gameObject.audio); } return; } } } } public void UpdateFxVolume(float volume) { this.fxVolume = volume; foreach (var i in fxAudioList) { i.volume = volume; } } public void UpdateMusicVolume(float volume) { this.musicVolume = volume; foreach (var i in musicAudioList) { i.volume = volume; } } public void IsPlayFxVolume(bool isPlay) { isPlayFxVolume = isPlay; if (isPlay == false) { foreach (var i in fxAudioList) { i.volume = 0; } } else { foreach (var i in fxAudioList) { i.volume = fxVolume; } } } public void IsPlayMusicVolume(bool isPlay) { isPlayMusicVolume = isPlay; if (isPlay == false) { foreach (var i in musicAudioList) { i.volume = 0; } } else { foreach (var i in musicAudioList) { i.volume = musicVolume; } } } public void ClearAudioSource() { for (int i = 0; i < musicAudioList.Count; i++) { if (musicAudioList[i].isPlaying == false) { musicAudioList.Remove(musicAudioList[i]); } } for (int i = 0; i < fxAudioList.Count; i++) { if (fxAudioList[i].isPlaying == false) { fxAudioList.Remove(fxAudioList[i]); } } } }
音乐控制面板类:
using UnityEngine; using System.Collections; public class SoundPanel : MonoBehaviour { public void UpdateFxVolume(float i) { SoundController.instance.UpdateFxVolume(i); } public void UpdateMusicVolume(float i) { SoundController.instance.UpdateMusicVolume(i); } public void isPlayFxSound(bool isOpen) { SoundController.instance.IsPlayFxVolume(isOpen); } public void isPlayMusicSound(bool isOpen) { SoundController.instance.IsPlayMusicVolume(isOpen); } public void PlayMusic() { SoundController.instance.PlayMusicSoundOverlap("3550"); } public void PlayFx() { SoundController.instance.PlayFxSoundOverlap("1843"); } }
源代码地址: http://yunpan.cn/cKSQeasSgVXbD 提取码 dedb
如果你感兴趣,你可以把你妹妹介绍给我