Hey everyone, I’m currently migrating our project from “Resources” to Addressables.
The worst pain so far is handling the asynchronous loading in a code base that is used to loading everything synchronously.
My question is, what is the best way to load an asset that could end up being invalidated before the loading is complete.
Consider this code:
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class BasicReference : MonoBehaviour
{
public AssetReference baseCube;
private GameObject _object = null;
public void SpawnThing()
{
this.baseCube.LoadAssetAsync<GameObject>().Completed += OnLoaded;
}
public void DestroyThing()
{
if (this._object != null)
{
Addressables.Release(this._object);
}
}
public void OnLoaded(AsyncOperationHandle<GameObject> operationHandle)
{
Debug.Assert(operationHandle.Status == AsyncOperationStatus.Succeeded);
this._object = operationHandle.Result;
// Do stuff with _object.
}
}
If SpawnThing() is called, then DestroyThing() is called before the load completion call OnLoaded(), we will end up with an asset reference leak and a wrong this._object. And we wouldn’t want that!
What would be the best way to prevent this situation?
For context, we are on PC without remote bundles, so if it comes to a dirty synchronous way of doing this I would consider it (as the current sync code works fine as it is).