C# 在整个游戏过程中将数据保存到json?
什么是循环浏览所有活动游戏对象并每10秒保存实例数据的适当方式 理想情况下,我希望创建一个包含所有游戏对象数据的单一JSON文件,如下所示:C# 在整个游戏过程中将数据保存到json?,c#,unity3d,json.net,C#,Unity3d,Json.net,什么是循环浏览所有活动游戏对象并每10秒保存实例数据的适当方式 理想情况下,我希望创建一个包含所有游戏对象数据的单一JSON文件,如下所示: { "Instance ID" : " xxxxxx" "Time" : 10 "Attributes" : [ { "Position" : "(xx,xx) },
{
"Instance ID" : " xxxxxx"
"Time" : 10
"Attributes" : [
{ "Position" : "(xx,xx) },
{ "Speed" : "xxx" },
{ "Color" : "xxx" },
]
"Instance ID" : " xxxxxx"
"Time" : 20
"Attributes" : [
{ "Position" : "(xx,xx) },
{ "Speed" : "xxx" },
{ "Color" : "xxx" },
]
...
}
目前我可以每10秒保存一个单一游戏对象的数据。但是,包含次要游戏对象只会覆盖原始JSON文件 介绍了JsonUtility,unity文档中的JSON序列化
下面是示例json的大致轮廓:
[Serializable]
public class CustomObject
{
public int instanceID;
public float time;
public Vector2 position;
public float speed;
public string color;
}
CustomObject myObject = new CustomObject();
myObject.instanceID = 1;
myObject.time = 10.0;
myObject.position = new Vector2(0.0f, 0.0f);
myObject.speed = 23;
myObject.color = 'red';
string jsonObject = JsonUtility.ToJson(myObject);
之后,当您想再次将其放入对象中时:
anotherObject = JsonUtility.FromJson<CustomObject>(jsonObject);
anotherObject=JsonUtility.FromJson(jsonObject);
我还没有测试过它,我不知道它是否有效,但它应该足以为您指出正确的方向。可能重复的