Solutions
Addressables
using UnityEngine;
using UnityEngine.AddressableAssets;
using System.Collections.Generic;
using System.Threading.Tasks;
public static class AddressableSOHelper
{
private static Dictionary<AssetReference, ScriptableObject> _loadedInstances = new Dictionary<AssetReference, ScriptableObject>();
public static async Task<T> LoadSOAsync<T>(AssetReference assetReference) where T : ScriptableObject
{
if (_loadedInstances.TryGetValue(assetReference, out var loadedInstance))
{
return loadedInstance as T;
}
var loadOperation = assetReference.LoadAssetAsync<T>();
T instance = await loadOperation.Task;
_loadedInstances[assetReference] = instance;
return instance;
}
public static void ReleaseAll()
{
foreach (var assetReference in _loadedInstances.Keys)
{
assetReference.ReleaseAsset();
}
_loadedInstances.Clear();
}
}Last updated