【问题标题】:My JSON writer is only writing one object from my simulation?我的 JSON 编写器只从我的模拟中写入一个对象?
【发布时间】:2020-04-22 23:22:25
【问题描述】:

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

我目前有带有标签的预制件,我正在查找这些预制件并将其存储到一个游戏对象数组中。然后使用 foreach 循环遍历每个游戏对象数组以获取相关信息以将其分配给我创建的可序列化类中的数据,然后将其附加到 JSON 文件中。在此过程中,它还将数据保存为单独的 JSON 字符串,而不是串联的字符串。

这是我第一次使用 JSON / Unity 的 JSON 实用程序,因此我们将不胜感激。

在我目前用于执行此操作的脚本的相关部分下方。

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
    }
}

【问题讨论】:

  • 我不明白,因为您在静态人类、动态人类、stattkc 对象等每个对象中只有 1 个项目,您怎么会期望每个对象不止一个?
  • 在我的场景中,我正在实例化 2 个静态人类、2 个动态人类、2 个椅子等。这些都有与之关联的标签。所以我尝试使用 Unity;s FindObjectsWithTag() 函数来收集它们并将它们存储到一个 GameObject 数组中。
  • 但是您的保存数据结构每个只有 1 个,例如 public string staticObjectId; public Vector3 staticObjectPosition; public Vector3 staticObjectRotation; 所以 1 个项目只能有 1 个 id、1 个位置和 1 个旋转..
  • 哦,对了,我明白你在说什么。我将如何更改保存数据结构以保存多个对象的信息。有没有办法使用 Unity 的 JSON 实用程序来做到这一点。

标签: json unity3d save


【解决方案1】:

感谢 BugFinder 的提示,需要对数组进行包装,因为 json 实用程序不支持数组,请参见:

https://answers.unity.com/questions/1123326/jsonutility-array-not-supported.html

因此,您可以遍历您的人类、机器人、目标、对象等,并将它们添加到一个 n 数组中,然后将该数组包装到您的 SaveData 对象中。更多信息;

https://forum.unity.com/threads/json-utility-save-and-load-a-list.488263/#post-3185216

【讨论】:

  • 谢谢,如有任何问题,我会调查并回复。
  • 在您提供的第一个链接中,您能否解释一下代码' void ParseJsonToObject(string json) { var WrappedjsonArray = JsonUtility.FromJson(json); } ' 正在做/你打算如何将它用于我想做的事情。
  • 还有统一的新 json 包,我认为是 newtonsoft 包,它确实支持数组
  • 感谢您的帮助。是的,当我使用 newtonsoft json.net 时。我设法弄清楚该怎么做。
猜你喜欢
  • 1970-01-01
  • 2010-10-01
  • 2021-10-26
  • 2017-03-08
  • 1970-01-01
  • 2013-02-28
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多