Saving and Loading Reference to Material/Asset (using Addressables?)

I need to be able to save and load a reference to a material to/from a file at runtime (not just in Editor, so can’t use AssetDatabase).

This could be done by creating a list of all materials and saving the index of that material to a file, then later reading that index and finding the material at that index. However, this method would be problematic if a material was ever removed or inserted into the list because indices for each material would change.

From my understanding, Addressables assigns each Asset (that’s marked as addressable) a unique GUID stored in an AssetReference that stays the same when renaming the asset, moving the asset to a different file location, etc. This would be ideal to use for saving/loading references to materials (or other assets).

I understand that addressable assets can be loaded using methods like:

public AssetReferenceT<Material> materialAssetRef;

private void Start() {
    Material material;
    // load using string address
    material = Addressables.LoadAssetAsync<Material>("MaterialAddress").WaitForCompletion();
    // load using AssetReference (assigned in Editor)
    material = materialAssetRef.LoadAssetAsync().WaitForCompletion();
}

But how can I get an AssetReference (that contains the GUID) from the material to save in a file?

public void Save(Material material) {
    // how can I get an AssetReference or GUID from this Material?
    AssetReferenceT<Material> materialAssetRef = ???;
    WriteToFile(materialAssetRef.AssetGUID);
}

public Material Load(string guidFromFile) {
    AssetReferenceT<Material> materialAssetRef = new AssetReferenceT<Material>(guidFromFile);
    return Addressables.LoadAssetAsync<Material>(materialAssetRef).WaitForCompletion();
}

Addressables are a huge heavy hairy lift.

I would just proxy serialize them by name:

  1. make ALL your materials uniquely-named
  2. put them under a Resources/AllMaterials folder
  3. serialize / save them by name
  4. load them by name.

var mtl = Resources.Load<Material>( savedMaterialName);

Make sure to have a fallback if it isn’t found.

Beyond that, here’s some general notes:

Load/Save steps:

An excellent discussion of loading/saving in Unity3D by Xarbrough:

Loading/Saving ScriptableObjects by a proxy identifier such as name:

When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object.

Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.

If you want to use PlayerPrefs to save your game, it’s always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

Save the asset guid from the AssetReference as string to json file.
When you want to reload it, you load in the json and then you can use the constructor of AssetReference and pass in the asset guid.
use that to load the actual asset again.

1 Like