Unity程序基础框架__音效管理模块

音效管理模块


作用:统一管理音乐音效相关

以前处理音效播放的时候就是哪儿需要播放音效就在哪儿去添加相关的代码段,弄得项目代码不简洁,也很冗杂,为了解决这个问题,整理了一个音效管理的模块,我们在哪儿需要播放代码的时候就直接调用一个函数就好了,这样代码又简洁又易懂。
音效资源文件要放在Resources文件夹下面,可以在目录里面再创建一个文件夹与其他资源分类开,音效放一个音效文件夹,音乐放一个文件夹,背景音乐一个文件夹,便于管理分类。


完整音效管理模块代码:

public class SoundManager : SignleBaseManager<SoundManager>
{



    private AudioSource backgroundMusic = null;

    private float backgroundValue = 1;

    private float soundValue = 1;

    private GameObject soundObj = null;

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

    public SoundManager()
    {
        MonoMgr.getInstance().AddUpdateListener(SoundUpdate);
    }

    public void SoundUpdate()
    {
        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>();
        }
        //异步加载背景音乐并且加载完成后播放
        ResManager.getInstance().LoadAysnc<AudioClip>("Music/" + name, (clip) =>
        {
            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>();
        ResManager.getInstance().LoadAysnc<AudioClip>("Music/" + name, (clip) =>
        {
            source.clip = clip;
            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);
        }
    }
}

使用:

public class SoundManagerTest : MonoBehaviour
{
    private bool isPlayBackgroundMusic = false;
    private bool isPlaySound = false;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)&&!isPlayBackgroundMusic)
        {
            isPlayBackgroundMusic = true;
            SoundManager.getInstance().PlayBackgroundMusic("BGM");
        }
        if (Input.GetMouseButtonDown(1) && !isPlaySound)
        {
            isPlaySound = true;
            SoundManager.getInstance().PlaySound("BGM",false);
        }
    }
}

泰课指路牌:https://www.taikr.com/course/1062/task/31006/show.

相关链接
Unity程序基础框架__单例基类模块
Unity程序基础框架__缓存池模块
Unity程序基础框架__事件中心模块
Unity程序基础框架__公共Mono模块
Unity程序基础框架__场景切换模块
Unity程序基础框架__资源加载模块
Unity程序基础框架__输入控制模块
Unity程序基础框架__事件中心模块基类优化
Unity程序基础框架__音效管理模块
Unity程序基础框架__UI管理模块

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值