Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

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:
    296
    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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    935
    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.
     
  3. Favo-Yang

    Favo-Yang

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

    wusticality

    Joined:
    Dec 15, 2016
    Posts:
    47
    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