首页 > 代码库 > c# unity 读取配置json文件
c# unity 读取配置json文件
using System; using LitJson; string configStr; void Start() { ArrayList info = LoadFile(Application.dataPath,"config1.txt"); foreach (string strs in info) { configStr += strs; } // string str = @" // { // ""Speed"" : 2 // }"; JsonData jd = JsonMapper.ToObject(configStr); speed = (int)jd["Speed"];//2 } ArrayList LoadFile(string path,string name) { StreamReader sr = null; try{ string filePath=path+"//"+name; if (File.Exists(filePath)) { sr = File.OpenText(path + "//" + name); } else { return null; } } catch(Exception e) { return null; } string line; ArrayList arrList = new ArrayList(); while((line = sr.ReadLine()) != null) { arrList.Add(line); } sr.Close(); sr.Dispose(); return arrList; }
public void ResolveJson() { string str = @" { ""Name"" : ""yusong"", ""Age"" : 26, ""Birthday"" : ""1986-11-21"", ""Thumbnail"":[ { ""Url"": ""http://xuanyusong.com"", ""Height"": 256, ""Width"": ""200"" }, { ""Url"": ""http://baidu.com"", ""Height"": 1024, ""Width"": ""500"" } ] }"; JsonData jd = JsonMapper.ToObject(str); Debug.Log("name = " + (string)jd["Name"]); Debug.Log("Age = " + (int)jd["Age"]); Debug.Log("Birthday = " + (string)jd["Birthday"]); JsonData jdItems = jd["Thumbnail"]; for (int i = 0; i < jdItems.Count; i++) { Debug.Log("URL = " + jdItems[i]["Url"]); Debug.Log("Height = " + (int)jdItems[i]["Height"]); Debug.Log("Width = " + jdItems[i]["Width"]); } }
c# unity 读取配置json文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。