From 7b515e470b62aeb68ce9b3e918d2a94fc33ae89f Mon Sep 17 00:00:00 2001 From: Noah Pilarski Date: Thu, 26 Sep 2024 20:31:57 -0400 Subject: [PATCH] make class for audio clip asset reference --- Winch/Miscellaneous/AssetReferenceAudioClip.cs | 12 ++++++++++++ Winch/Serialization/DredgeTypeHelpers.cs | 15 +++++++++++++-- Winch/Util/AddressablesUtil.cs | 8 ++++++++ Winch/Util/AudioClipUtil.cs | 14 +++++++------- 4 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 Winch/Miscellaneous/AssetReferenceAudioClip.cs diff --git a/Winch/Miscellaneous/AssetReferenceAudioClip.cs b/Winch/Miscellaneous/AssetReferenceAudioClip.cs new file mode 100644 index 00000000..b124d0ee --- /dev/null +++ b/Winch/Miscellaneous/AssetReferenceAudioClip.cs @@ -0,0 +1,12 @@ +using System; + +namespace UnityEngine.AddressableAssets; + +[Serializable] +public class AssetReferenceAudioClip : AssetReferenceT +{ + public AssetReferenceAudioClip(string guid) + : base(guid) + { + } +} diff --git a/Winch/Serialization/DredgeTypeHelpers.cs b/Winch/Serialization/DredgeTypeHelpers.cs index 2626d804..2a5066cd 100644 --- a/Winch/Serialization/DredgeTypeHelpers.cs +++ b/Winch/Serialization/DredgeTypeHelpers.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Reflection.Emit; +using System.Xml.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using UnityEngine; @@ -461,9 +462,19 @@ public static List ParseAssetReferenceOverrides(JArray o return parsed; } - public static AssetReference ParseAudioReference(string o) + public static AssetReferenceAudioClip ParseAudioReference(string s) { - return ParseAssetReference(o); + if (string.IsNullOrWhiteSpace(s)) return new AssetReferenceAudioClip(string.Empty); + + if (Guid.TryParse(s, out _)) return new AssetReferenceAudioClip(s); + + if (AddressablesUtil.TryGetAssetGUID(s, out string guid)) + return new AssetReferenceAudioClip(guid); + + if (AddressablesUtil.TryGetIdenticalLocationKey(s, out string identical)) + return new AssetReferenceAudioClip(identical); + + return new AssetReferenceAudioClip(string.Empty); } public static List ParseAudioReferences(JArray o) diff --git a/Winch/Util/AddressablesUtil.cs b/Winch/Util/AddressablesUtil.cs index 2c5adbfc..dbe54317 100644 --- a/Winch/Util/AddressablesUtil.cs +++ b/Winch/Util/AddressablesUtil.cs @@ -332,6 +332,10 @@ public static AssetReferenceTexture CreateAssetReferenceTexture(string namePathO public static AssetReferenceSprite CreateAssetReferenceSprite(string namePathOrGuid) => new AssetReferenceSprite(GetPossibleAssetGUID(namePathOrGuid)); + /// + public static AssetReferenceAudioClip CreateAssetReferenceAudioClip(string namePathOrGuid) + => new AssetReferenceAudioClip(GetPossibleAssetGUID(namePathOrGuid)); + /// /// Generates a new GUID in Unity's format /// @@ -365,6 +369,10 @@ public static AssetReferenceTexture3D GenerateAssetReference(string location, Te public static AssetReferenceSprite GenerateAssetReference(string location, Sprite resource) => new AssetReferenceSprite(AddResourceAtLocationWithGUID(location, resource)); + /// + public static AssetReferenceAudioClip GenerateAssetReference(string location, AudioClip resource) + => new AssetReferenceAudioClip(AddResourceAtLocationWithGUID(location, resource)); + /// public static AssetReferenceT GenerateAssetReference(string location, T resource) where T : UnityEngine.Object => new AssetReferenceT(AddResourceAtLocationWithGUID(location, resource)); diff --git a/Winch/Util/AudioClipUtil.cs b/Winch/Util/AudioClipUtil.cs index 5b1362d4..a2e040e1 100644 --- a/Winch/Util/AudioClipUtil.cs +++ b/Winch/Util/AudioClipUtil.cs @@ -12,8 +12,8 @@ namespace Winch.Util; public static class AudioClipUtil { - private static AssetReferenceT EmptyReference = new AssetReferenceT(string.Empty); - private static Dictionary> AudioReferenceMap = new(); + private static AssetReferenceAudioClip EmptyReference = new AssetReferenceAudioClip(string.Empty); + private static Dictionary AudioReferenceMap = new(); private static Dictionary AudioClipMap = new(); public static AudioClip GetAudioClip(string key) @@ -40,22 +40,22 @@ public static AudioClip GetAudioClip(string key) return null; } - public static AssetReferenceT GetAudioReference(string key) + public static AssetReferenceAudioClip GetAudioReference(string key) { if (string.IsNullOrWhiteSpace(key)) return EmptyReference; - if (AudioReferenceMap.TryGetValue(key, out AssetReferenceT audio)) + if (AudioReferenceMap.TryGetValue(key, out AssetReferenceAudioClip audio)) return audio; if (Guid.TryParse(key, out _)) - return new AssetReferenceT(key); + return new AssetReferenceAudioClip(key); if (AddressablesUtil.TryGetAssetGUID(key, out string guid)) - return new AssetReferenceT(guid); + return new AssetReferenceAudioClip(guid); if (AddressablesUtil.TryGetIdenticalLocationKey(key, out string identical)) - return new AssetReferenceT(identical); + return new AssetReferenceAudioClip(identical); WinchCore.Log.Error($"Audio reference '{key}' not found"); return EmptyReference;