| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | #if GDX_ADDRESSABLES && !UNITY_DOTSRUNTIME |
| | 6 | |
|
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.AddressableAssets; |
| | 10 | | using UnityEngine.ResourceManagement.AsyncOperations; |
| | 11 | |
|
| | 12 | | // ReSharper disable CommentTypo, IdentifierTypo |
| | 13 | | namespace GDX |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Addressables Based Extension Methods |
| | 17 | | /// </summary> |
| | 18 | | /// <remarks> |
| | 19 | | /// <para>Requires UnityEngine.CoreModule.dll to function correctly.</para> |
| | 20 | | /// <para>Requires <c>com.unity.addressables</c> Package.</para> |
| | 21 | | /// </remarks> |
| | 22 | | [VisualScriptingCompatible(2)] |
| | 23 | | public static class AddressablesExtensions |
| | 24 | | { |
| | 25 | | /// <summary> |
| | 26 | | /// An empty instance of an <see cref="UnityEngine.AddressableAssets.AssetReference" /> to be used for compa |
| | 27 | | /// </summary> |
| 0 | 28 | | static readonly AssetReference k_EmptyAssetReference = new AssetReference(); |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// <para>Can <paramref name="targetAssetReference" /> be instantiated at runtime?</para> |
| | 32 | | /// </summary> |
| | 33 | | /// <remarks>Checks that it is not empty, has a runtime key, and makes sure the key is valid.</remarks> |
| | 34 | | /// <param name="targetAssetReference">The target <see cref="UnityEngine.AddressableAssets.AssetReference" />.</ |
| | 35 | | /// <returns>true/false</returns> |
| | 36 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 37 | | public static bool CanInstantiate(this AssetReference targetAssetReference) |
| 0 | 38 | | { |
| 0 | 39 | | return !IsEmpty(targetAssetReference) && HasRuntimeKey(targetAssetReference) && |
| | 40 | | targetAssetReference.RuntimeKeyIsValid(); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Can the <paramref name="targetAsyncOperationHandle" /> be released? |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="targetAsyncOperationHandle"> |
| | 47 | | /// A target <see cref="UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle{T}" /> |
| | 48 | | /// typed as <see cref="UnityEngine.GameObject" />. |
| | 49 | | /// </param> |
| | 50 | | /// <param name="autoRelease">If it can, should the handle release?</param> |
| | 51 | | /// <returns>true/false</returns> |
| | 52 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 53 | | public static bool CanRelease(this AsyncOperationHandle<GameObject> targetAsyncOperationHandle, |
| | 54 | | bool autoRelease = false) |
| 0 | 55 | | { |
| 0 | 56 | | if (!targetAsyncOperationHandle.IsValid()) |
| 0 | 57 | | { |
| 0 | 58 | | return false; |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | if (targetAsyncOperationHandle.Status == AsyncOperationStatus.Succeeded) |
| 0 | 62 | | { |
| 0 | 63 | | if (autoRelease) |
| 0 | 64 | | { |
| | 65 | | // Not entirely sure about this one, as it makes sense that we should be moving to a instanced versi |
| | 66 | | // however games have shipped with the older method (.Release) |
| 0 | 67 | | Addressables.ReleaseInstance(targetAsyncOperationHandle); |
| 0 | 68 | | } |
| | 69 | |
|
| 0 | 70 | | return true; |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | if (targetAsyncOperationHandle.Result == null) |
| 0 | 74 | | { |
| 0 | 75 | | return false; |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | if (autoRelease) |
| 0 | 79 | | { |
| 0 | 80 | | Addressables.ReleaseInstance(targetAsyncOperationHandle); |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | return true; |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// <para>Does <paramref name="targetAssetReference" /> have a runtime key?</para> |
| | 88 | | /// </summary> |
| | 89 | | /// <remarks>Will return false if the reference is <see langword="null" />.</remarks> |
| | 90 | | /// <param name="targetAssetReference">The target <see cref="UnityEngine.AddressableAssets.AssetReference" />.</ |
| | 91 | | /// <returns>true/false</returns> |
| | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 93 | | public static bool HasRuntimeKey(this AssetReference targetAssetReference) |
| 0 | 94 | | { |
| 0 | 95 | | if (targetAssetReference == null) |
| 0 | 96 | | { |
| 0 | 97 | | return false; |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | return !string.IsNullOrEmpty((string)targetAssetReference.RuntimeKey); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Is <paramref name="targetAssetReference" /> empty? |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="targetAssetReference">The target <see cref="UnityEngine.AddressableAssets.AssetReference" />.</ |
| | 107 | | /// <returns>true/false</returns> |
| | 108 | | public static bool IsEmpty(this AssetReference targetAssetReference) |
| 0 | 109 | | { |
| 0 | 110 | | return targetAssetReference.AssetGUID == k_EmptyAssetReference.AssetGUID; |
| 0 | 111 | | } |
| | 112 | | } |
| | 113 | | } |
| | 114 | | #endif // GDX_ADDRESSABLES && !UNITY_DOTSRUNTIME |