Skip to content

Commit

Permalink
make class for audio clip asset reference
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Sep 27, 2024
1 parent faf3b68 commit 7b515e4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
12 changes: 12 additions & 0 deletions Winch/Miscellaneous/AssetReferenceAudioClip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace UnityEngine.AddressableAssets;

[Serializable]
public class AssetReferenceAudioClip : AssetReferenceT<AudioClip>
{
public AssetReferenceAudioClip(string guid)
: base(guid)
{
}
}
15 changes: 13 additions & 2 deletions Winch/Serialization/DredgeTypeHelpers.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -461,9 +462,19 @@ public static List<AssetReferenceOverride> ParseAssetReferenceOverrides(JArray o
return parsed;
}

public static AssetReference ParseAudioReference(string o)
public static AssetReferenceAudioClip ParseAudioReference(string s)
{
return ParseAssetReference<AudioClip>(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<AudioClip>(s, out string identical))
return new AssetReferenceAudioClip(identical);

return new AssetReferenceAudioClip(string.Empty);
}

public static List<AssetReference> ParseAudioReferences(JArray o)
Expand Down
8 changes: 8 additions & 0 deletions Winch/Util/AddressablesUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ public static AssetReferenceTexture CreateAssetReferenceTexture(string namePathO
public static AssetReferenceSprite CreateAssetReferenceSprite(string namePathOrGuid)
=> new AssetReferenceSprite(GetPossibleAssetGUID<Sprite>(namePathOrGuid));

/// <inheritdoc cref="CreateAssetReference(string)"/>
public static AssetReferenceAudioClip CreateAssetReferenceAudioClip(string namePathOrGuid)
=> new AssetReferenceAudioClip(GetPossibleAssetGUID<AudioClip>(namePathOrGuid));

/// <summary>
/// Generates a new GUID in Unity's format
/// </summary>
Expand Down Expand Up @@ -365,6 +369,10 @@ public static AssetReferenceTexture3D GenerateAssetReference(string location, Te
public static AssetReferenceSprite GenerateAssetReference(string location, Sprite resource)
=> new AssetReferenceSprite(AddResourceAtLocationWithGUID(location, resource));

/// <inheritdoc cref="GenerateAssetReference(string, UnityEngine.Object)"/>
public static AssetReferenceAudioClip GenerateAssetReference(string location, AudioClip resource)
=> new AssetReferenceAudioClip(AddResourceAtLocationWithGUID(location, resource));

/// <inheritdoc cref="GenerateAssetReference(string, UnityEngine.Object)"/>
public static AssetReferenceT<T> GenerateAssetReference<T>(string location, T resource) where T : UnityEngine.Object
=> new AssetReferenceT<T>(AddResourceAtLocationWithGUID(location, resource));
Expand Down
14 changes: 7 additions & 7 deletions Winch/Util/AudioClipUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace Winch.Util;

public static class AudioClipUtil
{
private static AssetReferenceT<AudioClip> EmptyReference = new AssetReferenceT<AudioClip>(string.Empty);
private static Dictionary<string, AssetReferenceT<AudioClip>> AudioReferenceMap = new();
private static AssetReferenceAudioClip EmptyReference = new AssetReferenceAudioClip(string.Empty);
private static Dictionary<string, AssetReferenceAudioClip> AudioReferenceMap = new();
private static Dictionary<string, AudioClip> AudioClipMap = new();

public static AudioClip GetAudioClip(string key)
Expand All @@ -40,22 +40,22 @@ public static AudioClip GetAudioClip(string key)
return null;
}

public static AssetReferenceT<AudioClip> GetAudioReference(string key)
public static AssetReferenceAudioClip GetAudioReference(string key)
{
if (string.IsNullOrWhiteSpace(key))
return EmptyReference;

if (AudioReferenceMap.TryGetValue(key, out AssetReferenceT<AudioClip> audio))
if (AudioReferenceMap.TryGetValue(key, out AssetReferenceAudioClip audio))
return audio;

if (Guid.TryParse(key, out _))
return new AssetReferenceT<AudioClip>(key);
return new AssetReferenceAudioClip(key);

if (AddressablesUtil.TryGetAssetGUID(key, out string guid))
return new AssetReferenceT<AudioClip>(guid);
return new AssetReferenceAudioClip(guid);

if (AddressablesUtil.TryGetIdenticalLocationKey<AudioClip>(key, out string identical))
return new AssetReferenceT<AudioClip>(identical);
return new AssetReferenceAudioClip(identical);

WinchCore.Log.Error($"Audio reference '{key}' not found");
return EmptyReference;
Expand Down

0 comments on commit 7b515e4

Please sign in to comment.