您好,登錄后才能下訂單哦!
本篇內容介紹了“Unity如何實現高效的音效管理類”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
自動維護一個 AudioSource的池子
如果超過了池子的峰值 池子會生成一個新的AudioSource對象來進行擴容
支持2D音效調用方式 public void PlaySound( string url, float volume = 1.0f, bool loop = false )
支持2D音樂調用方式 public void PlayMusic( string url, float volume = 1.0f, bool loop = true )
支持3D音效調用方式 public void PlaySoundAtPoint( string url, Vector3 position, float volume = 1f )
更改這塊的加載 用你們自己框架里的加載方式 或者 走 Resource.Load
using ResourceFramework; using System.Collections.Generic; using UnityEngine; public class AudioManager : SingleTon<AudioManager> { Queue<AudioSource> freeAudioSources = new Queue<AudioSource>(); List<AudioSource> activeAudioSources = new List<AudioSource>(); Dictionary<string, List<AudioSource>> playingSources = new Dictionary<string, List<AudioSource>>(); List<AudioSource> musicSources = new List<AudioSource>(); List<AudioSource> soundSources = new List<AudioSource>(); Dictionary<string, AudioClip> audioClips = new Dictionary<string, AudioClip>(); Transform transform; public float soundVolume { private set; get; } = 1.0f; public float musicVolume { private set; get; } = 1.0f; public bool soundMute { private set; get; } = false; public bool musicMute { private set; get; } = false; /// <summary> /// 初始化 給音效節點們一個爸爸 /// </summary> /// <param name="root"></param> public void Init( Transform root ) { transform = root; LoadAudioClip();//默認音效加載 } /// <summary> /// 加載本地音效到音效緩存池 /// </summary> /// <param name="url"></param> public void LoadAudioClip( string url = "Assets/AssetBundle/Sounds" ) { AudioClip[] clips = null; //#if UNITY_EDITOR // var files = System.IO.Directory.GetFiles( url ); // var clipList = new List<AudioClip>(); // foreach ( var file in files ) // { // var ac = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>( file ); // if ( null != ac ) // { // clipList.Add( ac ); // } // } // clips = clipList.ToArray(); //#else List<AudioClip> clipList = new List<AudioClip>(); for ( int i = 0; i < AllSoundName.allSoundPath.Length; i++ ) { clipList.Add( Loader.LoadAsset<AudioClip>( $"{url}/{AllSoundName.allSoundPath[ i ]}.mp3" ) ); } clips = clipList.ToArray(); //#endif foreach ( var clip in clips ) { audioClips.Add( clip.name, clip ); } } /// <summary> /// 緩存一個音效到池子里 /// </summary> /// <param name="audioClip"></param> public void Cache( AudioClip audioClip ) { if ( !audioClips.ContainsKey( audioClip.name ) ) { audioClips.Add( audioClip.name, audioClip ); } } /// <summary> /// 需要主動去調用這個更新檢查 當然你也可以開一個定時器來檢測 /// </summary> public void OnUpdate() { for ( int i = 0; i < activeAudioSources.Count; i++ ) { if ( !activeAudioSources[ i ].isPlaying ) { var free = activeAudioSources[ i ]; free.gameObject.SetActive( false ); freeAudioSources.Enqueue( free ); activeAudioSources.RemoveAt( i-- ); soundSources.Remove( free ); musicSources.Remove( free ); } } } /// <summary> /// 設置 音效系統音量 0~1 /// </summary> /// <param name="volume"></param> public void SetSoundSysVolume( float volume ) { float newVolume = Mathf.Clamp( volume, 0, 1 ); float scale = newVolume / soundVolume; soundVolume = newVolume; foreach ( var asource in soundSources ) { asource.volume *= scale; } } /// <summary> /// 設置 音樂系統音量 0~1 /// </summary> /// <param name="volume"></param> public void SetMusicSysVolume( float volume ) { float newVolume = Mathf.Clamp( volume, 0, 1 ); float scale = newVolume / musicVolume; musicVolume = newVolume; foreach ( var asource in musicSources ) { asource.volume *= scale; } } /// <summary> /// 設置音效靜音狀態 /// </summary> /// <param name="isMute"></param> public void SetSoundMuteState( bool isMute ) { soundMute = isMute; foreach ( var asource in soundSources ) { asource.mute = soundMute; } } /// <summary> /// 設置音樂靜音狀態 /// </summary> /// <param name="isMute"></param> public void SetMusicMuteState( bool isMute ) { musicMute = isMute; foreach ( var asource in musicSources ) { asource.mute = musicMute; } } /// <summary> /// 播放音樂 默認循環播放 /// </summary> /// <param name="url"></param> /// <param name="volume"></param> /// <param name="loop"></param> public void PlayMusic( string url, float volume = 1.0f, bool loop = true ) { if ( freeAudioSources.Count == 0 ) { GenAudioSouceNew(); } var audioSource = freeAudioSources.Dequeue(); activeAudioSources.Add( audioSource ); Recoverd( url, audioSource ); audioSource.gameObject.SetActive( true ); audioSource.volume = volume * musicVolume; audioSource.mute = soundMute; audioSource.loop = loop; audioSource.spatialBlend = 0f; audioSource.clip = GetAudioClip( url ); audioSource.Play(); if ( soundSources.Contains( audioSource ) ) { soundSources.Remove( audioSource ); } if ( !musicSources.Contains( audioSource ) ) { musicSources.Add( audioSource ); } } /// <summary> /// 停掉所有url相關的音效 /// 因為是可以同時存在多個相同url的音效的 它們都有自己的生命周期 /// </summary> /// <param name="url"></param> public void StopMusic( string url ) { if ( playingSources.ContainsKey( url ) ) { playingSources[ url ].ForEach( s => s.Stop() ); playingSources.Remove( url ); } } /// <summary> /// 播放音效 默認單次播放 /// </summary> /// <param name="url"></param> /// <param name="volume"></param> /// <param name="loop"></param> public void PlaySound( string url, float volume = 1.0f, bool loop = false ) { if ( freeAudioSources.Count == 0 ) { GenAudioSouceNew(); } var audioSource = freeAudioSources.Dequeue(); activeAudioSources.Add( audioSource ); Recoverd( url, audioSource ); audioSource.gameObject.SetActive( true ); audioSource.volume = volume * soundVolume; audioSource.mute = soundMute; audioSource.loop = loop; audioSource.spatialBlend = 0f; audioSource.clip = GetAudioClip( url ); audioSource.Play(); if ( musicSources.Contains( audioSource ) ) { musicSources.Remove( audioSource ); } if ( !soundSources.Contains( audioSource ) ) { soundSources.Add( audioSource ); } } /// <summary> /// 3D世界播放一個音效 指定一個位置 /// </summary> /// <param name="url"></param> /// <param name="position"></param> /// <param name="volume"></param> public void PlaySoundAtPoint( string url, Vector3 position, float volume = 1f ) { if ( freeAudioSources.Count == 0 ) { GenAudioSouceNew(); } var audioSource = freeAudioSources.Dequeue(); activeAudioSources.Add( audioSource ); Recoverd( url, audioSource ); audioSource.gameObject.transform.position = position; audioSource.gameObject.SetActive( true ); audioSource.clip = GetAudioClip( url ); audioSource.volume = volume * soundVolume; audioSource.mute = soundMute; audioSource.loop = false; audioSource.spatialBlend = 1f; audioSource.Play(); } public void PlayClipAtPoint( AudioClip clip, Vector3 position, float volume = 1f ) { foreach ( var item in audioClips ) { if ( item.Value == clip ) { PlaySoundAtPoint( item.Key, position, volume ); break; } } } public AudioClip GetAudioClip( string url ) { AudioClip clip = null; if ( audioClips.TryGetValue( url, out clip ) ) { return clip; } return null; } private void GenAudioSouceNew() { var obj = new GameObject( $"AudioComponent_{( activeAudioSources.Count + freeAudioSources.Count )}" ); obj.transform.SetParent( transform, true ); var asource = obj.AddComponent<AudioSource>(); asource.loop = false; asource.volume = 0; asource.playOnAwake = false; asource.mute = false; freeAudioSources.Enqueue( asource ); } private void Recoverd( string url, AudioSource ac ) { List<AudioSource> result = null; if ( !playingSources.TryGetValue( url, out result ) ) { result = new List<AudioSource>(); playingSources.Add( url, result ); } result.Add( ac ); } }
“Unity如何實現高效的音效管理類”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。