mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
AudioClip caching + cleanup
This commit is contained in:
parent
931403566b
commit
bb586d4d2c
@ -161,7 +161,7 @@ namespace NewHorizons.Builder.Props
|
||||
{
|
||||
try
|
||||
{
|
||||
clip = AudioUtility.LoadAudio(mod.ModHelper.Manifest.ModFolderPath + "/" + info.AudioFilePath);
|
||||
clip = AudioUtilities.LoadAudio(mod.ModHelper.Manifest.ModFolderPath + "/" + info.AudioFilePath);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
|
||||
@ -136,6 +136,7 @@ namespace NewHorizons
|
||||
|
||||
SearchUtilities.ClearCache();
|
||||
ImageUtilities.ClearCache();
|
||||
AudioUtilities.ClearCache();
|
||||
|
||||
_isChangingStarSystem = false;
|
||||
|
||||
|
||||
@ -9,18 +9,36 @@ using UnityEngine.Networking;
|
||||
|
||||
namespace NewHorizons.Utility
|
||||
{
|
||||
public static class AudioUtility
|
||||
public static class AudioUtilities
|
||||
{
|
||||
public static AudioClip LoadAudio(string filePath)
|
||||
private static Dictionary<string, AudioClip> _loadedAudioClips = new Dictionary<string, AudioClip>();
|
||||
|
||||
public static AudioClip LoadAudio(string path)
|
||||
{
|
||||
var task = Task.Run(async () => await GetAudioClip(filePath));
|
||||
if (_loadedAudioClips.ContainsKey(path))
|
||||
{
|
||||
Logger.Log($"Already loaded audio at path: {path}");
|
||||
return _loadedAudioClips[path];
|
||||
}
|
||||
Logger.Log($"Loading audio at path: {path}");
|
||||
var task = Task.Run(async () => await GetAudioClip(path));
|
||||
task.Wait();
|
||||
_loadedAudioClips.Add(path, task.Result);
|
||||
return task.Result;
|
||||
}
|
||||
|
||||
public static void ClearCache()
|
||||
{
|
||||
foreach (var audioClip in _loadedAudioClips.Values)
|
||||
{
|
||||
if (audioClip == null) continue;
|
||||
UnityEngine.Object.Destroy(audioClip);
|
||||
}
|
||||
_loadedAudioClips.Clear();
|
||||
}
|
||||
|
||||
private static async Task<AudioClip> GetAudioClip(string filePath)
|
||||
{
|
||||
|
||||
var extension = filePath.Split(new char[] { '.' }).Last();
|
||||
|
||||
UnityEngine.AudioType audioType;
|
||||
Loading…
x
Reference in New Issue
Block a user