< Summary

Class:GDX.AddressablesExtensions
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/AddressablesExtensions.cs
Covered lines:0
Uncovered lines:33
Coverable lines:33
Total lines:114
Line coverage:0% (0 of 33)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:5
Method coverage:0% (0 of 5)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AddressablesExtensions()0%2100%
CanInstantiate(...)0%12300%
CanRelease(...)0%42600%
HasRuntimeKey(...)0%6200%
IsEmpty(...)0%2100%

File(s)

./Packages/com.dotbunny.gdx/GDX/AddressablesExtensions.cs

#LineLine coverage
 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
 7using System.Runtime.CompilerServices;
 8using UnityEngine;
 9using UnityEngine.AddressableAssets;
 10using UnityEngine.ResourceManagement.AsyncOperations;
 11
 12// ReSharper disable CommentTypo, IdentifierTypo
 13namespace 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>
 028        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)
 038        {
 039            return !IsEmpty(targetAssetReference) && HasRuntimeKey(targetAssetReference) &&
 40                   targetAssetReference.RuntimeKeyIsValid();
 041        }
 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)
 055        {
 056            if (!targetAsyncOperationHandle.IsValid())
 057            {
 058                return false;
 59            }
 60
 061            if (targetAsyncOperationHandle.Status == AsyncOperationStatus.Succeeded)
 062            {
 063                if (autoRelease)
 064                {
 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)
 067                    Addressables.ReleaseInstance(targetAsyncOperationHandle);
 068                }
 69
 070                return true;
 71            }
 72
 073            if (targetAsyncOperationHandle.Result == null)
 074            {
 075                return false;
 76            }
 77
 078            if (autoRelease)
 079            {
 080                Addressables.ReleaseInstance(targetAsyncOperationHandle);
 081            }
 82
 083            return true;
 084        }
 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)
 094        {
 095            if (targetAssetReference == null)
 096            {
 097                return false;
 98            }
 99
 0100            return !string.IsNullOrEmpty((string)targetAssetReference.RuntimeKey);
 0101        }
 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)
 0109        {
 0110            return targetAssetReference.AssetGUID == k_EmptyAssetReference.AssetGUID;
 0111        }
 112    }
 113}
 114#endif // GDX_ADDRESSABLES && !UNITY_DOTSRUNTIME