简体   繁体   English

序列化 ScriptableObject 参考 (InstanceID)

[英]Serializing ScriptableObject Reference (InstanceID)

Let's say I have a PlayerData ScriptableObject that holds a reference to AvatarData ScriptableObject like so:假设我有一个 PlayerData ScriptableObject 包含对 AvatarData ScriptableObject 的引用,如下所示:

public class PlayerData : ScriptableObject
{
    public AvatarData avatar;
}

During gameplay player can change the avatar to another one.在游戏过程中,玩家可以将头像更改为另一个。 All AvatarData objects are saved in.asset files.所有 AvatarData 对象都保存在 .asset 文件中。 I want to save the reference to currently selected AvatarData instance and save it in json/binary file for loading back.我想保存对当前选择的 AvatarData 实例的引用并将其保存在 json/binary 文件中以供加载。

In case of objects created in memory the InstanceID will change each game session.对于在 memory 中创建的对象,InstanceID 将更改每个游戏 session。 What about instances of objects serialized in.asset files?在 .asset 文件中序列化的对象实例呢? Since the instance is saved to a file can we rely on it/be sure InstanceID won't change?由于实例已保存到文件中,我们可以依赖它/确保 InstanceID 不会更改吗? JsonUtility.ToJson will generate json with InstanceID for each object but I need to be sure I will get the same object when I call JsonUtility.FromJsonOverwrite . JsonUtility.ToJson将为每个 object 生成带有 InstanceID 的 json 但我需要确定当我调用JsonUtility.FromJsonOverwrite时我会得到相同的 object I know it won't work with objects created in memory but I think it should work with instances saved as asset files.我知道它不适用于在 memory 中创建的对象,但我认为它应该适用于保存为资产文件的实例。 I am afraid the InstanceID may change each time I build the app.恐怕每次构建应用程序时 InstanceID 都可能会发生变化。

Does anyone have experience with this?这个事情谁有经验? Is it better to create own UUID/Hashtable to reference the objects in Unity3D?创建自己的 UUID/Hashtable 来引用 Unity3D 中的对象会更好吗?

Most answers I found suggest using UnityEditor namespace but it cannot be used in builds.我发现的大多数答案都建议使用 UnityEditor 命名空间,但它不能在构建中使用。

I found this post about the issue.我发现这篇关于这个问题的帖子。 Seems we cannot rely on InstanceID in our save systems and this is the way to go:似乎我们不能在我们的保存系统中依赖 InstanceID,这是通往 go 的方法:

Create another ScriptableObject assets that holds a list of references to your ScriptableObject assets.创建另一个 ScriptableObject 资产,其中包含对您的 ScriptableObject 资产的引用列表。 In your save data, record the list index.在您的保存数据中,记录列表索引。 You can implement ISerializationCallbackReceiver and get/set the index in the callbacks.您可以实现ISerializationCallbackReceiver并在回调中获取/设置索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM