不確定標題是否完全正確-我一直在對此進行一些研究,但找不到一個好的答案。我已經看到了幾個相關的問題,但我無法確定哪種解決方案實際上會有所幫助以及如何實施它。
我正在嘗試匯入以不同語言生成的 Json 檔案,并試圖找出一種有效讀取資料的方法。這是一個類似于我的世界的世界保存系統,使用一組塊和塊。每個塊位置都是一個 int,并為其分配了一個 ID。該檔案看起來像這樣(縮短):
[
{
"anchor": [
-128,
0,
-128
],
"blocks": {
},
"id": 0
},
{
"anchor": [
48,
0,
-112
],
"blocks": {
"1037": 10,
"1038": 10,
"1053": 10,
"1054": 10,
"1069": 10,
"1070": 10,
"1071": 10,
},
"id": 27
}
]
我可以將錨點拉為整數陣列,將 id 拉為獨立的整數,但我無法拉出塊串列 - 匯入此資料的最佳方法是什么?
這是我之前使用的類,在意識到字典不能與 jsonUtility 一起使用之前:
public class Chunk
{
public int[] anchor;
public Dictionary<int, int> blocks;
public int id;
public Chunk(Vector3Int location, int newid)
{
blocks = new Dictionary<int, int>();
id = newid;
anchor = new int[] {location.x, location.z, location.y};
}
}
我也嘗試過使用通用物件串列的版本,但無濟于事。
以及作為準系統 json 匯入的匯入程序:
string import = File.ReadAllText(saveDataLocation);
Chunk islandImport = JsonUtility.FromJson<Chunk>(import);
uj5u.com熱心網友回復:
以下使用 Newtonsoft.Json 作業:
public class Chunk
{
public int[] anchor;
public Dictionary<int, int> blocks;
public int id;
}
進而:
var chunks = Newtonsoft.Json.JsonConvert.DeserializeObject<Chunk[]>(json);
(我不確定這將如何轉化為 Unity。)
uj5u.com熱心網友回復:
將塊型別宣告為 JObject 或動態。所以在反序列化時你不會看到任何問題。
再宣告一個 redyonly 屬性以回傳 blocks 屬性的實際值。
uj5u.com熱心網友回復:
試試這個
public class Model
{
public List<int> anchor { get; set; }
public Dictionary<string, int> blocks { get; set; }
public int id { get; set; }
}
并將其與
JsonConvert.DeserializeObject<List<Model>>(myJson);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461606.html
上一篇:您如何在Unity中進行協作?
下一篇:Unity輸入不作業,如何解決?
