边策划边制作的游戏开发日志(四)添加音效管理系统和UI系统

为了给游戏添加背景音乐和一些音效,我添加了一个音效管理系统,十分简单,直接上代码。

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

public class AudioManager : MonoBehaviour
{
    private static AudioManager _instance;

    public AudioClip[] AudioClipArray;                     

    private static Dictionary<string, AudioClip> AudioDic = new Dictionary<string, AudioClip>(); 
    private static AudioSource audioBGM;                  
    private static AudioSource[] audioSources;


    public Slider volumeSlider;
    public float Volume { get; set; }
    public static AudioManager Instance { get => _instance; set => _instance = value; }

    void Awake()
    {
        Instance = this;
        foreach (var item in AudioClipArray)
        {
            AudioDic.Add(item.name, item);
        }
        volumeSlider.onValueChanged.AddListener((value) => SetVolume());
        audioBGM = gameObject.AddComponent<AudioSource>();
        audioSources = GetComponents<AudioSource>();

        Volume = 50;
    }

    
    public void PlayEffect(string acName)
    {
 
        if (AudioDic.ContainsKey(acName) && !string.IsNullOrEmpty(acName))
        {
            AudioClip ac = AudioDic[acName];
            PlayEffect(ac);
        }
    }

    private void PlayEffect(AudioClip ac)
    {
        if (ac)
        {

            audioSources = gameObject.GetComponents<AudioSource>();

            for (int i = 1; i < audioSources.Length; i++)
            {
                if (!audioSources[i].isPlaying)
                {
                    audioSources[i].loop = false;
                    audioSources[i].clip = ac;
                    audioSources[i].volume = Volume;
                    audioSources[i].Play();
                    return;
                }
            }

            AudioSource newAs = gameObject.AddComponent<AudioSource>();
            newAs.loop = false;
            newAs.clip = ac;
            newAs.volume = Volume;
            newAs.Play();
        }
    }

    public void PlayBGM(string acName)
    {
        if (AudioDic.ContainsKey(acName) && !string.IsNullOrEmpty(acName))
        {
            AudioClip ac = AudioDic[acName];
            PlayBGM(ac);
        }
    }

    private void PlayBGM(AudioClip ac)
    {
        if (ac)
        {
            audioBGM.clip = ac;
            audioBGM.loop = true;
            audioBGM.volume = Volume;
            audioBGM.Play();
        }
    }

    public void StopPlayBGM()
    {
        audioBGM.Stop();
    }

    public void SetVolume()
    {
        Volume =volumeSlider.value;
        for (int i = 0; i < audioSources.Length; i++)
            audioSources[i].volume = Volume;
    }
}

目前只处理了BGM和BGS,后续可能会拓展。
这套系统的优点在于可以根据需要自动增加AudioSource的数目,便于节约资源。同时具备基本的调节音量等功能。在其他脚本里通过单例调用即可。

同时我设计了一个非常简单的UI框架,用BasePanel作为页面基类。

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

public class BasePanel : MonoBehaviour
{
    private Vector3 initPos;
    protected EventDefine ShowEvent;
    protected EventDefine HideEvent;

    public virtual void Start()
    {
        initPos = transform.position;
        HideThis();
        EventCenter.AddListener(ShowEvent, ShowThis);
        EventCenter.AddListener(HideEvent, HideThis);
    }

    public virtual void OnDestroy()
    {
        EventCenter.RemoveListener(ShowEvent, ShowThis);
        EventCenter.RemoveListener(HideEvent, HideThis);
    }

    public virtual void ShowThis()
    {
        transform.position = initPos;
    }

    public virtual void HideThis()
    {
        transform.position = new Vector3(1000, 1000, 1000);
    }

}

这里用到了事件派发机制解耦,同时隐藏UI的方式是把UI页面移动到看不见的地方(节约性能)。以后UI复杂了可能会考虑拓展

你可能感兴趣的:(游戏开发,unity)