【发布时间】: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 实用程序来做到这一点。