61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Code.Infrastructure.Configs;
|
|
using Cysharp.Threading.Tasks;
|
|
using Reflex.Core;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
|
|
namespace Code.Infrastructure.Utils
|
|
{
|
|
public class MultipleConfig<TConfig> : IDisposable where TConfig : ScriptableObject
|
|
{
|
|
private AsyncOperationHandle<IList<TConfig>> _configsHandle;
|
|
|
|
public IList<TConfig> Configs => _configsHandle.Result;
|
|
|
|
public async UniTask<IList<TConfig>> LoadConfigs()
|
|
{
|
|
if (_configsHandle.IsValid()) return _configsHandle.Result;
|
|
var configLocator = Container.RootContainer.Resolve<IConfigLocator>();
|
|
|
|
var label = configLocator.TryGetManyConfigAssetReferences<TConfig>();
|
|
|
|
_configsHandle = Addressables.LoadAssetsAsync<TConfig>(label);
|
|
|
|
await _configsHandle;
|
|
|
|
return _configsHandle.Result;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if(_configsHandle.IsValid()) _configsHandle.Release();
|
|
}
|
|
}
|
|
public class SingleConfig<TConfig> : IDisposable where TConfig : ScriptableObject
|
|
{
|
|
private AsyncOperationHandle<TConfig> _configHandle;
|
|
public TConfig Config => _configHandle.IsDone ? _configHandle.Result : null;
|
|
|
|
public async UniTask<TConfig> LoadConfig()
|
|
{
|
|
if (_configHandle.IsValid()) return _configHandle.Result;
|
|
var configLocator = Container.RootContainer.Resolve<IConfigLocator>();
|
|
|
|
var label = configLocator.TryGetConfigAssetReference<TConfig>();
|
|
|
|
_configHandle = Addressables.LoadAssetAsync<TConfig>(label);
|
|
|
|
await _configHandle;
|
|
|
|
return _configHandle.Result;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if(_configHandle.IsValid()) _configHandle.Release();
|
|
}
|
|
}
|
|
} |