本篇内容主要讲解“Unity AudioManager声音管理器怎么用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Unity AudioManager声音管理器怎么用”吧!
10多年的聂荣网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整聂荣建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“聂荣网站设计”,“聂荣网站推广”以来,每个客户项目都认真落实执行。
我们一般会把音频分为四类:以农场类项目为例
1.背景音乐:全局只有一个,不同的场景不同的时间不同的天气都会有不同的背景音乐,其主要作用是为了烘托氛围。所以大部分的背景音乐都是2d的且音量是所有音效中最低的,目的为不突兀不刺激为主。
// 播放背景音乐 public void PlayBackgroundMusic(AudioClip clip, bool isLoop = true, float speed = 1) // 暂停播放背景音乐 public void PauseBackgroundMusic(bool isGradual = true)
2.人声配音:全局只有一个,经常会用在人物对话,npc讲话等人声场景下。当播放新的声音时会把当前的声音覆盖掉。
/// 播放单通道音效 public void PlaySingleSound(AudioClip clip, bool isLoop = false, float speed = 1) /// 暂停播放单通道音效 public void PauseSingleSound(bool isGradual = true)
3.技能ui音效:全局可以多个,在触发ui的时候或者释放技能的时候我们的配音是可以多个可重叠的。
/// 播放多通道音效 public void PlayMultipleSound(AudioClip clip, bool isLoop = false, float speed = 1) /// 停止播放指定的多通道音效 public void StopMultipleSound(AudioClip clip)
4.世界3d音效:全局可以多个,世界音效其实就是3d音,我们一般用在固定位置范围内的音效,通过设置触发距离听到音量不一样的空间声音,比如cf的敌人脚步声。
/// 播放世界音效 public void PlayWorldSound(GameObject attachTarget, AudioClip clip, bool isLoop = false, float speed = 1) /// 暂停播放指定的世界音效 public void PauseWorldSound(GameObject attachTarget, bool isGradual = true)
源代码:
using DG.Tweening; using System.Collections.Generic; using UnityEngine; public delegate void AudAction(); ////// 音频管理者 /// [DisallowMultipleComponent] public sealed class AudioManager : MonoBehaviour { ////// 是否静音初始值【请勿在代码中修改】 /// public bool MuteDefault = false; ////// 背景音乐优先级初始值【请勿在代码中修改】 /// public int BackgroundPriorityDefault = 0; ////// 单通道音效优先级初始值【请勿在代码中修改】 /// public int SinglePriorityDefault = 10; ////// 多通道音效优先级初始值【请勿在代码中修改】 /// public int MultiplePriorityDefault = 20; ////// 世界音效优先级初始值【请勿在代码中修改】 /// public int WorldPriorityDefault = 30; ////// 背景音乐音量初始值【请勿在代码中修改】 /// public float BackgroundVolumeDefault = 0.6f; ////// 单通道音效音量初始值【请勿在代码中修改】 /// public float SingleVolumeDefault = 1; ////// 多通道音效音量初始值【请勿在代码中修改】 /// public float MultipleVolumeDefault = 1; ////// 世界音效音量初始值【请勿在代码中修改】 /// public float WorldVolumeDefault = 1; ////// 单通道音效播放结束事件 /// public event AudAction SingleSoundEndOfPlayEvent; private AudioSource _backgroundAudio; private AudioSource _singleAudio; private List_multipleAudios = new List (); private Dictionary _worldAudios = new Dictionary (); private bool _singleSoundPlayDetector = false; private bool _isMute = false; private int _backgroundPriority = 0; private int _singlePriority = 10; private int _multiplePriority = 20; private int _worldPriority = 30; private float _backgroundVolume = 0.6f; private float _singleVolume = 1; private float _multipleVolume = 1; private float _worldVolume = 1; public void Awake() { _backgroundAudio = CreateAudioSource("BackgroundAudio", BackgroundPriorityDefault, BackgroundVolumeDefault, 1, 0); _singleAudio = CreateAudioSource("SingleAudio", SinglePriorityDefault, SingleVolumeDefault, 1, 0); Mute = MuteDefault; BackgroundPriority = BackgroundPriorityDefault; SinglePriority = SinglePriorityDefault; MultiplePriority = MultiplePriorityDefault; WorldPriority = WorldPriorityDefault; BackgroundVolume = BackgroundVolumeDefault; SingleVolume = SingleVolumeDefault; MultipleVolume = MultipleVolumeDefault; WorldVolume = WorldVolumeDefault; } public void Start() { StopBackgroundMusic(); StopSingleSound(); StopAllMultipleSound(); StopAllWorldSound(); } public void Update() { if (_singleSoundPlayDetector) { if (!_singleAudio.isPlaying) { _singleSoundPlayDetector = false; SingleSoundEndOfPlayEvent?.Invoke(); } } } /// /// 静音 /// public bool Mute { get { return _isMute; } set { if (_isMute != value) { _isMute = value; _backgroundAudio.mute = _isMute; _singleAudio.mute = _isMute; for (int i = 0; i < _multipleAudios.Count; i++) { _multipleAudios[i].mute = _isMute; } foreach (var audio in _worldAudios) { audio.Value.mute = _isMute; } } } } ////// 背景音乐优先级 /// public int BackgroundPriority { get { return _backgroundPriority; } set { if (_backgroundPriority != value) { _backgroundPriority = value; _backgroundAudio.priority = _backgroundPriority; } } } ////// 单通道音效优先级 /// public int SinglePriority { get { return _singlePriority; } set { if (_singlePriority != value) { _singlePriority = value; _singleAudio.priority = _singlePriority; } } } ////// 多通道音效优先级 /// public int MultiplePriority { get { return _multiplePriority; } set { if (_multiplePriority != value) { _multiplePriority = value; for (int i = 0; i < _multipleAudios.Count; i++) { _multipleAudios[i].priority = _multiplePriority; } } } } ////// 世界音效优先级 /// public int WorldPriority { get { return _worldPriority; } set { if (_worldPriority != value) { _worldPriority = value; foreach (var audio in _worldAudios) { audio.Value.priority = _worldPriority; } } } } ////// 背景音乐音量 /// public float BackgroundVolume { get { return _backgroundVolume; } set { if (!Mathf.Approximately(_backgroundVolume, value)) { _backgroundVolume = value; _backgroundAudio.volume = _backgroundVolume; } } } ////// 单通道音效音量 /// public float SingleVolume { get { return _singleVolume; } set { if (!Mathf.Approximately(_singleVolume, value)) { _singleVolume = value; _singleAudio.volume = _singleVolume; } } } ////// 多通道音效音量 /// public float MultipleVolume { get { return _multipleVolume; } set { if (!Mathf.Approximately(_multipleVolume, value)) { _multipleVolume = value; for (int i = 0; i < _multipleAudios.Count; i++) { _multipleAudios[i].volume = _multipleVolume; } } } } ////// 世界音效音量 /// public float WorldVolume { get { return _worldVolume; } set { if (!Mathf.Approximately(_worldVolume, value)) { _worldVolume = value; foreach (var audio in _worldAudios) { audio.Value.volume = _worldVolume; } } } } ////// 播放背景音乐 /// /// 音乐剪辑 /// 是否循环 /// 播放速度 public void PlayBackgroundMusic(AudioClip clip, bool isLoop = true, float speed = 1) { if (_backgroundAudio.isPlaying) { _backgroundAudio.Stop(); } _backgroundAudio.clip = clip; _backgroundAudio.loop = isLoop; _backgroundAudio.pitch = speed; _backgroundAudio.Play(); } ////// 暂停播放背景音乐 /// /// 是否渐进式 public void PauseBackgroundMusic(bool isGradual = true) { if (isGradual) { _backgroundAudio.DOFade(0, 2).OnComplete(() => { _backgroundAudio.volume = BackgroundVolume; _backgroundAudio.Pause(); }); } else { _backgroundAudio.Pause(); } } ////// 恢复播放背景音乐 /// /// 是否渐进式 public void UnPauseBackgroundMusic(bool isGradual = true) { if (isGradual) { _backgroundAudio.UnPause(); _backgroundAudio.volume = 0; _backgroundAudio.DOFade(BackgroundVolume, 2); } else { _backgroundAudio.UnPause(); } } ////// 停止播放背景音乐 /// public void StopBackgroundMusic() { if (_backgroundAudio.isPlaying) { _backgroundAudio.Stop(); } } ////// 播放单通道音效 /// /// 音乐剪辑 /// 是否循环 /// 播放速度 public void PlaySingleSound(AudioClip clip, bool isLoop = false, float speed = 1) { if (_singleAudio.isPlaying) { _singleAudio.Stop(); } _singleAudio.clip = clip; _singleAudio.loop = isLoop; _singleAudio.pitch = speed; _singleAudio.Play(); _singleSoundPlayDetector = true; } ////// 暂停播放单通道音效 /// /// 是否渐进式 public void PauseSingleSound(bool isGradual = true) { if (isGradual) { _singleAudio.DOFade(0, 2).OnComplete(() => { _singleAudio.volume = SingleVolume; _singleAudio.Pause(); }); } else { _singleAudio.Pause(); } } ////// 恢复播放单通道音效 /// /// 是否渐进式 public void UnPauseSingleSound(bool isGradual = true) { if (isGradual) { _singleAudio.UnPause(); _singleAudio.volume = 0; _singleAudio.DOFade(SingleVolume, 2); } else { _singleAudio.UnPause(); } } ////// 停止播放单通道音效 /// public void StopSingleSound() { if (_singleAudio.isPlaying) { _singleAudio.Stop(); } } ////// 播放多通道音效 /// /// 音乐剪辑 /// 是否循环 /// 播放速度 public void PlayMultipleSound(AudioClip clip, bool isLoop = false, float speed = 1) { AudioSource audio = ExtractIdleMultipleAudioSource(); audio.clip = clip; audio.loop = isLoop; audio.pitch = speed; audio.Play(); } ////// 停止播放指定的多通道音效 /// /// 音乐剪辑 public void StopMultipleSound(AudioClip clip) { for (int i = 0; i < _multipleAudios.Count; i++) { if (_multipleAudios[i].isPlaying) { if (_multipleAudios[i].clip == clip) { _multipleAudios[i].Stop(); } } } } ////// 停止播放所有多通道音效 /// public void StopAllMultipleSound() { for (int i = 0; i < _multipleAudios.Count; i++) { if (_multipleAudios[i].isPlaying) { _multipleAudios[i].Stop(); } } } ////// 销毁所有闲置中的多通道音效的音源 /// public void ClearIdleMultipleAudioSource() { for (int i = 0; i < _multipleAudios.Count; i++) { if (!_multipleAudios[i].isPlaying) { AudioSource audio = _multipleAudios[i]; _multipleAudios.RemoveAt(i); i -= 1; Destroy(audio.gameObject); } } } ////// 播放世界音效 /// /// 附加目标 /// 音乐剪辑 /// 是否循环 /// 播放速度 public void PlayWorldSound(GameObject attachTarget, AudioClip clip, bool isLoop = false, float speed = 1) { if (_worldAudios.ContainsKey(attachTarget)) { AudioSource audio = _worldAudios[attachTarget]; if (audio.isPlaying) { audio.Stop(); } audio.clip = clip; audio.loop = isLoop; audio.pitch = speed; audio.Play(); } else { AudioSource audio = AttachAudioSource(attachTarget, WorldPriority, WorldVolume, 1, 1); _worldAudios.Add(attachTarget, audio); audio.clip = clip; audio.loop = isLoop; audio.pitch = speed; audio.Play(); } } ////// 暂停播放指定的世界音效 /// /// 附加目标 /// 是否渐进式 public void PauseWorldSound(GameObject attachTarget, bool isGradual = true) { if (_worldAudios.ContainsKey(attachTarget)) { AudioSource audio = _worldAudios[attachTarget]; if (isGradual) { audio.DOFade(0, 2).OnComplete(() => { audio.volume = WorldVolume; audio.Pause(); }); } else { audio.Pause(); } } } ////// 恢复播放指定的世界音效 /// /// 附加目标 /// 是否渐进式 public void UnPauseWorldSound(GameObject attachTarget, bool isGradual = true) { if (_worldAudios.ContainsKey(attachTarget)) { AudioSource audio = _worldAudios[attachTarget]; if (isGradual) { audio.UnPause(); audio.volume = 0; audio.DOFade(WorldVolume, 2); } else { audio.UnPause(); } } } ////// 停止播放所有的世界音效 /// public void StopAllWorldSound() { foreach (var audio in _worldAudios) { if (audio.Value.isPlaying) { audio.Value.Stop(); } } } ////// 销毁所有闲置中的世界音效的音源 /// public void ClearIdleWorldAudioSource() { HashSetremoveSet = new HashSet (); foreach (var audio in _worldAudios) { if (!audio.Value.isPlaying) { removeSet.Add(audio.Key); Destroy(audio.Value); } } foreach (var item in removeSet) { _worldAudios.Remove(item); } } //创建一个音源 private AudioSource CreateAudioSource(string name, int priority, float volume, float speed, float spatialBlend) { GameObject audioObj = new GameObject(name); audioObj.transform.SetParent(transform); audioObj.transform.localPosition = Vector3.zero; audioObj.transform.localRotation = Quaternion.identity; audioObj.transform.localScale = Vector3.one; AudioSource audio = audioObj.AddComponent (); audio.playOnAwake = false; audio.priority = priority; audio.volume = volume; audio.pitch = speed; audio.spatialBlend = spatialBlend; audio.mute = _isMute; return audio; } //附加一个音源 private AudioSource AttachAudioSource(GameObject target, int priority, float volume, float speed, float spatialBlend) { AudioSource audio = target.AddComponent (); audio.playOnAwake = false; audio.priority = priority; audio.volume = volume; audio.pitch = speed; audio.spatialBlend = spatialBlend; audio.mute = _isMute; return audio; } //提取闲置中的多通道音源 private AudioSource ExtractIdleMultipleAudioSource() { for (int i = 0; i < _multipleAudios.Count; i++) { if (!_multipleAudios[i].isPlaying) { return _multipleAudios[i]; } } AudioSource audio = CreateAudioSource("MultipleAudio", MultiplePriority, MultipleVolume, 1, 0); _multipleAudios.Add(audio); return audio; } }
到此,相信大家对“Unity AudioManager声音管理器怎么用”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款