using System; using System.Collections.Generic; using System.Text; using UnityEngine; /// /// 声音管理类 /// public class SoundManager : MonoSingleton { #region 数据成员 //背景音乐播放组件 private AudioSource m_bgSound; //音效播放组件 private AudioSource m_effectSound; //静音管理 private bool mute = false; public bool Mute { get { return mute; } set { m_bgSound.mute = value; m_effectSound.mute = value; mute = value; } } //音乐大小 public float BgVolume { get { return m_bgSound.volume; } set { m_bgSound.volume = value; } } //音效大小 public float EffectVolume { get { return m_effectSound.volume; } set { m_effectSound.volume = value; } } #endregion /// /// 在Awake中添加两个音频播放器并初始化 /// protected void Awake() { m_bgSound = this.gameObject.AddComponent(); m_bgSound.playOnAwake = false; m_bgSound.loop = false; m_effectSound = this.gameObject.AddComponent(); m_effectSound.playOnAwake = false; } /// /// 播放BGM /// /// public void PlayBg(AudioClip audioClip) { if (Mute) return; //播放 if (audioClip != null) { m_bgSound.clip = audioClip; m_bgSound.Play(); } } /// /// 停止BGM /// public void StopBg() { if (m_bgSound.clip != null) { m_bgSound.Stop(); m_bgSound.clip = null; } } public void RePlay() { if (m_bgSound.clip != null) { m_bgSound.Play(); } } public void Pause() { if (m_bgSound.clip != null) { m_bgSound.Pause(); } } /// /// 播放音效 /// /// public void PlayEffect(AudioClip audioClip) { if (Mute) return; //播放 m_effectSound.PlayOneShot(audioClip); } /// /// 静音方法 /// public void OnMuteButtonDown() { Mute = true; } /// /// 取消静音 /// public void OnNotMuteButtonDown() { Mute = false; } }