123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- /// <summary>
- /// 声音管理类
- /// </summary>
- public class SoundManager : MonoSingleton<SoundManager>
- {
- #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
- /// <summary>
- /// 在Awake中添加两个音频播放器并初始化
- /// </summary>
- protected void Awake()
- {
- m_bgSound = this.gameObject.AddComponent<AudioSource>();
- m_bgSound.playOnAwake = false;
- m_bgSound.loop = false;
- m_effectSound = this.gameObject.AddComponent<AudioSource>();
- m_effectSound.playOnAwake = false;
- }
- /// <summary>
- /// 播放BGM
- /// </summary>
- /// <param name="audioName"></param>
- public void PlayBg(AudioClip audioClip)
- {
- if (Mute)
- return;
- //播放
- if (audioClip != null)
- {
- m_bgSound.clip = audioClip;
- m_bgSound.Play();
- }
- }
- /// <summary>
- /// 停止BGM
- /// </summary>
- 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();
- }
- }
- /// <summary>
- /// 播放音效
- /// </summary>
- /// <param name="audioName"></param>
- public void PlayEffect(AudioClip audioClip)
- {
- if (Mute)
- return;
- //播放
- m_effectSound.PlayOneShot(audioClip);
- }
- /// <summary>
- /// 静音方法
- /// </summary>
- public void OnMuteButtonDown()
- {
- Mute = true;
- }
- /// <summary>
- /// 取消静音
- /// </summary>
- public void OnNotMuteButtonDown()
- {
- Mute = false;
- }
- }
|