gpt4 book ai didi

unity-game-engine - 如何序列化 ScriptableObjects?

转载 作者:行者123 更新时间:2023-12-02 10:40:06 29 4
gpt4 key购买 nike

我正在使用一个我不完全理解但运行良好的保存系统。它保存了可序列化的代码,但我开始使用 ScriptableObjects 作为保存库存的方法,但它损坏了,我现在不知道如何修复它。

这是我正在使用的代码:

[CreateAssetMenu(fileName = "Attack")]
public class PlayerAttckCard : ScriptableObject
{
public string AtackName,bookName;
public bool multiTarget;
public float Bacepower;
public GameObject miniGame;
}

我尝试使用[System.Serialized]但我收到了这个错误:

SerializationException: Type UnityEngine.ScriptableObject in assembly UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.

我是否需要更改保存游戏的方式,或者有办法解决此问题吗?

最佳答案

序列化为 JSON?您可以使用 JsonUtility 来完成此操作。

https://docs.unity3d.com/Manual/JSONSerialization.html

public class TestObject : ScriptableObject
{
public string foo, bar;
}

var obj = ScriptableObject.CreateInstance<TestObject>();
obj.foo = "hello";
obj.bar = "world";
var json = JsonUtility.ToJson(obj);
Debug.Log(json); // {"foo":"hello","bar":"world"}

obj.foo = "something";
obj.bar = "else";
JsonUtility.FromJsonOverwrite(json, obj);
Debug.Log(obj.foo + ", " + obj.bar); // hello, world

关于unity-game-engine - 如何序列化 ScriptableObjects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52377939/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com