Search Unity

How to get path or label after asset loaded?

Discussion in 'Addressables' started by Favo-Yang, May 28, 2019.

  1. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    I intend to load a list of sprites by label, then store into a dictionary (key=path, value=sprite). I can achieve this by loading locations by label, then load sprite one by one.

    Code (CSharp):
    1. var locations = await Addressables.LoadResourceLocationsAsync(label.labelString).Task;
    2. foreach (var loc in locations)
    3. {
    4.     var sprite= await Addressables.LoadAssetAsync<Sprite>(location).Task;
    5.     myDict[loc.InternalId] = sprite;
    6. }
    I guess this approach will be slower than Addressables.LoadAssetsAsync api, which takes a list of location as param, and returns a list of assets. However I'm not sure if the order of the locations param and the returned assets are matched. If so, I can build the dictionary from there.

    Code (CSharp):
    1. var locations = await Addressables.LoadResourceLocationsAsync(label.labelString).Task;
    2. var sprites = await Addressables.LoadAssetsAsync<Sprite>(locations, null).Task;
    3. for (int i=0; i<locations.Count; i++)
    4. {
    5.     myDict[locations[i].InternalId] = sprites[i];
    6. }
    Or if there a way I can get a wrappered object of asset, something like AssetReference, which exposes asset, path, or even label property of the asset? Some dummy code below,

    Code (CSharp):
    1. var locations = await Addressables.LoadResourceLocationsAsync(label.labelString).Task;
    2. var assetReferences = await Addressables.LoadAssetReferencesAsync<Sprite>(locations, null).Task;
    3. foreach (var assetRef in assetReferences)
    4. {
    5.     myDict[assetRef.RuntimeKey] = assetRef.asset;
    6. }
     
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    In the upcoming release, the location will contain a PrimaryKey. This will be the address unless you alter the default behavior. Hopefully that helps your use case.
     
    lclemens likes this.
  3. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    It's better than InternalId I guess.
     
  4. wusticality

    wusticality

    Joined:
    Dec 15, 2016
    Posts:
    71
    Hey @unity_bill, any ETA on when PrimaryKey will drop? I also require this behaviour (though I'd much rather have a function to get the address of a loaded asset).

    See my post here: https://forum.unity.com/threads/get-the-address-of-a-loaded-asset.699404/#post-4676705

    EDIT: Aaaand it just dropped, 5 minutes later haha
     
    Last edited: Jun 23, 2019
    Mikael-H likes this.
  5. sandeepsmartest

    sandeepsmartest

    Joined:
    Nov 7, 2012
    Posts:
    139
    @unity_bill So, 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.
  6. GeekOutGamer

    GeekOutGamer

    Joined:
    Dec 11, 2013
    Posts:
    4
    I'm having the same issue with LoadAssetsAsync and not knowing the order they returned in. Is this now possible?

    I like the idea of Addressables but the documentation and subsequent code examples on the interwebs are negligible.
     
    lclemens likes this.
  7. GeekOutGamer

    GeekOutGamer

    Joined:
    Dec 11, 2013
    Posts:
    4
    I've done a bit of playing around. It appears that using the result Action<TObject> on LoadAssetsAsync will just spit them out at you as and when each one completes. However, if you use the Completed event handler, then it will have a list in the requested order.
     
  8. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    So fetching the primary key after calling LoadAssetsAsync("mylable") .... was the claim that this would be implemented back in 2019 false?