< Summary

Class:DCL.Providers.AddressableResourceProvider
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Addressables/AddressableResourceProvider.cs
Covered lines:2
Uncovered lines:0
Coverable lines:2
Total lines:82
Line coverage:100% (2 of 2)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:2
Method coverage:100% (2 of 2)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Dispose()0%110100%
Initialize()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Addressables/AddressableResourceProvider.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System.Collections.Generic;
 3using System.Threading;
 4using UnityEngine;
 5using UnityEngine.AddressableAssets;
 6using UnityEngine.ResourceManagement.AsyncOperations;
 7
 8namespace DCL.Providers
 9{
 10    /// <summary>
 11    /// Service provider for addressable assets
 12    /// </summary>
 13    public class AddressableResourceProvider : IAddressableResourceProvider
 14    {
 15        /// <summary>
 16        /// Provides a list of addressable assets
 17        /// </summary>
 18        /// <param name="key">The label to look for the addressable</param>
 19        /// <param name="cancellationToken">Flow control cancellation token</param>
 20        /// <typeparam name="T">Asset type</typeparam>
 21        /// <returns></returns>
 22        public async UniTask<IList<T>> GetAddressablesList<T>(string key, CancellationToken cancellationToken = default)
 23        {
 24            //This function does nothing if initialization has already occurred
 25            await Addressables.InitializeAsync().WithCancellation(cancellationToken);
 26
 27            AsyncOperationHandle<IList<T>> request = Addressables.LoadAssetsAsync<T>(key, null);
 28            await request.WithCancellation(cancellationToken);
 29            return request.Result;
 30        }
 31
 32        /// <summary>
 33        /// Provides a single addressable asset
 34        /// </summary>
 35        /// <param name="key">Key or address of the asset to be retrieved</param>
 36        /// <param name="cancellationToken">Flow control cancellation token</param>
 37        /// <typeparam name="T">Asset type</typeparam>
 38        /// <returns></returns>
 39        public async UniTask<T> GetAddressable<T>(string key, CancellationToken cancellationToken = default)
 40        {
 41            // This function does nothing if initialization has already occurred
 42            await Addressables.InitializeAsync().WithCancellation(cancellationToken);
 43
 44            // T should be an asset type, component is not accepted
 45            if (typeof(Component).IsAssignableFrom(typeof(T)))
 46            {
 47                // load as GameObject instead as it is the root type
 48                var goRequest = Addressables.LoadAssetAsync<GameObject>(key);
 49                await goRequest.WithCancellation(cancellationToken);
 50
 51                var component = goRequest.Result.GetComponent<T>();
 52
 53                if (component == null)
 54                    throw new InvalidKeyException($"GameObject {key} does not contain the component of the requested typ
 55
 56                return component;
 57            }
 58
 59            AsyncOperationHandle<T> request = Addressables.LoadAssetAsync<T>(key);
 60            await request.WithCancellation(cancellationToken);
 61            return request.Result;
 62        }
 63
 64        public async UniTask<T> Instantiate<T>(string address, string name = default, Transform parent = null, Cancellat
 65        {
 66            // This function does nothing if initialization has already occurred
 67            await Addressables.InitializeAsync().WithCancellation(cancellationToken);
 68
 69            AsyncOperationHandle<GameObject> request = Addressables.InstantiateAsync(address, parent);
 70            await request.WithCancellation(cancellationToken);
 71
 72            if(Application.isEditor && name != default(string))
 73                request.Result.name = name;
 74
 75            return request.Result.GetComponent<T>();
 76        }
 77
 43378        public void Dispose() { }
 79
 42580        public void Initialize() { }
 81    }
 82}

Methods/Properties

Dispose()
Initialize()