UNITY3D音频管理器

简介:在unity3d中创建公用的音频管理器,实现音频播放,并根据游戏设置中的音频开关控制音乐及音效播放

在unity3d中给gameObject添加AudioSource组件即可实现音频播放功能,但不利于统一管理,尤其是当场景中物件较多时,很难逐一开启或者关闭

最近的项目中需要可设置音乐与音效开关,为了方便统一管理自己写了一个音频管理器,主要用于以下功能:

1.将音效进行分类,每一类使用一个公用的音效播放器,统一管理场景中的音乐\音效,避免为每个物件重复添加AudioSource组件

2.避免多个2D音效同时播放,造成音效混杂刺耳

3.方便在游戏设置中对音乐/音效进行统一开关处理

4.避免因物件被销毁或禁止时造成附带音效不能正常播放

代码如下,兹以备份

using System.Collections.Generic;
using UnityEngine;

public class AM
{
    public class AudioPlayer : MonoBehaviour
    {
        public AudioSource myaudio { get; set; }
        public AudioType type { get; set; }
    }
    public enum AudioType
    {
        Sound = 0,
        Music = 1,
    }
    private static AMHelper am = (new GameObject("AMHelper")).AddComponent();
    public static Dictionary apDic = new Dictionary();
    public static int maxnum = 10;
    public static int musicOpen { get; set; }
    public static int soundOpen { get; set; }

    public static AudioPlayer AddAudioPlayer(string name, AudioType type = AudioType.Sound)
    {
        if (apDic.Count >= maxnum)
        {
            throw new System.Exception(string.Format("音频播放器数量超过{0}个限制!!!!!", maxnum));
        }
        if (apDic.ContainsKey(name))
        {
            Debug.Log(string.Format("{0}音频播放器已存在,无需添加", name));
            return apDic[name];
        }
        AudioPlayer newap = (new GameObject(name)).AddComponent() as AudioPlayer;
        apDic.Add(name, newap);
        newap.myaudio = newap.gameObject.AddComponent() as AudioSource;
        newap.type = type;
        newap.myaudio.playOnAwake = false;
        newap.transform.parent = am.transform;
        return newap;
    }

    public static void RemoveAduioPlayer(string name)
    {
        AudioPlayer audioFMS = apDic[name];
        apDic.Remove(name);
        Object.Destroy(audioFMS.gameObject);
    }

    public static bool AudioIsOpen(AudioPlayer ap)
    {
        if (musicOpen.GetPref(PrefKeys.musicOpen) == 0 && ap.type == AudioType.Music) return false;
        else if (soundOpen.GetPref(PrefKeys.soundOpen) == 0 && ap.type == AudioType.Sound) return false;
        else return true;
    }

    //音频播放(可响应音乐/音效开关控制)
    public static void Play(string apname, string clippath, bool setloop = false)
    {
        AudioPlayer ap = apDic[apname];
        //判断音频或者音效开关是否打开
        if (!AudioIsOpen(ap)) return;

        //如果是打开的才执行下面的播放
        if (!ap.myaudio.isPlaying || ap.myaudio.loop != setloop)
        {
            ap.myaudio.clip = Resources.Load(clippath);
            ap.myaudio.Play();
        }
        else ap.myaudio.PlayOneShot(Resources.Load(clippath));
        ap.myaudio.loop = setloop;
    }

    public static void Continue(string apname)
    {
        AudioPlayer ap = apDic[apname];
        //判断音频或者音效开关是否打开
        if (!AudioIsOpen(ap)) return;

        //如果是打开的才执行下面的播放
        ap.myaudio.Play();
    }

    public static AudioSource GetAudio(string apname)
    {
        AudioPlayer getap = null;
        if(apDic.TryGetValue(apname,out getap)) return getap.myaudio;
        else throw new System.Exception("没有这个播放器");
    }

    public static void CleanUp()
    {
        if (apDic.Count == 0) return;
        foreach(KeyValuePair pair in apDic)
        {
            Object.Destroy(pair.Value.gameObject);
            apDic.Remove(pair.Key);
        }
    }
}

public class AMHelper : MonoBehaviour
{
    public void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }
}

你可能感兴趣的:(UNITY3d)