json - 如何将场景中的游戏对象位置写入 json 文件?
问题描述
单击按钮时,我正在加载函数 WriteJsonForLevel()。我已经放置了三个标签名为“RedCoin”的游戏对象,我想将游戏对象的位置写入 JSON 文件。我可以得到对象的位置,但它都被覆盖了。我只能看到最后一个 GameObject 位置(即循环完成)
public List<GameObject> levelObjects;
public string level;
public Vector3 pos;
// Start is called before the first frame update
void Start()
{
levelObjects = new List<GameObject>();
}
// Update is called once per frame
void Update()
{
}
public void WritejsonForAll()
{
WriteJsonForLevel();
}
public void WriteJsonForLevel()
{
/* FileStream fs = new FileStream(Application.dataPath + "/sample.json",FileMode.Create);
StreamWriter writer= new StreamWriter(fs);*/
GameObject[] coinObjRed = GameObject.FindGameObjectsWithTag("RedCoin");
putAllObjectInList(coinObjRed);
}
public void putAllObjectInList(GameObject[] p)
{
string path = Application.dataPath + "/text.json";
foreach (GameObject q in p)
{
levelObjects.Add(q);
}
for (int i = 0; i < levelObjects.Count; i++)
{
GameObject lvlObj = levelObjects[i];
Vector3 pos = lvlObj.transform.position;
string posOutput = JsonUtility.ToJson(pos);
File.WriteAllText(path,posOutput);
Debug.Log("position:" + posOutput);
}
}
}
解决方案
您正在使用WriteAllText
它将在每次调用时覆盖文件。由于它每次在循环中都会覆盖,它只会将最后一个对象写入文件,因为其他之前的写入都会被覆盖。我会考虑制作一个序列化的数据类,将数据分配给它,将其转换为 JSON 字符串,然后保存。
// stores individual locations for saving
[System.Serializable]
public class IndividualLocation
{
public IndividualLocation(Vector3 pos)
{
xPos = pos.x;
yPos = pos.y;
zPos = pos.z;
}
public float xPos;
public float yPos;
public float zPos;
}
// stores all game locations for saving
[System.Serializable]
public class AllGameLocations
{
public List<IndividualLocation> Locations = new List<IndividualLocation>();
}
public void PutAllObjectInList(in GameObject[] p)
{
string path = Application.dataPath + "/text.json";
// create a new object to write to
AllGameLocations data = new AllGameLocations();
// iterate the objects adding each to our structure
foreach(GameObject obj in p)
{
data.Locations.Add(new IndividualLocation(obj.transform.position));
}
// now that the data is filled, write out to the file
File.WriteAllText(path, JsonUtility.ToJson(AllGameLocations));
}
如果您需要有关如何正确加载数据的片段,我可以添加一个。
编辑:这是一个加载片段
public void LoadJSONObject()
{
string path = Application.dataPath + "/text.json";
// if the file path or name does not exist
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Debug.LogWarning("File or path does not exist! " + path);
return
}
// load in the save data as byte array
byte[] jsonDataAsBytes = null;
try
{
jsonDataAsBytes = File.ReadAllBytes(path);
Debug.Log("<color=green>Loaded all data from: </color>" + path);
}
catch (Exception e)
{
Debug.LogWarning("Failed to load data from: " + path);
Debug.LogWarning("Error: " + e.Message);
return;
}
if (jsonDataAsBytes == null)
return;
// convert the byte array to json
string jsonData;
// convert the byte array to json
jsonData = Encoding.ASCII.GetString(jsonDataAsBytes);
// convert to the specified object type
AllGameLocations returnedData;
JsonUtility.FromJsonOverwrite<AllGameLocations>(jsonData, AllGameLocations);
// use returnedData as a normal object now
float firstObjectX = returnedData.Locations[0].xPos;
}
}
让我知道负载是否有效,只需输入未经测试即可。还添加了一些错误处理以确保数据存在并且加载正常工作。
推荐阅读
- flutter - 如何在 Flutter 中将 Container 上的涟漪效果显示为 IconButton 子项?
- vue.js - vuex 未知动作类型:events/loadAll
- python - 从数据框到桌面中的 Excel
- html - CSS onclick 动画
- core-data - SwiftUI 将 Core Data 实体读入 []
- python - 使用多个字典过滤数据框列
- matplotlib - 使用 matplotlib 时需要关于刻度格式的帮助
- ansible - 当条件不相等时,Ansible 失败!= 不适用于我的游戏
- vue.js - 获取的 Vuex 商店项目抛出未定义,然后呈现正常
- c - printf ("%d", 0.0625) 打印一个零,而附近的值(例如 0.0624)打印出你所期望的荒谬数字,为什么 0.0625 不同?