英文:
Unity3D ScriptableObject serialization
问题
我遇到了保存游戏会话的问题。以下是从保存的数据中提取的一些JSON片段。
"boughtSpells":[{"instanceID":13864},{"instanceID":13421}]
这里还有一些代码。
[CreateAssetMenu(fileName = "New Spell Pattern", menuName = "SpellPattern")]
[System.Serializable]
public class SpellPattern : ScriptableObject {
public string spellName;
[TextArea]
public string spellDescription;
}
游戏数据模型。
[Serializable]
public class GameData
{
public List<SpellPattern> boughtSpells;
}
我认为它保存了Unity的Scriptable Object ID的引用(它是唯一的,但不是持久的),而不是将其中的数据序列化。我需要正确序列化数据,但我不知道如何在Unity的Scriptable Objects中实现,但同时我又不想改变这些Scriptable Objects的架构。
英文:
I have a problem with saving a game session. Here's some JSON fragment from the saved data.
"boughtSpells":[{"instanceID":13864},{"instanceID":13421}]
And here's some code.
[CreateAssetMenu(fileName = "New Spell Pattern", menuName = "SpellPattern")]
[System.Serializable]
public class SpellPattern : ScriptableObject {
public string spellName;
[TextArea]
public string spellDescription;
}
Game data model.
[Serializable]
public class GameData
{
public List<SpellPattern> boughtSpells;
}
I think it saves a reference of Unity's Scriptable Object ID(which is unique, but not persistent), rather than serializing the data in it.
I need to serialize the data correctly, but I have no idea how to make it with Unity's Scriptable Objects, but at the same time I don't want to change the architecture of these Scriptable Objects.
答案1
得分: 1
脚本化对象不应在运行时编辑。Scriptable Objects 的目的是:
- 用于在编辑器会话期间保存和存储数据,例如库存系统。
- 用于由各种系统共享的资源。
如果需要在运行时保存数据模型,我建议您使用结构体(Structs)。
如果需要保存游戏状态/会话,您可以使用PlayerPrefs,如果要保存的数据不是普通的整数或字符串,则应将其序列化为字节文件,使用BinaryFormatter并将其保存到内存中(可能是Application.PersistantDatapath)。
英文:
Scriptable objects are not meant to be edited at run time. The purpose of Scriptable Objects is:
-for saving and storying the data during an editor session e.g. an Inventory system -for having an asset which is shared by various systems
For saving data models at run time I recommend you to use Structs.
In case you need to save game state/session you can use PlayerPrefs and if the data you want to save is not plain int or string, you should serialise it in form of byte files using Binaryformatter and save it to the memory(Application.PersitentDatapath maybe).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论