Unity3D ScriptableObject序列化

huangapple go评论92阅读模式
英文:

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.

&quot;boughtSpells&quot;:[{&quot;instanceID&quot;:13864},{&quot;instanceID&quot;:13421}]

And here's some code.

[CreateAssetMenu(fileName = &quot;New Spell Pattern&quot;, menuName = &quot;SpellPattern&quot;)]
[System.Serializable]
public class SpellPattern : ScriptableObject {
	public string spellName;

	[TextArea]
	public string spellDescription;
}

Game data model.

[Serializable]
public class GameData
{
    public List&lt;SpellPattern&gt; 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).

huangapple
  • 本文由 发表于 2020年1月4日 11:53:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定