1. 程式人生 > >Unity實現通用的專案音訊管理器

Unity實現通用的專案音訊管理器

一、音訊管理器指令碼如下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;                                    

public class AudioManager : MonoBehaviour {
    public AudioClip[] AudioClipArray;                               //剪輯陣列
    public static float AudioBackgroundVolumns = 1F;                 //背景音量
    public static float AudioEffectVolumns = 1F;                     //音效音量

    private static Dictionary<string, AudioClip> _DicAudioClipLib;   //音訊庫
    private static AudioSource[] _AudioSourceArray;                  //音訊源陣列
    private static AudioSource _AudioSource_BackgroundAudio;         //背景音樂
    private static AudioSource _AudioSource_AudioEffectA;            //音效源A
    private static AudioSource _AudioSource_AudioEffectB;            //音效源B

    /// <summary>
    /// 音效庫資源載入
    /// </summary>
	void Awake() {
	    //音訊庫載入
        _DicAudioClipLib = new Dictionary<string, AudioClip>();
        foreach (AudioClip audioClip in AudioClipArray){
            _DicAudioClipLib.Add(audioClip.name,audioClip);
        }
        //處理音訊源
        _AudioSourceArray=this.GetComponents<AudioSource>();
        _AudioSource_BackgroundAudio = _AudioSourceArray[0];
        _AudioSource_AudioEffectA = _AudioSourceArray[1];
        _AudioSource_AudioEffectB = _AudioSourceArray[2];

        //從資料持久化中得到音量數值
        if (PlayerPrefs.GetFloat("AudioBackgroundVolumns")>=0){
            AudioBackgroundVolumns = PlayerPrefs.GetFloat("AudioBackgroundVolumns");
            _AudioSource_BackgroundAudio.volume = AudioBackgroundVolumns;
        }
        if (PlayerPrefs.GetFloat("AudioEffectVolumns")>=0){
            AudioEffectVolumns = PlayerPrefs.GetFloat("AudioEffectVolumns");
            _AudioSource_AudioEffectA.volume = AudioEffectVolumns;
            _AudioSource_AudioEffectB.volume = AudioEffectVolumns;
        }
	}//Start_end

    /// <summary>
    /// 播放背景音樂
    /// </summary>
    /// <param name="audioClip">音訊剪輯</param>
    public static void PlayBackground(AudioClip audioClip){
        //防止背景音樂的重複播放。
        if (_AudioSource_BackgroundAudio.clip == audioClip){
            return;
        }
        //處理全域性背景音樂音量
        _AudioSource_BackgroundAudio.volume = AudioBackgroundVolumns;
        if (audioClip){
            _AudioSource_BackgroundAudio.clip = audioClip;
            _AudioSource_BackgroundAudio.Play();
        }else{
            Debug.LogWarning("[AudioManager.cs/PlayBackground()] audioClip==null !");
        }
    }

    //播放背景音樂
    public static void PlayBackground(string strAudioName){
        if (!string.IsNullOrEmpty(strAudioName)){
            PlayBackground(_DicAudioClipLib[strAudioName]);
        }else{
            Debug.LogWarning("[AudioManager.cs/PlayBackground()] strAudioName==null !");            
        }
    }

    /// <summary>
    /// 播放音效_音訊源A
    /// </summary>
    /// <param name="audioClip">音訊剪輯</param>
    private static void PlayAudioEffectA(AudioClip audioClip){
        //處理全域性音效音量
        _AudioSource_AudioEffectA.volume = AudioEffectVolumns;

        if (audioClip){
            _AudioSource_AudioEffectA.clip = audioClip;
            _AudioSource_AudioEffectA.Play();
        }
        else{
            Debug.LogWarning("[AudioManager.cs/PlayAudioEffectA()] audioClip==null ! Please Check! ");
        }
    }

    /// <summary>
    /// 播放音效_音訊源B
    /// </summary>
    /// <param name="audioClip">音訊剪輯</param>
    private static void PlayAudioEffectB(AudioClip audioClip){
        //處理全域性音效音量
        _AudioSource_AudioEffectB.volume = AudioEffectVolumns;

        if (audioClip){
            _AudioSource_AudioEffectB.clip = audioClip;
            _AudioSource_AudioEffectB.Play();
        }
        else{
            Debug.LogWarning("[AudioManager.cs/PlayAudioEffectB()] audioClip==null ! Please Check! ");
        }
    }

    /// <summary>
    /// 播放音效_音訊源A
    /// </summary>
    /// <param name="strAudioEffctName">音效名稱</param>
    public static void PlayAudioEffectA(string strAudioEffctName)
    {
        if (!string.IsNullOrEmpty(strAudioEffctName)){
            PlayAudioEffectA(_DicAudioClipLib[strAudioEffctName]);
        }
        else{
            Debug.LogWarning("[AudioManager.cs/PlayAudioEffectA()] strAudioEffctName==null ! Please Check! ");
        }
    }

    /// <summary>
    /// 播放音效_音訊源B
    /// </summary>
    /// <param name="strAudioEffctName">音效名稱</param>
    public static void PlayAudioEffectB(string strAudioEffctName)
    {
        if (!string.IsNullOrEmpty(strAudioEffctName))
        {
            PlayAudioEffectB(_DicAudioClipLib[strAudioEffctName]);
        }
        else
        {
            Debug.LogWarning("[AudioManager.cs/PlayAudioEffectB()] strAudioEffctName==null ! Please Check! ");
        }
    }

    /// <summary>
    /// 改變背景音樂音量
    /// </summary>
    /// <param name="floAudioBGVolumns"></param>
    public static void SetAudioBackgroundVolumns(float floAudioBGVolumns){
        _AudioSource_BackgroundAudio.volume = floAudioBGVolumns;
        AudioBackgroundVolumns = floAudioBGVolumns;
        //資料持久化
        PlayerPrefs.SetFloat("AudioBackgroundVolumns", floAudioBGVolumns);
    }

    /// <summary>
    /// 改變音效音量
    /// </summary>
    /// <param name="floAudioEffectVolumns"></param>
    public static void SetAudioEffectVolumns(float floAudioEffectVolumns){
        _AudioSource_AudioEffectA.volume = floAudioEffectVolumns;
        _AudioSource_AudioEffectB.volume = floAudioEffectVolumns;
        AudioEffectVolumns = floAudioEffectVolumns;
        //資料持久化
        PlayerPrefs.SetFloat("AudioEffectVolumns", floAudioEffectVolumns);
    }
}//Class_end

二、該“音訊管理器”指令碼的使用方法

①在專案的層級檢視中新建一個空物件"_AudioManager" 

②把AudioManager.cs指令碼賦值給空物件“_AudioManager”

③拖拽檢視中的音效剪輯檔案到對應的引數列表中,

④在專案中新增“音訊監聽(Audio Listener)”與3個“音訊源(Audio Sources)”元件即可

 

注意:本內容來自《Unity3D/2D遊戲開發從0到1》的第24章內容