How to get name from AssetReference

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

1 Like

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.

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)

My main issue is that I am pooling items and need a unique identifier to make a dictionary. I am currently using the IAsyncOperation '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 has nothing in common besides the ā€œ.Keyā€ property, which is useless when I had previously instantiated the pool using labels.

1 Like

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ā€?

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.

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.

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

I have the same issue,too.
Does asset key will back in the next version?

1 Like

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.

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. Interface IInstanceProvider | Package Manager UI website

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

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.

1 Like

Thanks for replying

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.

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.

Hey @unity_bill , I’d also love to see a way to get the address of a loaded asset. Created a relevant post here:

2 Likes

Do you have a solution yet?

1 Like

We would also like to see this implemented.

2 Likes

so as of now the only way to get the addressable path of the loaded asset is by instantiation of the object. is it?

1 Like

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.

3 Likes