序列化 ScriptableObject 引用 (InstanceID)

Serializing ScriptableObject Reference (InstanceID)

假设我有一个 PlayerData ScriptableObject,它包含对 AvatarData ScriptableObject 的引用,如下所示:

public class PlayerData : ScriptableObject
{
    public AvatarData avatar;
}

在游戏过程中玩家可以更换头像。所有 AvatarData 对象都保存在 .asset 文件中。我想保存对当前选定的 AvatarData 实例的引用并将其保存在 json/binary 文件中以供加载。

对于在内存中创建的对象,InstanceID 将在每个游戏会话中发生变化。在 .asset 文件中序列化的对象实例呢?由于实例已保存到文件中,我们可以 it/be 确定 InstanceID 不会更改吗? JsonUtility.ToJson 将为每个对象生成带有 InstanceID 的 json,但我需要确保在调用 JsonUtility.FromJsonOverwrite 时我将获得相同的对象。我知道它不适用于在内存中创建的对象,但我认为它应该适用于保存为资产文件的实例。恐怕每次构建应用程序时 InstanceID 都可能会发生变化。

有没有人有这方面的经验?创建自己的 UUID/Hashtable 来引用 Unity3D 中的对象是否更好?

我找到的大多数答案都建议使用 UnityEditor 命名空间,但不能在构建中使用它。

我发现 this post 关于这个问题。似乎我们不能在我们的保存系统中依赖 InstanceID,这是要走的路:

Create another ScriptableObject assets that holds a list of references to your ScriptableObject assets. In your save data, record the list index. You can implement ISerializationCallbackReceiver and get/set the index in the callbacks.