Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to get name from AssetReference

Discussion in 'Addressables' started by BinaryEclipse, Apr 9, 2019.

  1. BinaryEclipse

    BinaryEclipse

    Joined:
    Sep 1, 2016
    Posts:
    43
    I want to simply get the name of the asset from the AssetReference, but there seems to be no way of doing this?
    Am I mistaken? Otherwise I suppose this is a feature request
     
    yuch3n likes this.
  2. yuch3n

    yuch3n

    Joined:
    Sep 26, 2018
    Posts:
    26
    I agree with this, I was hoping to get the name of an AssetReference and loading an Addressable by name with an additional label (hdpi, xhdpi etc) in order to support variants, but it seems it is not possible to access the name. Please add this as a feature.
     
  3. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    For the general ask of looking up the name: we are looking into a good way to do this, but aren't sure yet if we'll add it or not.

    for the ARef+label scenario, you can get the RuntimeKey from the asset reference. This is the GUID, but it works just like the address. you can do `Addressables.LoadAsset(aRef.RuntimeKey)`
     
  4. BinaryEclipse

    BinaryEclipse

    Joined:
    Sep 1, 2016
    Posts:
    43
    My main issue is that I am pooling items and need a unique identifier to make a dictionary. I am currently using the IAsyncOperation<GameObject> 's .Key property, however this means that I cannot populate my pool with a label (because then my key for each instantiated item would be something like "Weapons", leaving me with nothing unique). This forces me to have a huge list of every weapon and instantiate each of those individually. In case I'm unclear, if I pass an AssetReference to my spawner, it'll need to check with the pool for something that is ready for me, but IAsyncOperation<GameObject> has nothing in common besides the ".Key" property, which is useless when I had previously instantiated the pool using labels.
     
    einWikinger likes this.
  5. yuch3n

    yuch3n

    Joined:
    Sep 26, 2018
    Posts:
    26
    Hmmm... I'm not sure how that would work. For example, if I have an asset named "Duck" with a label "xxhdpi" and another version also named "Duck" with the label "hdpi", and my prefab has an AssetReference to the xxhdpi version of "Duck", if I try to load using its GUID but with the label "hdpi", how would it know that I want the hdpi version of "Duck"?
     
  6. BinaryEclipse

    BinaryEclipse

    Joined:
    Sep 1, 2016
    Posts:
    43
    I believe you just need the proper asset reference. The system doesn't really care what the name is. There's no reason to relate two different addressable assetsbased on the arbitrary name. Also, I believe you cna either load all assets of a label, or load one asset by asset reference. If you want a specific addressable, use the specific asset reference. Just use one more AssetReference serialized in your script and you can choose the one you want based on your logic.
     
  7. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    you are correct. that would not work. I must have been sleepy when I wrote that.

    we have been looking into a way to cleanly support this, but for now, you do need multiple references.
     
  8. BinaryEclipse

    BinaryEclipse

    Joined:
    Sep 1, 2016
    Posts:
    43
    As of 0.7.4, the switch to using AsyncOperationHandle means that I can't even use the (inconvenient) asset's Key as a dictionary key for my pool. Missing both the Key and the AssetReference's name leaves me in a bad spot
     
  9. whowh

    whowh

    Joined:
    Mar 7, 2019
    Posts:
    23
    I have the same issue,too.
    Does asset key will back in the next version?
     
    lclemens likes this.
  10. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    we are not planning to add this back, but we are most likely going to add in an "address" field to the operation handle. This isn't locked, so I don't want to promise it, but that's where we're leaning. So if you had a thing with address "knife" and labels "shiny" and "weapon", you could load with any of those three, but the result would indicate it was "knife".
    If we do this, it'll most likely be in the release after 0.8.
     
  11. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    On additional thing I wanted to add, this isn't well documented yet, but we are working on a sample that does pooling by creating a custom IInstanceProvider. https://docs.unity3d.com/Packages/c...ment.ResourceProviders.IInstanceProvider.html

    doing something like this, where you are injecting code inside the Addressables flow is probably the best way to do pooling. Then it doesn't matter if this was an AssetReference, or some other load.

    -Bill
     
  12. BinaryEclipse

    BinaryEclipse

    Joined:
    Sep 1, 2016
    Posts:
    43
    This would be perfect! To just use labels wherein the resultant operation handle has some unique identifier ie:'knife' for each instantiated item really is ideal as long as the same address would also be available on the AssetRerefence itself.
     
    unity_bill likes this.
  13. whowh

    whowh

    Joined:
    Mar 7, 2019
    Posts:
    23
    Thanks for replying
     
  14. BinaryEclipse

    BinaryEclipse

    Joined:
    Sep 1, 2016
    Posts:
    43
    So after the new update, you only offer PrimaryKey for LoadResourceLocationsAsync and not InstantiateAsync. Is there a reason for this, because I'd have to change a lot to do this the long way, and even if I do, still have to do the InstantiateAsync and still do boxing with lambda to include the key in with AsyncOperationHandle<GameObject>.
     
  15. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    Right now, no, you have to load locations, then the the thing you want.

    we were looking into a way to add this when doing an asset load or instantiate, but couldn't land on something that was really clean. Since we're headed into an API stabilization to drop "-preview", we didn't want to add something we'd remove later. That being said, we are still trying to come up with a solution we're happy with. One might be to actually add it to our code. Another may be for us to create a sample project (added with our other samples https://github.com/Unity-Technologies/Addressables-Sample) that shows how you could do this. I'm mostly sure it'd be possible to create a MyAddressables.Instantiate method that could return something that had both the PrimaryKey and the result of the instantiation purely in user code (without changing addressables).

    Again, it's on the list to explore, we just haven't gotten to it.
     
  16. wusticality

    wusticality

    Joined:
    Dec 15, 2016
    Posts:
    71
    lclemens and leobilck like this.
  17. JasperCiti

    JasperCiti

    Joined:
    Jul 5, 2013
    Posts:
    17
    Do you have a solution yet?
     
    lclemens likes this.
  18. OneManEscapePlan

    OneManEscapePlan

    Joined:
    Oct 14, 2015
    Posts:
    218
    We would also like to see this implemented.
     
    iSinner and lclemens like this.
  19. suryanarayana

    suryanarayana

    Joined:
    Sep 14, 2020
    Posts:
    6
    so as of now the only way to get the addressable path of the loaded asset is by instantiation of the object. is it?
     
    lclemens likes this.
  20. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    The only way to get the address is to call Addressables.LoadResourceLocationsAsync(). This gives you the locations, which know the PrimaryKey (primary key is the address).

    So if you need to know address from an instantiated thing, you'd need to build a little wrapper class that did a
    LoadResourceLocationsAsync then used that result, and saved the key.
     
  21. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    863
    Am I using it wrong? Addressables.LoadResourceLocationsAsync() returns an empty list. upload_2020-10-9_18-25-41.png

    Output:
    0
    5

    upload_2020-10-9_18-27-33.png
     
    unnanego likes this.
  22. joanpescador

    joanpescador

    Joined:
    Dec 21, 2016
    Posts:
    122
    @unity_bill Still no plan for this? It seems wierd to me that, at this point, still not possible to get a list of addressables names without instantiating the asset.
    Can you tell if this possibility has been definitively abandoned?
    Thanks,
     
    Last edited: Jan 3, 2022
    lclemens likes this.
  23. KwahuNashoba

    KwahuNashoba

    Joined:
    Mar 30, 2015
    Posts:
    110
    I actually managed to get the key (address) via:
    Code (CSharp):
    1. Addressables.LoadResourceLocationsAsync(assetReference).WaitForCompletion().First()?.PrimaryKey
    The reason that @illinar is getting an empty list might be due to the fact that he is calling
    Code (CSharp):
    1. public static AsyncOperationHandle<IList<IResourceLocation>> LoadResourceLocationsAsync(object key, Type type = null);
    2.  
    instead of
    Code (CSharp):
    1. public static AsyncOperationHandle<IList<IResourceLocation>> LoadResourceLocationsAsync(IEnumerable keys, MergeMode mode, Type type = null);
    because it tends to call an "object" version due to the fact that there is no MergeMode param provided
     
    serpin and Hypertectonic like this.
  24. joanpescador

    joanpescador

    Joined:
    Dec 21, 2016
    Posts:
    122
    It works smoothly!!
    Silly me. :oops:
    Thank you very much.
     
  25. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    760
  26. andymilsom

    andymilsom

    Unity Technologies

    Joined:
    Mar 2, 2016
    Posts:
    294
    No, there is no intention of adding the asset name to the AssetReference. We will look into the possibility of added features, but cannot guarantee that it will be implemented.

    You could utilise data in the Locator to find what you need, but it is not guaranteed to be there. The internalId may be the assetPath or a guid or dynamic minimal string, the primary key may be any of the keys and the address could be disabled from the catalog.

    We only provide the data needed for loading. To implement this would be to wastefully add data that is not needed.

    There are various ways you can implement this yourself. If getting the primary key works for you, then that should be fine. Alternatively you can keep a lookup of guid to asset name, or inherit AssetReference with a new asset name variable.
     
  27. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    760
  28. AdamBebkoSL

    AdamBebkoSL

    Joined:
    Dec 9, 2021
    Posts:
    33
    @andymilsom I would also like to find the name of the asset at runtime just to aid in logging. It's easy to get the name from GUID in editor time... but not outside the editor. I'm kind of shocked this is so difficult as I searched how to to do this.
     
  29. egor-7355

    egor-7355

    Joined:
    Sep 29, 2016
    Posts:
    5
    It seems that you can fetch the name of asset from assetReference like this (I imagine it only works if inside editor):
    assetReference.editorAsset.name
     
  30. andymilsom

    andymilsom

    Unity Technologies

    Joined:
    Mar 2, 2016
    Posts:
    294
    At editor time it is easy as you can get the asset directly.

    At runtime we do not store any excess information for debugging purposes. Though you may want to check out the Addressables profiler module (2022.2+) which may give you want you need. https://docs.unity3d.com/Packages/c...@1.21/manual/editor/tools/ProfilerModule.html

    If you want the asset name, then we would have to store it in addition to any other information anyway. In such a case there would not be any saving in using a more efficient internal naming mechanism to have shorter values. See internal asset naming mode setting from https://docs.unity3d.com/Packages/com.unity.addressables@1.21/manual/editor/groups/ContentPackingAndLoadingSchema.html#advanced-options
    This you can get from the location internalId like so:
    Code (CSharp):
    1. AssetReference myRef = default; // presume got from elsewhere
    2. var l = Addressables.LoadResourceLocationsAsync(myRef);
    3. if (!l.IsDone) // likely done immediately
    4.     yield return l;
    5. var loc = l.Result[0];
    6. string myAssetNaming = loc.InternalId; // defined by internal asset naming mode