| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Providers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.AddressableAssets; |
| | 7 | | using UnityEngine.ResourceManagement.AsyncOperations; |
| | 8 | |
|
| | 9 | | namespace MainScripts.DCL.Controllers.AssetManager.Addressables.Editor |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// <see cref="AddressableResourceProvider"/> does not advance the UniTask if not in the play mode. |
| | 13 | | /// it is a synchronous replacements to operates from Unit tests fully synchronously |
| | 14 | | /// </summary> |
| | 15 | | public class EditorAddressableResourceProvider : IAddressableResourceProvider |
| | 16 | | { |
| | 17 | | public UniTask<IList<T>> GetAddressablesList<T>(string key, CancellationToken cancellationToken = default) |
| | 18 | | { |
| 0 | 19 | | EnsureInitializedSync(); |
| 0 | 20 | | var r = UnityEngine.AddressableAssets.Addressables.LoadAssetsAsync<T>(key, null).WaitForCompletion(); |
| 0 | 21 | | return UniTask.FromResult(r); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public UniTask<T> GetAddressable<T>(string key, CancellationToken cancellationToken = default) |
| | 25 | | { |
| 0 | 26 | | EnsureInitializedSync(); |
| | 27 | |
|
| 0 | 28 | | if (typeof(Component).IsAssignableFrom(typeof(T))) |
| | 29 | | { |
| | 30 | | // load as GameObject instead as it is the root type |
| 0 | 31 | | var goRequest = UnityEngine.AddressableAssets.Addressables.LoadAssetAsync<GameObject>(key); |
| 0 | 32 | | goRequest.WaitForCompletion(); |
| | 33 | |
|
| 0 | 34 | | var component = goRequest.Result.GetComponent<T>(); |
| | 35 | |
|
| 0 | 36 | | if (component == null) |
| 0 | 37 | | throw new InvalidKeyException($"GameObject {key} does not contain the component of the requested typ |
| | 38 | |
|
| 0 | 39 | | return UniTask.FromResult(component); |
| | 40 | | } |
| | 41 | |
|
| 0 | 42 | | AsyncOperationHandle<T> request = UnityEngine.AddressableAssets.Addressables.LoadAssetAsync<T>(key); |
| 0 | 43 | | request.WaitForCompletion(); |
| 0 | 44 | | return UniTask.FromResult(request.Result); |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public UniTask<T> Instantiate<T>(string address, string name = "", Transform parent = null, CancellationToken ca |
| | 48 | | { |
| 0 | 49 | | EnsureInitializedSync(); |
| | 50 | |
|
| 0 | 51 | | var request = UnityEngine.AddressableAssets.Addressables.InstantiateAsync(address, parent); |
| 0 | 52 | | request.WaitForCompletion(); |
| | 53 | |
|
| 0 | 54 | | if (Application.isEditor && name != default(string)) |
| 0 | 55 | | request.Result.name = name; |
| | 56 | |
|
| 0 | 57 | | return UniTask.FromResult(request.Result.GetComponent<T>()); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | private void EnsureInitializedSync() |
| | 61 | | { |
| 0 | 62 | | var init = UnityEngine.AddressableAssets.Addressables.InitializeAsync(); |
| 0 | 63 | | init.WaitForCompletion(); |
| 0 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | public void Dispose() { } |
| | 67 | |
|
| 0 | 68 | | public void Initialize() { } |
| | 69 | | } |
| | 70 | | } |