效果
自动维护一个 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{ Queue freeAudioSources = new Queue (); List activeAudioSources = new List (); Dictionary > playingSources = new Dictionary >(); List musicSources = new List (); List soundSources = new List (); Dictionary audioClips = new Dictionary (); 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; /// /// 初始化 给音效节点们一个爸爸 /// /// public void Init( Transform root ) { transform = root; LoadAudioClip();//默认音效加载 } ////// 加载本地音效到音效缓存池 /// /// 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(); // foreach ( var file in files ) // { // var ac = UnityEditor.AssetDatabase.LoadAssetAtPath ( file ); // if ( null != ac ) // { // clipList.Add( ac ); // } // } // clips = clipList.ToArray(); //#else List clipList = new List (); for ( int i = 0; i < AllSoundName.allSoundPath.Length; i++ ) { clipList.Add( Loader.LoadAsset ( $"{url}/{AllSoundName.allSoundPath[ i ]}.mp3" ) ); } clips = clipList.ToArray(); //#endif foreach ( var clip in clips ) { audioClips.Add( clip.name, clip ); } } /// /// 缓存一个音效到池子里 /// /// public void Cache( AudioClip audioClip ) { if ( !audioClips.ContainsKey( audioClip.name ) ) { audioClips.Add( audioClip.name, audioClip ); } } ////// 需要主动去调用这个更新检查 当然你也可以开一个定时器来检测 /// 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 ); } } } ////// 设置 音效系统音量 0~1 /// /// 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; } } ////// 设置 音乐系统音量 0~1 /// /// 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; } } ////// 设置音效静音状态 /// /// public void SetSoundMuteState( bool isMute ) { soundMute = isMute; foreach ( var asource in soundSources ) { asource.mute = soundMute; } } ////// 设置音乐静音状态 /// /// public void SetMusicMuteState( bool isMute ) { musicMute = isMute; foreach ( var asource in musicSources ) { asource.mute = musicMute; } } ////// 播放音乐 默认循环播放 /// /// /// /// 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 ); } } ////// 停掉所有url相关的音效 /// 因为是可以同时存在多个相同url的音效的 它们都有自己的生命周期 /// /// public void StopMusic( string url ) { if ( playingSources.ContainsKey( url ) ) { playingSources[ url ].ForEach( s => s.Stop() ); playingSources.Remove( url ); } } ////// 播放音效 默认单次播放 /// /// /// /// 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 ); } } ////// 3D世界播放一个音效 指定一个位置 /// /// /// /// 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(); asource.loop = false; asource.volume = 0; asource.playOnAwake = false; asource.mute = false; freeAudioSources.Enqueue( asource ); } private void Recoverd( string url, AudioSource ac ) { List result = null; if ( !playingSources.TryGetValue( url, out result ) ) { result = new List (); playingSources.Add( url, result ); } result.Add( ac ); } }
以上就是Unity实现高效的音效管理类的示例代码的详细内容,更多关于Unity音效管理类的资料请关注脚本之家其它相关文章!