Search Unity

Get the address of a loaded asset.

Discussion in 'Addressables' started by wusticality, Jun 22, 2019.

Thread Status:
Not open for further replies.
  1. wusticality

    wusticality

    Joined:
    Dec 15, 2016
    Posts:
    71
    Hello,

    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<Material>("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?

    Thanks,

    Kevin
     
    guaralism and EwieElektro like this.
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
  3. wusticality

    wusticality

    Joined:
    Dec 15, 2016
    Posts:
    71
    Unfortunately PrimaryKey is not supported yet on IResourceLocation as near as I can tell. Not sure on the ETA of the next release.

    EDIT: Aaaand it just dropped, 5 minutes later haha
     
    Last edited: Jun 23, 2019
  4. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    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).

    Code (CSharp):
    1. Addressables.LoadAssetsAsync<GameObject>("Pooled", result => { }).Completed += OnLoadDone;
    Code (CSharp):
    1. private void OnLoadDone(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<IList<GameObject>> handle)
    2. {
    3.     var objectList = handle.Result;
    4.     foreach(GameObject item in objectList) {
    5.         // how can i get the asset reference here??
    6.         // i want something like assetReference.RuntimeKey to add it to a dictionary for a pool.
    7.     }
    8. }
     
  5. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    Use
    Addressables.LoadResourceLocationsAsync
     
  6. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    LoadResourceLocationAsync might have the asset reference key, but it doesn't give access to the game object.

    Code (CSharp):
    1. private void OnLoadDone(AsyncOperationHandle<IList<IResourceLocation>> handle)
    2. {
    3.     var locList = handle.Result;
    4.     foreach (IResourceLocation location in locList) {
    5.  
    6.         // there is location.PrimaryKey... is that the same as assetRef.RuntimeKey??
    7.  
    8.         // so I may have the key for the dictionary... but now there's no GameObject??
    9.         GameObject obj = Addressables.InstantiateAsync(location); // can't do this because it's async.
    10.     }
    11. }
     
  7. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    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..
     
  8. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Thanks for the help. ugh, what a mess....
     
  9. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    201
    @unity_bill is this feature coming any time soon?
     
  10. EwieElektro

    EwieElektro

    Joined:
    Feb 22, 2016
    Posts:
    45
    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
     
    lclemens and Luis_Fernandes like this.
  11. sandeepsmartest

    sandeepsmartest

    Joined:
    Nov 7, 2012
    Posts:
    139
    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.
     
    lclemens likes this.
  12. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    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.
     
Thread Status:
Not open for further replies.