方法一 直接传结构体与保持文件名字
public void WriteJson(object ob, string jsonName)
{
string json = LitJson.JsonMapper.ToJson(ob);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json);
string filePath = Application.dataPath + "/Resources/" + jsonName + ".json";
using (System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
stream.Write(bytes, 0, bytes.Length);
}
UnityEditor.AssetDatabase.Refresh();//属性Asset,生成完成后立刻可以看到文件
}
方法二 jsonwriter 半编译写法
void CreateJson()
{
string path = Application.dataPath + "/Resources/Jsons/" + objBase.transform.name + ".json";
FileInfo fileInfo = new FileInfo(path);
StreamWriter sw = fileInfo.CreateText();
StringBuilder sb = new StringBuilder();
JsonWriter jsonwriter = new JsonWriter(sb);
jsonwriter.WriteObjectStart();//1
for (int i = 0; i < numbers.Count; i++)
{
jsonwriter.WritePropertyName("Number" + (i + 1));
jsonwriter.WriteObjectStart();//1-1
type = new List
foreach (Transform child in numbers[i].transform)
type.Add(child);
jsonwriter.WritePropertyName("TypeCount"); jsonwriter.Write(type.Count);
for (int j = 0; j < type.Count; j++)
{
jsonwriter.WritePropertyName("Type" + (j + 1));
jsonwriter.WriteObjectStart();//1-1-1
pos = new List
foreach (Transform child in type[j].transform)
pos.Add(child);
jsonwriter.WritePropertyName("Size"); jsonwriter.Write(pos[0].transform.localScale.x.ToString());
jsonwriter.WritePropertyName("Pos");
jsonwriter.WriteArrayStart();
for (int k = 0; k < pos.Count; k++)
{
jsonwriter.Write("(" + pos[k].transform.localPosition.x + "," + pos[k].transform.localPosition.y + ")");
}
jsonwriter.WriteArrayEnd();
jsonwriter.WriteObjectEnd();//1-1-1
}
jsonwriter.WriteObjectEnd();//1-1
}
jsonwriter.WriteObjectEnd();//1
sw.WriteLine(sb.ToString());
sw.Close();
AssetDatabase.Refresh();
}
Json 里面的数据是double 类型,使用float 会出问题
double.Parse float .Parse 不一样