Using addressables with a save system

We are working on a game where the user can add objects to a scene and will need to be able to save the scene and load it later. The Addressables system initially sounded like a great solution; this is what I envisioned:

  1. We add prefabs to the Addressables system
  2. The player adds objects (instantiated from those prefabs) to a scene.
  3. When the player saves, we serialize each object’s Addressables address, position and rotation, etc.
  4. When the player loads, we deserialize the data. For each object, we instantiate from the corresponding prefab using the Addressables address, then apply the position, rotation, etc.

The problem is that it seems very difficult (impossible?) to actually retrieve an Addressable’s address at runtime.

If we do [SerializeField] private GameObject prefab;, we don’t know anything about whether the prefab asset is included in the Addressables system:

If we load assets from Addressables using a label Addressables.LoadAssetsAsync<T>(label, null).Completed += OnAssetsLoaded; , we also don’t get any information about the assets’ addresses.

We can use AssetReferenceGameObject (which is very frustrating to use due to the clunky dropdown in the inspector), but that still doesn’t expose the asset’s address. AssetReference does have a “RuntimeKey” property but the documentation is not very clear on how this actually works. If we serialize the RuntimeKey into a save file, is it going to be valid the next time the player runs the game? Does the RuntimeKey change when we make a new build?

I recommend making a string field on your component and assign the address when you instantiate it. That way you have full control over what you’re saving and don’t have to worry about the intricacies of addressables or the potential slowness of looking up an address.

[EDIT] About loading from a label, if you use that label as the address it will still work. If you’re loading multiple assets from one label, you can use GetResourceLocations instead which I think you can get the keys from (but I haven’t used it so not sure).

1 Like

I think you can actually serialize the AssetReference itself. No need to get the RuntimeKey.

I ended up doing this, and writing a simple editor script to loop through all the project assets and save the Addressables address to each component/ScriptableObject; it works very well and vastly simplifies building a save system.