2020-11-03 Unity 简单音效控制脚本

using UnityEngine;
using System.Collections.Generic;

/// 
/// *音乐管理器
/// 
public class AudioController : SingletonObject
{
    /// 
    /// 背景音
    /// 
    AudioSource bgmAudio;
    /// 
    /// 特效音列表
    /// 
    private List effectList = new List();
    /// 
    /// 路径
    /// 
    public string audioPath= "Source/";

    /// 
    /// 播放背景音乐
    /// 
    public void PlayBgm(string bgmName)
    {
        if (MusicSwitch)
        {
            if (bgmAudio == null) 
            {
                bgmAudio = gameObject.AddComponent();
            }

            bgmAudio.loop = true;
            bgmAudio.volume = Sound;
            AudioClip clip = ResourceLoader.Ins.Load(audioPath + bgmName);
            bgmAudio.clip = clip;
            bgmAudio.Play();
        }
    }

    /// 
    /// 暂停背景音乐
    /// 
    public void BGMPause()
    {
        if (bgmAudio!=null)
        {
            bgmAudio.Pause();
        }
    }

    /// 
    /// 停止背景音乐
    /// 
    public void BGMStop()
    {
        if (bgmAudio != null)
        {
            bgmAudio.Stop();
        }
    }

    /// 
    /// 背景音乐重新播放
    /// 
    public void BGMReplay()
    {
        if (MusicSwitch)
        {
            if (bgmAudio != null)
            {
                bgmAudio.Play();
            }
        }
    }

    /// 
    /// 设置背景音音量
    /// 
    public void SetBgmVolume(float valume)
    {
        if (bgmAudio != null)
        {
            bgmAudio.volume = valume;
        }
    }

    /// 
    /// 播放游戏对战音效
    /// 
    public void PlayEffect(string name, bool loop = false,float volume = -1)
    {
        if (!SoundSwitch) return;

        AudioSource source = GetAudio();
        AudioClip clip = ResourceLoader.Ins.Load(audioPath + name);
        if (clip != null)
        {
            if(volume==-1)
                 source.volume = Sound;
            else
                source.volume = volume;
            source.loop = loop;
            source.clip = clip;
            source.Play();
        }
        else
        {
            Debug.LogError("PlayGameEffect is null:"+name);
        }
    }

    /// 
    /// 从缓存中获取播放完毕的Audio
    /// 
    AudioSource GetAudio() 
    {
        AudioSource source = null;
        foreach (var item in effectList)
        {
            if (!item.isPlaying)
            {
                source = item;
                break;
            }
        }

        if (source == null)
        {
            source = gameObject.AddComponent();
            effectList.Add(source);
        }
        return source;
    }

    /// 
    /// 停止指定音效
    /// 
    public void StopGameEffect(string name)
    {
        foreach (var item in effectList)
        {
            if (item.clip.name == name )
            {
                item.Stop();
            }
        }
    }

    /// 
    /// 暂停音效
    /// 
    public void PauseEffect(string name)
    {
        foreach (var item in effectList)
        {
            if (item.clip.name == name)
            {
                item.Pause();
            }
        }
    }

    /// 
    /// 暂停所有音效
    /// 
    public void PauseAll()
    {
        foreach (var item in effectList)
        {
             item.Pause();
        }
    }

    /// 
    /// 设置音量
    /// 
    public void SetVolumeAll(float volume)
    {
        SetBgmVolume(volume);
        foreach (var item in effectList)
        {
            item.volume = volume;
        }
    }

    /// 
    /// 背景音乐开关
    /// 
    public  bool MusicSwitch
    {
        get
        {
            string swi = PlayerPrefs.GetString("musicswitch","true");
            return bool.Parse(swi);
        }
        set
        {
            PlayerPrefs.SetString("musicswitch", value.ToString());
            if (!value)
            {
                BGMStop();
            }
            else 
            {
                BGMReplay();
            }
        }
    }

    /// 
    /// 特效声音开关 音效
    /// 
    public  bool SoundSwitch
    {
        get
        {
            string swi = PlayerPrefs.GetString("SoundSwitch","true");
            return bool.Parse(swi);
        }
        set
        {
            PlayerPrefs.SetString("SoundSwitch", value.ToString());
            if (!value)
            {
                PauseAll();
            }
        }
    }

    /// 
    /// 音量
    /// 
    public float Sound
    {
        get
        {
            return PlayerPrefs.GetFloat("sound", 1);
        }
        set
        {
            PlayerPrefs.SetFloat("sound", value);
            SetVolumeAll(value);
        }
    }

}

你可能感兴趣的:(2020-11-03 Unity 简单音效控制脚本)