Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在整个游戏过程中将数据保存到json?_C#_Unity3d_Json.net - Fatal编程技术网

C# 在整个游戏过程中将数据保存到json?

C# 在整个游戏过程中将数据保存到json?,c#,unity3d,json.net,C#,Unity3d,Json.net,什么是循环浏览所有活动游戏对象并每10秒保存实例数据的适当方式 理想情况下,我希望创建一个包含所有游戏对象数据的单一JSON文件,如下所示: { "Instance ID" : " xxxxxx" "Time" : 10 "Attributes" : [ { "Position" : "(xx,xx) },

什么是循环浏览所有活动游戏对象并每10秒保存实例数据的适当方式

理想情况下,我希望创建一个包含所有游戏对象数据的单一JSON文件,如下所示:

            {
                "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);
我还没有测试过它,我不知道它是否有效,但它应该足以为您指出正确的方向。

可能重复的