Here is an example save system:
using System.IO;
using UnityEngine;
[CreateAssetMenu(fileName = "GameSave", menuName = "ScriptableObjects/GameSave", order = 1)]
public class ExampleSaveSystem : ScriptableObject
{
public float someFloat;
public GameObject prefabReference;
public ScriptableObject scriptableObjectReference;
private string GetPath()
{
return Application.persistentDataPath + "/" + name + ".json";
}
public void SaveToFile()
{
File.WriteAllText(GetPath(), JsonUtility.ToJson(this, true));
}
public void LoadFromFile()
{
if (!File.Exists(GetPath()))
{
SaveToFile();
return;
}
JsonUtility.FromJsonOverwrite(File.ReadAllText(GetPath()), this);
}
}
A prefab or ScriptableObject reference will be serialized by its instanceid. Is this a reliable way to store a reference long term? I have tested it in a build and it did work, but I’m worried that eventually the instanceids will change.