diff --git a/VirtueSky/Audio/Runtime/AudioManager.cs b/VirtueSky/Audio/Runtime/AudioManager.cs index 542d8bd..609c374 100644 --- a/VirtueSky/Audio/Runtime/AudioManager.cs +++ b/VirtueSky/Audio/Runtime/AudioManager.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Linq; using UnityEngine; using VirtueSky.Core; using VirtueSky.DataType; @@ -42,8 +44,7 @@ [Space] [TitleColor("AudioManager Settings", CustomColor.DeepSkyBlue, CustomColo private SoundComponent music; - [ReadOnly, SerializeField] private DictionaryCustom dictSfxCache = - new DictionaryCustom(); + [ReadOnly, SerializeField] private List listCacheSfx = new List(); private int key = 0; @@ -100,9 +101,9 @@ void OnMusicVolumeChanged(float volume) void OnSfxVolumeChanged(float volume) { - foreach (var cache in dictSfxCache) + for (var i = 0; i < listCacheSfx.Count; i++) { - cache.Value.Volume = volume; + listCacheSfx[i].Volume = volume; } } @@ -115,7 +116,7 @@ private SoundCache PlaySfx(SoundData soundData) if (!soundData.loop) sfxComponent.OnCompleted += OnFinishPlayingAudio; SoundCache soundCache = GetSoundCache(soundData); sfxComponent.Key = key; - dictSfxCache.Add(soundCache.key, sfxComponent); + listCacheSfx.Add(sfxComponent); return soundCache; } @@ -124,10 +125,6 @@ private void StopSfx(SoundCache soundCache) var soundComponent = GetSoundComponent(soundCache); if (soundComponent == null) return; StopAndCleanAudioComponent(soundComponent); - if (dictSfxCache.ContainsKey(soundCache.key)) - { - dictSfxCache.Remove(soundCache.key); - } } private void PauseSfx(SoundCache soundCache) @@ -154,15 +151,14 @@ private void FinishSfx(SoundCache soundCache) private void StopAllSfx() { - foreach (var cache in dictSfxCache) + var listTemp = listCacheSfx.ToList(); + for (int i = 0; i < listTemp.Count; i++) { - StopAndCleanAudioComponent(cache.Value); + StopAndCleanAudioComponent(listTemp[i]); } - if (dictSfxCache.Count > 0) - { - dictSfxCache.Clear(); - } + listCacheSfx.Clear(); + listTemp.Clear(); key = 0; } @@ -225,7 +221,7 @@ void StopAndCleanAudioComponent(SoundComponent soundComponent) soundComponent.Stop(); soundComponent.gameObject.DeSpawn(); - dictSfxCache.Remove(soundComponent.Key); + if (listCacheSfx.Contains(soundComponent)) listCacheSfx.Remove(soundComponent); } void StopAudioMusic(SoundComponent soundComponent) @@ -237,13 +233,9 @@ void StopAudioMusic(SoundComponent soundComponent) SoundComponent GetSoundComponent(SoundCache soundCache) { if (soundCache == null) return null; - if (!dictSfxCache.ContainsKey(soundCache.key)) return null; - foreach (var cache in dictSfxCache.GetDict) + for (var i = 0; i < listCacheSfx.Count; i++) { - if (cache.Key == soundCache.key) - { - return cache.Value; - } + if (soundCache.key == listCacheSfx[i].Key) return listCacheSfx[i]; } return null;