How do we get the GUID/AssetReference at runtime?

I have a portrait picker which loads a bunch of sprites by label and presents them to the player. I need to serialize the player’s choice but I can’t figure out how to get a GUID or AssetRefrence at runtime?

I see that the ResourceLocation stores the primary key but that isn’t a stable reference. If the sprite name needs to be changed in an update poof my serialized data is suddenly invalid.

As far as I understand it, you would use the address. Sure, the address can be manually changed which would invalidate your save file, but why would you change the address? The address is different from the asset name in the project and I believe it’s easy to commit to the rule that once the player is released none of the addresses may change.

If you mark a folder addressable then changing the file name will automatically change the address. Perhaps this is a bug but it’s how it works.

I’ll point out a further point too. The design intent of addressables is for designers to change the address names to something suitable. This is a useful feature for various reasons. In all the examples you’ll see them giving addresses useful names. The address names aren’t even required to be unique! For this use case I absolutely require a unique reference.

That’s what makes AssetReference important though. It’s always valid even if the address name changes and it points to one specific asset. That’s why it has GUID as a backing.

I have EXACTLY the same application and of course problem.

I have not figured out any good way to do it with the current system, in general the “addressables” package leaves so much to be desired. Getting a unique address during runtime (and also having something better than an arbitrary string as input that as you point out changes when you move the file around) seems super essential to me and I do not understand why it is not trivial to obtain.

In any case I will give you a “hack” solution.

Make a SO called portraitHolder add your portrait texture as a field + a string for your GUID create the SO and add your portraits manually record its GUID to the GUID field. In your game load and save the SO and obtain the portrait from there. Myself I made a menu item that can automatically record GUIDs of SOs to speed up the process,

use AssetDatabase.TryGetGUIDAndLocalFileIdentifier

Looks like the Addressables system will generate an AssetReference object for you if you follow the instructions here: Getting started with Addressable Assets | Package Manager UI website

Essentially, on your prefab you need to have a script with a public AssetReference variable exposed. Drag the prefab into that field in the inspector to populate the reference. Now, when you query that field on the prefab’s component at runtime you can get the GUID, the name, or just serialise the whole AssetReference.

Hey - I’ve done something very similar. I’ve made a bunch of SO’s for portraitData which has a serialized field for asset reference (prefab) as well as an int for ID (I have an editor script to sanity check all the id’s). Then add each asset to a label.

Then when initialising - I go through each ref in the group of labels, and shove the ref into a dictionary of int, ref.

When saving the game, I just save the ID to the save file and can reference it easily when needed.