【unity基础框架】声音系统

该代码实现了一个SoundManager类,用于管理Unity游戏中的背景音乐和音效。它维护了一个AudioSource来播放背景音乐,以及一个列表来存储和管理音效。当音效播放完毕,会自动从列表中移除并销毁。提供了播放、暂停、停止背景音乐和音效的功能,并能调整音量。
摘要由CSDN通过智能技术生成

声音系统,管理音效

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

public class SoundManager : SingletonMono<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);
        }
    }

}

核心:

1.背景音乐:专门的AudioSource存储bgm,在调用方法后创建声音对象。

2.普通音效:list存储audiosource,在调用后通过update检查是否播放完毕,播放完毕则移除list并销毁。

PlayBackgroundMusic播放名字的音乐
ChangeBackgroundMusicValue修改背景音乐音量
PauseBackgroundMusic暂停背景音乐
StopBackgroundMusic停止背景音乐
PlaySound播放音效
ChangeSoundValue修改音效的音量
StopSound停止音效

测试: 

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

public class SoundTest : MonoBehaviour
{
    private bool isPlayBackgroundMusic = false;
    private bool isPlaySound = false;
    

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值