public enum AudioType
{
    BackgroundMusic,
    TY_Click,
    Jump,
}
首先我们定义三种音效,….反正嘛,,,自己push进去几种…
新建一个 AudioManager 类,这个类切换场景我们保持不销毁,因为游戏所有场景都会有声音的存在…嗷呜~~嗷呜~~嗷呜~~
游戏中,一般我们播放一种背景音乐。以及 多种点击多个button产生的不同点击音效…等等
在游戏设置中,我们可以设置音效大小以及关闭  (关闭==零嘛)
    private static AudioManager audioManager;
    public static AudioManager Instance { get { return audioManager; } }
 //播放声音的组件…都懂的
    private AudioSource audioSource;
//声音的类型,以及路径。这个类型如果觉得一个Type不够,自己可以按需求加。
    private Dictionary<AudioType, string> audioPathDict;
//缓存音频文件,下次可以直接用,不需要再加载
    private Dictionary<AudioType, AudioClip> audioClipDic;
//缓存池,音频组件
    private List<AudioSource> CacheList;
//使用中的,音频组件
    private List<AudioSource> UseList;
//音乐的音量
    private float musicVolume = 1;
//音效的音量
    private float soundVolume = 1;
//音乐音量 用于本地读取PlayerPrefs
    private string musicVolumePrefs = “MusicVolume”;
//音效音效 用于本地读取PlayerPrefs
    private string soundVolumePrefs = “SoundVolume”;
//音频组件缓存池最大长度
    private int CacheMaxCount = 10;
    void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
        audioManager = this;
        //可以使用xml或者json直接读取,这边测试,直接写死
        audioPathDict = new Dictionary<AudioType, string>()
        {
            { AudioType.BackgroundMusic, “AudioClip/Music/MainMenuScene” },
            { AudioType.TY_Click, “AudioClip/Sound/Sound1”},
            { AudioType.Jump, “AudioClip/Sound/Sound2”},
        };
        audioSource = gameObject.AddComponent<AudioSource>();
        CacheList = new List<AudioSource>();
        UseList = new List<AudioSource>();
        audioClipDic = new Dictionary<AudioType, AudioClip>();
    }
    void Start()
    {
        // 从本地缓存读取声音音量
        if (PlayerPrefs.HasKey(musicVolumePrefs))
        {
            musicVolume = PlayerPrefs.GetFloat(musicVolumePrefs);
        }
        if (PlayerPrefs.HasKey(soundVolumePrefs))
        {
            musicVolume = PlayerPrefs.GetFloat(soundVolumePrefs);
        }
    }
    /// <summary>
    /// 播放背景音乐
    /// </summary>
    /// <param name=”id“></param>
    /// <param name=”loop“></param>
    public void PlayMusic(AudioType type, bool loop = true)
    {
            audioSource.clip = GetAudioClip(type);
            audioSource.clip.LoadAudioData();
            audioSource.loop = loop;
            audioSource.volume = musicVolume;
            audioSource.Play();
    }
    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name=”id“></param>
    public void PlaySound(AudioType type)
    {
        //当缓存池没有的时候,新建
        if (CacheList.Count != 0)
        {
            AudioSource audioSource = UnusedToUsed();
            audioSource.clip = GetAudioClip(type);
            audioSource.clip.LoadAudioData();
            audioSource.Play();
            StartCoroutine(WaitPlayOver(audioSource));
        }
        else
        {
            AddAudioSource();
            AudioSource audioSource = UnusedToUsed();
            audioSource.clip = GetAudioClip(type);
            audioSource.clip.LoadAudioData();
            audioSource.volume = soundVolume;
            audioSource.loop = false;
            audioSource.Play();
            StartCoroutine(WaitPlayOver(audioSource));
        }
    }
    /// <summary>
    /// 当播放音效结束后,将其移至未使用集合
    /// </summary>
    /// <param name=”audioSource“></param>
    /// <returns></returns>
    IEnumerator WaitPlayOver(AudioSource audioSource)
    {
        //等待音乐播放完毕
        yield return new WaitUntil(() => { return !audioSource.isPlaying; });
        //加入缓存
        UsedToUnused(audioSource);
    }
    /// <summary>
    /// 获取音频文件,获取后会缓存一份
    /// </summary>
    /// <param name=”id“></param>
    /// <returns></returns>
    private AudioClip GetAudioClip(AudioType type)
    {
        if (!audioClipDic.ContainsKey(type))
        {
            if (!audioPathDict.ContainsKey(type))
                return null;
            AudioClip ac = Resources.Load(audioPathDict[type]) as AudioClip;
            audioClipDic.Add(type, ac);
        }
        return audioClipDic[type];
    }
    /// <summary>
    /// 添加音频组件
    /// </summary>
    /// <returns></returns>
    private AudioSource AddAudioSource()
    {
        if (CacheList.Count != 0)
        {
            return UnusedToUsed();
        }
        else
        {
            AudioSource audioSource = gameObject.AddComponent<AudioSource>();
            CacheList.Add(audioSource);
            return audioSource;
        }
    }
    /// <summary>
    /// 从对象池取出未使用的音频
    /// </summary>
    /// <returns></returns>
    private AudioSource UnusedToUsed()
    {
        AudioSource audioSource = CacheList[0];
        CacheList.RemoveAt(0);
        UseList.Add(audioSource);
        return audioSource;
    }
    /// <summary>
    /// 使用音频结束后加入缓存池
    /// </summary>
    /// <param name=”audioSource“></param>
    private void UsedToUnused(AudioSource audioSource)
    {
        UseList.Remove(audioSource);
        if (CacheList.Count >= CacheMaxCount)
        {
            Destroy(audioSource);
        }
        else
        {
            CacheList.Add(audioSource);
        }
    }
    /// <summary>
    /// 修改音乐音量
    /// </summary>
    /// <param name=”volume“></param>
    public void ChangeMusicVolume(float volume)
    {
        musicVolume = volume;
        audioSource.volume = volume;
        PlayerPrefs.SetFloat(musicVolumePrefs, volume);
    }
    /// <summary>
    /// 修改音效音量
    /// </summary>
    /// <param name=”volume“></param>
    public void ChangeSoundVolume(float volume)
    {
        soundVolume = volume;
        for (int i = 0; i < CacheList.Count; i++)
        {
            CacheList[i].volume = volume;
        }
        for (int i = 0; i < UseList.Count; i++)
        {
            UseList[i].volume = volume;
        }
        PlayerPrefs.SetFloat(soundVolumePrefs, volume);
    }