本篇內(nèi)容主要講解“Unity AudioManager聲音管理器怎么用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Unity AudioManager聲音管理器怎么用”吧!
10多年的聶榮網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整聶榮建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“聶榮網(wǎng)站設(shè)計”,“聶榮網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
我們一般會把音頻分為四類:以農(nóng)場類項目為例
1.背景音樂:全局只有一個,不同的場景不同的時間不同的天氣都會有不同的背景音樂,其主要作用是為了烘托氛圍。所以大部分的背景音樂都是2d的且音量是所有音效中最低的,目的為不突兀不刺激為主。
// 播放背景音樂 public void PlayBackgroundMusic(AudioClip clip, bool isLoop = true, float speed = 1) // 暫停播放背景音樂 public void PauseBackgroundMusic(bool isGradual = true)
2.人聲配音:全局只有一個,經(jīng)常會用在人物對話,npc講話等人聲場景下。當(dāng)播放新的聲音時會把當(dāng)前的聲音覆蓋掉。
/// 播放單通道音效 public void PlaySingleSound(AudioClip clip, bool isLoop = false, float speed = 1) /// 暫停播放單通道音效 public void PauseSingleSound(bool isGradual = true)
3.技能ui音效:全局可以多個,在觸發(fā)ui的時候或者釋放技能的時候我們的配音是可以多個可重疊的。
/// 播放多通道音效 public void PlayMultipleSound(AudioClip clip, bool isLoop = false, float speed = 1) /// 停止播放指定的多通道音效 public void StopMultipleSound(AudioClip clip)
4.世界3d音效:全局可以多個,世界音效其實就是3d音,我們一般用在固定位置范圍內(nèi)的音效,通過設(shè)置觸發(fā)距離聽到音量不一樣的空間聲音,比如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; ////// 背景音樂優(yōu)先級初始值【請勿在代碼中修改】 /// public int BackgroundPriorityDefault = 0; ////// 單通道音效優(yōu)先級初始值【請勿在代碼中修改】 /// public int SinglePriorityDefault = 10; ////// 多通道音效優(yōu)先級初始值【請勿在代碼中修改】 /// public int MultiplePriorityDefault = 20; ////// 世界音效優(yōu)先級初始值【請勿在代碼中修改】 /// public int WorldPriorityDefault = 30; ////// 背景音樂音量初始值【請勿在代碼中修改】 /// public float BackgroundVolumeDefault = 0.6f; ////// 單通道音效音量初始值【請勿在代碼中修改】 /// public float SingleVolumeDefault = 1; ////// 多通道音效音量初始值【請勿在代碼中修改】 /// public float MultipleVolumeDefault = 1; ////// 世界音效音量初始值【請勿在代碼中修改】 /// public float WorldVolumeDefault = 1; ////// 單通道音效播放結(jié)束事件 /// 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; } } } } ////// 背景音樂優(yōu)先級 /// public int BackgroundPriority { get { return _backgroundPriority; } set { if (_backgroundPriority != value) { _backgroundPriority = value; _backgroundAudio.priority = _backgroundPriority; } } } ////// 單通道音效優(yōu)先級 /// public int SinglePriority { get { return _singlePriority; } set { if (_singlePriority != value) { _singlePriority = value; _singleAudio.priority = _singlePriority; } } } ////// 多通道音效優(yōu)先級 /// public int MultiplePriority { get { return _multiplePriority; } set { if (_multiplePriority != value) { _multiplePriority = value; for (int i = 0; i < _multipleAudios.Count; i++) { _multipleAudios[i].priority = _multiplePriority; } } } } ////// 世界音效優(yōu)先級 /// 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; } } } } ////// 播放背景音樂 /// /// 音樂剪輯 /// 是否循環(huán) /// 播放速度 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(); } } ////// 恢復(fù)播放背景音樂 /// /// 是否漸進式 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(); } } ////// 播放單通道音效 /// /// 音樂剪輯 /// 是否循環(huán) /// 播放速度 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(); } } ////// 恢復(fù)播放單通道音效 /// /// 是否漸進式 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(); } } ////// 播放多通道音效 /// /// 音樂剪輯 /// 是否循環(huán) /// 播放速度 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); } } } ////// 播放世界音效 /// /// 附加目標(biāo) /// 音樂剪輯 /// 是否循環(huán) /// 播放速度 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(); } } ////// 暫停播放指定的世界音效 /// /// 附加目標(biāo) /// 是否漸進式 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(); } } } ////// 恢復(fù)播放指定的世界音效 /// /// 附加目標(biāo) /// 是否漸進式 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); } } //創(chuàng)建一個音源 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聲音管理器怎么用”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!