Search Unity

How to get name from AssetReference

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

  1. BinaryEclipse

    BinaryEclipse

    Joined:
    Sep 1, 2016
    Posts:
    25
    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:
    13
    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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    954
    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:
    25
    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.
     
  5. yuch3n

    yuch3n

    Joined:
    Sep 26, 2018
    Posts:
    13
    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:
    25
    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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    954
    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:
    25
    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?
     
  10. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    954
    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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    954
    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:
    25
    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:
    25
    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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    954
    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:
    47
    plotkids likes this.