我的JSON写入器只写入了我模拟中的一个对象?

问题描述 投票:0回答:1

我正试图让我的WriteData()函数以JSON格式保存场景中多个对象的信息。然而,我面临的问题是,我的写入器只保存了构成我的地板的一个单元的初始坐标,而不是保存地板的所有坐标。

我目前有带标签的预制件,我正在寻找并将其存储到一个gameobject数组中。然后用foreach循环去查看每个游戏对象数组,得到相关信息,将其分配到我创建的可序列化类中的数据中,然后追加到JSON文件中。在这个过程中,它也是将数据保存为单独的JSON字符串,而不是一个连接的字符串。

这是我第一次使用JSON Unity的JSON Utility,所以希望得到任何帮助。

下面是我目前使用的脚本的相关部分。

using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class GameManager : MonoBehaviour
{

    //used to calculate the rate at which data is collected and written to file
    public float sampleRate = 1f;

    //name of the file when writing data
    string fileName;

    SaveData saveData;      //save data class object 

    //fields for retrieving the data about dynamic humans in the scene
    GameObject[] dynamicHumanTarget;
    string newDynamicHumanId;
    Vector3 newDynamicHumanPosition = new Vector3();
    Vector3 newDynamicHumanRotation = new Vector3();

    //fields for retrieving the data about static humans in the scene;
    GameObject[] staticHumanTarget;
    string newStaticHumanId;
    Vector3 newStaticHumanPosition = new Vector3();
    Vector3 newStaticHumanRotation = new Vector3();

    //fields for retrieving the data about static humans in the scene;
    GameObject[] staticObjectTarget;
    string newStaticObjectId;
    Vector3 newStaticObjectPosition = new Vector3();
    Vector3 newStaticObjectRotation = new Vector3();

    //fields for retrieving the data about the target in the scene;
    GameObject[] targets;
    string newTargetId;
    Vector3 newTargetPosition = new Vector3();
    Vector3 newTargetRotation = new Vector3();

    //fields for retrieving the data about the target in the scene;
    GameObject[] rooms;
    //string newWallId;
    IntVector2 newRoomCoordinates = new IntVector2();



    //called before the simulation is executed
    void Start()
    {
        fileName = "/SimulationData_" + System.DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + ".json";
        saveData = new SaveData();

        dynamicHumanTarget = GameObject.FindGameObjectsWithTag("DynamicHuman");
        staticHumanTarget = GameObject.FindGameObjectsWithTag("StaticHuman");
        staticObjectTarget = GameObject.FindGameObjectsWithTag("StaticObject");
        targets = GameObject.FindGameObjectsWithTag("Target");
        rooms = GameObject.FindGameObjectsWithTag("Cell");
    }

    //called to update the simulation 
    void Update()
    {
        GetDynamicHumanData();
        GetStaticHumanData();
        GetStaticObjectData();
        GetTargetData();
        GetWallData();
        StartWriter();
    }

    public void WriteData()
    {
        string path = Application.persistentDataPath + fileName;
        if (!File.Exists(path))
        {
            File.WriteAllText(path, "");
        }

        //robot
        saveData.robotID = robotInstance.robotID;
        saveData.robotPosition = robotInstance.transform.position;
        saveData.robotRotation = robotInstance.transform.eulerAngles;

        //dynamic humans
        saveData.dynamicHumanId = newDynamicHumanId;
        saveData.dynamicHumanPosition = newDynamicHumanPosition;
        saveData.dynamicHumanRotation = newDynamicHumanRotation;

        ///static humans
        saveData.staticHumanId = newStaticHumanId;
        saveData.staticHumanPosition = newStaticHumanPosition;
        saveData.staticHumanRotation = newStaticHumanRotation;

        //static objects
        saveData.staticObjectId = newStaticObjectId;
        saveData.staticObjectPosition = newStaticObjectPosition;
        saveData.staticObjectRotation = newStaticObjectRotation;

        //room objects
        saveData.roomCoordinates = newRoomCoordinates;


        string json = JsonUtility.ToJson(saveData);
        Debug.Log(json);
        File.AppendAllText(path, json);
    }

    void GetWallData()
    {
        foreach (GameObject room in rooms)
        {
            // newWallId = room.GetComponent<MazeWall>().id;
            newRoomCoordinates = room.GetComponent<MazeCell>().coordinates;
        }
    }

    void GetTargetData()
    {
        foreach (GameObject target in targets)
        {
            newTargetId = target.GetComponent<Target>().id;
            newTargetPosition = target.transform.position;
            newTargetRotation = target.transform.eulerAngles;
        }

    }


    void GetStaticObjectData()
    {
        foreach (GameObject staticObject in staticObjectTarget)
        {
            newStaticObjectId = staticObject.GetComponent<StaticObject>().id;
            newStaticObjectPosition = staticObject.transform.position;
            newStaticObjectRotation = staticObject.transform.eulerAngles;
        }
    }


    void GetStaticHumanData()
    {
        foreach (GameObject staticHuman in staticHumanTarget)
        {
            newStaticHumanId = staticHuman.GetComponent<StaticHuman>().id;
            newStaticHumanPosition = staticHuman.transform.position;
            newStaticHumanRotation = staticHuman.transform.eulerAngles;
        }
    }


    void GetDynamicHumanData()
    {
        foreach (GameObject dynamicHuman in dynamicHumanTarget)
        {
            newDynamicHumanId = dynamicHuman.GetComponent<DynamicHuman>().id;
            newDynamicHumanPosition = dynamicHuman.transform.position;
            newDynamicHumanRotation = dynamicHuman.transform.eulerAngles;

        }
    }

    //start the data writer and repeatedly invoke it based on the sample rate
    public void StartWriter()
    {
        InvokeRepeating("WriteData", 0, 1 / sampleRate);
    }

    //stops the data writer
    public void StopWriter()
    {
        CancelInvoke();
    }
}
    [System.Serializable]
    public class SaveData
    {
        //room positions       
        public IntVector2 roomCoordinates;

        //target position
        public string targetId;
        public Vector3 targetPosition;
        public Vector3 targetRotation;

        //static objects e.g chairs
        public string staticObjectId;
        public Vector3 staticObjectPosition;
        public Vector3 staticObjectRotation;

        //static humans
        public string staticHumanId;
        public Vector3 staticHumanPosition;
        public Vector3 staticHumanRotation;


        //dynamic humans
        public string dynamicHumanId;
        public Vector3 dynamicHumanPosition;
        public Vector3 dynamicHumanRotation;


        //robot data
        public string robotID;
        public Vector3 robotPosition;
        public Vector3 robotRotation;

    }

我的JSON文件的示例片段。

{
    "roomCoordinates": {
        "x": 2,
        "z": 2
    },
    "targetId": "",
    "targetPosition": {
        "x": 0.0,
        "y": 0.0,
        "z": 0.0
    },
    "targetRotation": {
        "x": 0.0,
        "y": 0.0,
        "z": 0.0
    },
    "staticObjectId": "Chair",
    "staticObjectPosition": {
        "x": 0.5,
        "y": 0.0,
        "z": 1.5
    },
    "staticObjectRotation": {
        "x": 0.0,
        "y": 0.0,
        "z": 0.0
    },
    "staticHumanId": "StaticHuman",
    "staticHumanPosition": {
        "x": -1.5,
        "y": 0.0,
        "z": -4.5
    },
    "staticHumanRotation": {
        "x": 0.0,
        "y": 0.0,
        "z": 0.0
    },
    "dynamicHumanId": "DynamicHuman",
    "dynamicHumanPosition": {
        "x": -1.149653673171997,
        "y": 0.10249999165534973,
        "z": 3.8513429164886476
    },
    "dynamicHumanRotation": {
        "x": 0.0,
        "y": 40.97020721435547,
        "z": 0.0
    },
    "robotID": "Robot",
    "robotPosition": {
        "x": 2.5047712326049806,
        "y": 0.07088841497898102,
        "z": -3.4777321815490724
    },
    "robotRotation": {
        "x": 359.7475891113281,
        "y": 359.37066650390627,
        "z": -0.003071203362196684
    }
}{
    "roomCoordinates": {
        "x": 2,
        "z": 2
    },
    "targetId": "",
    "targetPosition": {
        "x": 0.0,
        "y": 0.0,
        "z": 0.0
    },
    "targetRotation": {
        "x": 0.0,
        "y": 0.0,
        "z": 0.0
    },
    "staticObjectId": "Chair",
    "staticObjectPosition": {
        "x": 0.5,
        "y": 0.0,
        "z": 1.5
    },
    "staticObjectRotation": {
        "x": 0.0,
        "y": 0.0,
        "z": 0.0
    },
    "staticHumanId": "StaticHuman",
    "staticHumanPosition": {
        "x": -1.5,
        "y": 0.0,
        "z": -4.5
    },
    "staticHumanRotation": {
        "x": 0.0,
        "y": 0.0,
        "z": 0.0
    },
    "dynamicHumanId": "DynamicHuman",
    "dynamicHumanPosition": {
        "x": -0.9079896807670593,
        "y": 0.1025000512599945,
        "z": 4.286510944366455
    },
    "dynamicHumanRotation": {
        "x": 0.0,
        "y": 20.490304946899415,
        "z": 0.0
    },
    "robotID": "Robot",
    "robotPosition": {
        "x": 2.4922380447387697,
        "y": 0.07088327407836914,
        "z": -3.2451395988464357
    },
    "robotRotation": {
        "x": 359.7496032714844,
        "y": 357.0594177246094,
        "z": 359.7879638671875
    }
}
json unity3d save
1个回答
0
投票

感谢BugFinder的提示,你需要封装数组,因为json实用程序不支持数组,请看这个。

https:/answers.unity.comquestions1123326jsonutility-array-not-supported.html。

因此,您可以循环浏览您的人类、机器人、目标、对象等,并将它们添加到一个数组中,然后将该数组包裹到您的SaveData对象中。更多信息。

https:/forum.unity.comthreadsjson-utility-save-and-load-a-list.488263#post-3185216)。

© www.soinside.com 2019 - 2024. All rights reserved.