In our game we cache a bunch of assets by label so that they are all loaded once the game begins. Ideally, we would store a mapping from address to asset in a map. Currently, it doesn’t seem easily possible to get the address of a loaded asset.
We wanted to request that there be a mechanism to get the address associated with an asset at runtime. For example, something like this:
var request = Addressables.LoadAssetAsync(“game.materials.foobar”);
yield return request;
var asset = request.Result;
// Prints “game.materials.foobar”.
var address = Addressables.GetAddress(asset);
Debug.Log(address);
Is this on the roadmap, and if not, is it something you can add soon?
I’m guessing it’s possible now, but I’m having trouble understanding how to get the AssetReference (specifically, I want to get the RuntimeKey).
Addressables.LoadAssetsAsync<GameObject>("Pooled", result => { }).Completed += OnLoadDone;
private void OnLoadDone(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<IList<GameObject>> handle)
{
var objectList = handle.Result;
foreach(GameObject item in objectList) {
// how can i get the asset reference here??
// i want something like assetReference.RuntimeKey to add it to a dictionary for a pool.
}
}
LoadResourceLocationAsync might have the asset reference key, but it doesn’t give access to the game object.
private void OnLoadDone(AsyncOperationHandle<IList<IResourceLocation>> handle)
{
var locList = handle.Result;
foreach (IResourceLocation location in locList) {
// there is location.PrimaryKey... is that the same as assetRef.RuntimeKey??
// so I may have the key for the dictionary... but now there's no GameObject??
GameObject obj = Addressables.InstantiateAsync(location); // can't do this because it's async.
}
}
So you run the async operation and hook up another completed callback. Use a lambda to capture the key so you will have the key and gameobject in the final callback…
I’m searching for a solution too and not found yet.
It would be very useful when there is beside the primarykey also the runtimekey available. Or when it’s possible to get the PrimaryKey from an AssetReference.
At the moment you have no chance to get a relation between the AssetReference and ResourceLocation, e.g. for pooling or serialization
whats the way to retrieve “label” of the object. Basically, im loading addressables using lables, now i want to know what is the label of asset that has been loaded(not the path and gameobject name)?? Can someone help? Thanks in advance.
You will have to store that information yourself. You could do it with a dictionary lookup or with a MonoBehaviour. See my post above about capturing the label with a lambda.