Unity 声音系统
1.制作混音器创建完混声器之后在window菜单栏下找到AudioMixer面板打开创建声道并且暴漏exposeVolume参数供脚本使用首先选中声道右键Volume点第一个expose我已经暴漏过了所以现在是Unexpose。Sound声道同理暴漏完参数修改一下名字等会儿好用2.UI界面配置需要两个滑动条slider和两个toggleslider需要把Background放未填充的背景Fill里放已填充的背景toggle需要把label字体删掉background放关闭的uicheckmark放打开的ui3.脚本1UISystemConfig这个绑定在音乐控制面板上using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; public class UISystemConfig : MonoBehaviour { public Image musicOff; public Image soundOff; public Toggle toggleMusic; public Toggle toggleSound; public Slider sliderMusic; public Slider sliderSound; private void Start() { this.toggleMusic.isOn Config.MusicOn; this.toggleSound.isOn Config.SoundOn; this.sliderMusic.value Config.MusicVolume; this.sliderSound.value Config.SoundVolume; } public void MusicToggle(bool on) { musicOff.enabled !on; Config.MusicOn on; SoundManager.Instance.PlaySound(SoundDefine.click); } public void SoundToggle(bool on) { soundOff.enabled !on; Config.SoundOn on; SoundManager.Instance.PlaySound(SoundDefine.click); } public void MusicVolume(float value) { Config.MusicVolume (int)value; PlaySound(); } public void SoundVolume(float value) { Config.SoundVolume (int)value; PlaySound(); } float lastPlay 0; private void PlaySound() { if (Time.realtimeSinceStartup - lastPlay 0.1) { lastPlay Time.realtimeSinceStartup; SoundManager.Instance.PlaySound(SoundDefine.click); } } }2Config不用挂任何数据中转站using System.Collections; using System.Collections.Generic; using UnityEngine; public class Config { public static bool MusicOn { get { return PlayerPrefs.GetInt(Music, 1) 1; } set { PlayerPrefs.SetInt(Music, value ? 1 : 0); SoundManager.Instance.MusicOn value; } } public static bool SoundOn { get { return PlayerPrefs.GetInt(Sound, 1) 1; } set { PlayerPrefs.SetInt(Sound, value ? 1 : 0); SoundManager.Instance.SoundOn value; } } public static int MusicVolume { get { return PlayerPrefs.GetInt(MusicVolume, 100); } set { PlayerPrefs.SetInt(MusicVolume, value); SoundManager.Instance.MusicVolume value; } } public static int SoundVolume { get { return PlayerPrefs.GetInt(SoundVolume, 100); } set { PlayerPrefs.SetInt(SoundVolume, value); SoundManager.Instance.SoundVolume value; } } //~Config() //{ // PlayerPrefs.Save(); //} }3音源配置表using System.Collections; using System.Collections.Generic; using UnityEngine; public class SoundDefine : MonoBehaviour { public const string Index bg1; public const string click click; }如果你想能直接用我的写法那你存放的资源必须和我的路径一致4SoundManger,这个挂空物体上在主页面放置不销毁using System.Collections; using System.Collections.Generic; using System.Globalization; using UnityEngine; using UnityEngine.Audio; public class SoundManager: MonoSingletonSoundManager { public AudioMixer audioMixer; public AudioSource musicAudioSource; public AudioSource soundAudioSource; const string MusicPath Music/; const string SoundPath Sound/; private bool musicOn; private bool soundOn; private int musicVolume; private int soundVolume; void Start() { this.musicVolume Config.MusicVolume; this.soundVolume Config.SoundVolume; this.MusicOn Config.MusicOn; this.SoundOn Config.SoundOn; } public bool MusicOn { get { return musicOn; } set { musicOn value; this.MusicMute(!musicOn); } } public int MusicVolume { get { return musicVolume; } set { if(musicVolume ! value) { musicVolume value; if (musicOn) this.SetVolume(MusicVolume, musicVolume); } } } public int SoundVolume { get { return soundVolume; } set { if (soundVolume ! value) { soundVolume value; if (soundOn) this.SetVolume(SoundVolume, soundVolume); } } } public bool SoundOn { get { return soundOn; } set { soundOn value; this.SoundMute(!soundOn); } } public void MusicMute(bool mute) { this.SetVolume(MusicVolume, mute ? 0 : musicVolume); } public void SoundMute(bool mute) { this.SetVolume(SoundVolume, mute ? 0 : soundVolume); } private void SetVolume(string name,int value) { float volume value * 0.5f - 50f; this.audioMixer.SetFloat(name, volume); } public void PlayMusic(string name) { AudioClip clip Resources.LoadAudioClip(MusicPath name); if(clip null) { return; } if (musicAudioSource.isPlaying) { musicAudioSource.Stop(); } musicAudioSource.clip clip; musicAudioSource.Play(); } public void PlaySound(string name) { AudioClip clip Resources.LoadAudioClip(SoundPath name); if (clip null) { return; } soundAudioSource.PlayOneShot(clip); } }配置单例类同样不用挂载任何物体上using UnityEngine; public abstract class MonoSingletonT : MonoBehaviour where T : MonoBehaviour { private static T _instance; public static T Instance { get { if (_instance null) { _instance FindObjectOfTypeT(); if (_instance null) { GameObject obj new GameObject(typeof(T).Name); _instance obj.AddComponentT(); } } return _instance; } } protected virtual void Awake() { if (_instance null) { _instance this as T; DontDestroyOnLoad(gameObject); // 切场景不销毁 } else { Destroy(gameObject); // 防止重复 } } }4.设置1.slider和toggle要绑上方法注意是静态2.拖拽音源的时候请注意由于soundManager和两个音源放在一起所以要先点组件再拖不要直接拽soundManager空物体一个一个点击组件拖到下面的方框3.output记得选一个music一个sound,如果想让音乐循环可以勾选音乐loop4.使用直接用就好了举例using System.Collections; using System.Collections.Generic; using UnityEngine; public class test : MonoBehaviour { public GameObject MusicPanel; // Start is called before the first frame update void Start() { SoundManager.Instance.PlayMusic(SoundDefine.Index); } // Update is called once per frame void Update() { } //Button public void MusicSetting() { SoundManager.Instance.PlayMusic(SoundDefine.click); MusicPanel.SetActive(true); } public void close() { SoundManager.Instance.PlayMusic(SoundDefine.click); MusicPanel.SetActive(false); } public void click() { } }