| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.AddressableAssets; |
| | 6 | | using UnityEngine.ResourceManagement.AsyncOperations; |
| | 7 | |
|
| | 8 | | namespace 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 | |
|
| 433 | 78 | | public void Dispose() { } |
| | 79 | |
|
| 425 | 80 | | public void Initialize() { } |
| | 81 | | } |
| | 82 | | } |