【Unity】音乐音效的跨关卡管理

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AudioManager : MonoBehaviour {

    private static AudioManager instance;
    public static AudioManager _instance
    {
        get
        {
            if (!instance)
            {
                instance = FindObjectOfType(typeof(AudioManager)) as AudioManager;
                if (!instance)
                {
                    var obj = new GameObject("AudioManager");
                    instance = obj.AddComponent<AudioManager>();
                    DontDestroyOnLoad(obj);                    
                }
            }
            return instance;
        }
    }
    AudioSource bgmPlayer;
    AudioSource effectPlayer;
    Dictionary<string, string> bgmDic;
    public float initBgmVolume=0.5f;
    public float initEffectVolume=0.5f;

    private void Awake()
    {
        instance = this;
        SetBgmDic();
        DontDestroyOnLoad(gameObject);
    }
    
    private void Start()
    {      
        InitPlayer();
        SetBgmDic();
    }

    private void Update()
    {
        string sceneName = SceneManager.GetActiveScene().name;
        string bgmName;
        if(bgmDic.TryGetValue(sceneName,out bgmName))
        {
            PlayeBGM(bgmName);
        }
    }

    void InitPlayer()
    {
        bgmPlayer = gameObject.AddComponent<AudioSource>();
        effectPlayer = gameObject.AddComponent<AudioSource>();
        bgmPlayer.playOnAwake = false;
        bgmPlayer.loop = true;
        //只执行一次
        if (PlayerPrefs.HasKey("isMute"))
        {
            //读档
            effectPlayer.mute = bgmPlayer.mute = PlayerPrefs.GetInt("isMute") == 0 ? false : true;
            bgmPlayer.volume = PlayerPrefs.GetFloat("bgmVolume");
            bgmPlayer.volume = PlayerPrefs.GetFloat("effectVolume");
        }
        else
        {   //初始值  
            bgmPlayer.mute = false;
            effectPlayer.mute = false;
            bgmPlayer.volume = initBgmVolume;
            effectPlayer.volume = initEffectVolume;
        }
        
    }
    void SetBgmDic()
    {
        bgmDic = new Dictionary<string, string>();
        bgmDic.Add("hair","coldNight");
    }

    //什么时候执行?需要换背景音乐的时候
    void PlayeBGM(string name)
    {
        string lastName;
        //上个场景在播放的clip以实际为准
        if (bgmPlayer.clip == null)
        {
            lastName = "";
        }
        else
        {
            lastName = bgmPlayer.clip.name;
        }
        //把last的空和不为空,进行了统一
        if (lastName!=name)
        {
            //更换
            bgmPlayer.clip = Resources.Load<AudioClip>(MainContainer.bgmFolder+ name);
            lastName = name;
            bgmPlayer.Play();
        }     
    }

    void PlayEffect(string name)
    {
        effectPlayer.clip= Resources.Load<AudioClip>(MainContainer.effectFolder+ name);
        effectPlayer.PlayOneShot(effectPlayer.clip);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值