From 7f8ff06b6e29def088cfaf2a37255ce4c18ac364 Mon Sep 17 00:00:00 2001 From: Jiaqi Liu Date: Sat, 12 Aug 2023 20:40:59 -0700 Subject: [PATCH] Cache all audio files --- Assets/Scripts/Pal3/Audio/AudioManager.cs | 6 ----- .../Scripts/Pal3/Data/GameResourceProvider.cs | 22 +++++-------------- .../Scripts/Pal3/GameResourceInitializer.cs | 1 - 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/Assets/Scripts/Pal3/Audio/AudioManager.cs b/Assets/Scripts/Pal3/Audio/AudioManager.cs index 181057211..a0edc8ec3 100644 --- a/Assets/Scripts/Pal3/Audio/AudioManager.cs +++ b/Assets/Scripts/Pal3/Audio/AudioManager.cs @@ -143,7 +143,6 @@ private IEnumerator PlayAudioClipAsync(AudioSource audioSource, if (audioSource.clip != null) { audioSource.Stop(); - Destroy(audioSource.clip); } audioSource.clip = audioClip; @@ -275,11 +274,6 @@ private void DestroyAllSfxAudioSource() if (audioSourceParentTransform.GetComponent() is { } audioSource) { audioSource.Stop(); - - if (audioSource.clip != null) - { - Destroy(audioSource.clip); - } } Destroy(audioSourceParentTransform.gameObject); diff --git a/Assets/Scripts/Pal3/Data/GameResourceProvider.cs b/Assets/Scripts/Pal3/Data/GameResourceProvider.cs index aac5d020d..1d0a69200 100644 --- a/Assets/Scripts/Pal3/Data/GameResourceProvider.cs +++ b/Assets/Scripts/Pal3/Data/GameResourceProvider.cs @@ -59,9 +59,6 @@ public sealed class GameResourceProvider : IDisposable, private readonly Dictionary> _gameResourceFileCache = new (); - // Cache player actor movement sfx audio clips - private readonly HashSet _audioClipCacheList = AudioConstants.PlayerActorMovementSfxAudioFileNames; - // No need to deallocate the shadow texture since it is been used almost every where. private static readonly Texture2D ShadowTexture = Resources.Load("Textures/shadow"); @@ -250,16 +247,13 @@ public IEnumerator LoadAudioClipAsync(string filePath, bool streamAudio, Action onLoaded) { - var fileName = Path.GetFileName(filePath); - var shouldCache = _audioClipCacheList.Contains(fileName); + string cacheKey = filePath.ToLower(); - if (shouldCache && _audioClipCache.ContainsKey(fileName)) + if (_audioClipCache.ContainsKey(cacheKey) && + _audioClipCache[cacheKey] != null) { - if (_audioClipCache[fileName] != null) // check if clip has been destroyed - { - onLoaded?.Invoke(_audioClipCache[fileName]); - yield break; - } + onLoaded?.Invoke(_audioClipCache[cacheKey]); + yield break; } yield return AudioClipLoader.LoadAudioClipAsync(filePath, @@ -267,11 +261,7 @@ public IEnumerator LoadAudioClipAsync(string filePath, streamAudio, audioClip => { - if (shouldCache && audioClip != null) - { - _audioClipCache[fileName] = audioClip; - } - + _audioClipCache[cacheKey] = audioClip; onLoaded?.Invoke(audioClip); }); } diff --git a/Assets/Scripts/Pal3/GameResourceInitializer.cs b/Assets/Scripts/Pal3/GameResourceInitializer.cs index c5ab15e72..80fc489bc 100644 --- a/Assets/Scripts/Pal3/GameResourceInitializer.cs +++ b/Assets/Scripts/Pal3/GameResourceInitializer.cs @@ -148,7 +148,6 @@ private IEnumerator InitResourceAsync() } // Create and register IFileReader instances using reflection - //foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { // Get all types that implement any version of IFileReader<>