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 = 0i <numbers.Counti++)
{
jsonwriter.WritePropertyName("Number" + (i + 1))
jsonwriter.WriteObjectStart()//1-1
type = new List<Transform>()
foreach (Transform child in numbers[i].transform)
type.Add(child)
jsonwriter.WritePropertyName("TypeCount")jsonwriter.Write(type.Count)
for (int j = 0j <type.Countj++)
{
jsonwriter.WritePropertyName("Type" + (j + 1))
jsonwriter.WriteObjectStart()//1-1-1
pos = new List<Transform>()
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 = 0k <pos.Countk++)
{
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 不一样
这种个人认为其实都是文本文件,只不过格式不一样了。很遗憾配置文件我用得很低端,我自己用的大多就是文件流(简单粗暴)。对于INI,我似乎只能给出以下方法:
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath)
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath)
//读写示例方法
void demo()
{
StringBuilder demos = new StringBuilder (255)
WritePrivateProfileString ("section", "name", "theDemo", @"D:\theDemoini.ini")
GetPrivateProfileString ("section" , "name" ,"" ,demos,255 , @"D:\theDemoini.ini")
print (demos)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)