简体   繁体   中英

Serializing ScriptableObject Reference (InstanceID)

Let's say I have a PlayerData ScriptableObject that holds a reference to AvatarData ScriptableObject like so:

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. I want to save the reference to currently selected AvatarData instance and save it in json/binary file for loading back.

In case of objects created in memory the InstanceID will change each game session. What about instances of objects serialized in.asset files? Since the instance is saved to a file can we rely on it/be sure InstanceID won't change? 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 . I know it won't work with objects created in memory but I think it should work with instances saved as asset files. I am afraid the InstanceID may change each time I build the app.

Does anyone have experience with this? Is it better to create own UUID/Hashtable to reference the objects in Unity3D?

Most answers I found suggest using UnityEditor namespace but it cannot be used in builds.

I found this post about the issue. Seems we cannot rely on InstanceID in our save systems and this is the way to go:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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