依赖于Addressable
依赖于单例模板:传送门
在这里插入图片描述

using System.Collections.Generic;
using System.Security.Cryptography;
using System;
using UnityEngine;
using UnityEngine.AddressableAssets;

namespace EasyAVG
{
    public class AudioManager : MonoSingleton<AudioManager>
    {
        private AudioSource backgroundMusic = null;

        private float backgroundValue = 1;

        private float soundValue = 1;

        private GameObject soundObj = null;

        private List<AudioSource> soundList = new List<AudioSource>();


        void Update()
        {
            for (int i = soundList.Count - 1; i > 0; i--)
            {
                if (!soundList[i].isPlaying)
                {
                    GameObject.Destroy(soundList[i]);
                    soundList.RemoveAt(i);
                }
            }
        }
        public void PlayBackgroundMusic(string name)
        {
            if (backgroundMusic == null)
            {
                GameObject obj = new GameObject("backgroundMusic");
                backgroundMusic = obj.AddComponent<AudioSource>();
            }

            var handle = Addressables.LoadAssetAsync<AudioClip>(name);
            handle.Completed +=
                (x) =>
            {
                var clip = (AudioClip)x.Result;
                backgroundMusic.clip = clip;
                backgroundMusic.loop = true;
                backgroundMusic.volume = backgroundValue;
                backgroundMusic.Play();
            };

        }

        public void ChangeBackgroundMusicValue(float v)
        {
            backgroundValue = v;
            if (backgroundMusic == null)
                return;
            backgroundMusic.volume = backgroundValue;
        }

        public void PauseBackgroundMusic()
        {
            if (backgroundMusic == null)
                return;
            backgroundMusic.Pause();
        }

        public void StopBackgroundMusic()
        {
            if (backgroundMusic == null)
                return;
            backgroundMusic.Stop();
        }

        public void PlaySound(string name, bool isLoop, Action<AudioSource> callback = null)
        {
            if (soundObj == null)
            {
                soundObj = new GameObject();
                soundObj.name = "Sounds";
            }
            AudioSource source = soundObj.AddComponent<AudioSource>();
            var handle = Addressables.LoadAssetAsync<AudioClip>(name);
            handle.Completed +=
                (x) =>
                {
                    source.clip = (AudioClip)x.Result;
                    source.loop = isLoop;
                    source.volume = soundValue;
                    source.Play();
                    //音效异步加载结束后,将这个音效组件加入集合
                    soundList.Add(source);
                    if (callback != null)
                        callback(source);
                };
        }

        public void ChangeSoundValue(float v)
        {
            soundValue = v;
            for (int i = 0; i < soundList.Count; i++)
            {
                soundList[i].volume = v;
            }
        }

        public void StopSound(AudioSource source)
        {
            if (soundList.Contains(source))
            {
                soundList.Remove(source);
                source.Stop();
                GameObject.Destroy(source);
            }
        }
    }
}