主要是用Json文件保存场景内物体的位置名称等信息,并且用的时候需要读取。
1、创建需要保存的类
public class EachModel { public string ModelName; public string FileSize; public string FileName; public string PicName; public string[] Pos; public string[] Roate; public string[] Scale; public EachModel() { Pos = new string[3]; Roate = new string[4]; Scale = new string[3]; } public EachModel(string ModelName, string FileName, string PicName, string[] Pos, string[] Roate, string[] Scale) { this.ModelName = ModelName; this.FileName = FileName; this.PicName = PicName; for (int i = 0; i < Pos.Length; i++) { this.Pos[i] = Pos[i]; this.Roate[i] = Roate[i]; this.Scale[i] = Scale[i]; } this.Roate[3] = Roate[3]; } }
以及类的集合
public class ModelList { public List<EachModel> sites = new List<EachModel>(); }
将site内容转化成Json文件写入
private void SaveModels(Transform model) { print(configPath); //string filePath = Application.streamingAssetsPath + @"/Config/JsonModel.json"; ModelList r = new ModelList(); EachModel myModel = new EachModel(); myModel.Pos = new string[] { model.position.x.ToString("f2"), model.position.y.ToString("f2"), model.position.z.ToString("f2") }; myModel.Roate = new string[] { model.rotation.x.ToString("f2"), model.rotation.y.ToString("f2"), model.rotation.z.ToString("f2"), model.rotation.w.ToString("f2") }; myModel.Scale = new string[] { model.localScale.x.ToString("f2"), model.localScale.y.ToString("f2"), model.localScale.z.ToString("f2") }; myModel.ModelName = modelName.text; myModel.FileName = fileName.text; DirectoryInfo di = new DirectoryInfo(currentPicUrl); string[] str = di.Name.Split(‘.‘); myModel.PicName = str[0]; myModel.FileSize = filebig.text; //判断config文件是否存在,如果存在就读取Json里的内容来更新site if (File.Exists(configPath)) { StreamReader streamreader = new StreamReader(Application.streamingAssetsPath + "/Config/JsonModel.json");//读取数据,转换成数据流 JsonReader js = new JsonReader(streamreader);//再转换成json数据 r = JsonMapper.ToObject<ModelList>(js);//读取 if (IsAdd) { r.sites.Add(myModel); } else { for (int i = 0; i < r.sites.Count; i++) { if (r.sites[i].FileName.Equals(fileName.text)) { r.sites[i] = myModel; break; } } } streamreader.Close(); streamreader.Dispose(); } else { r.sites.Add(myModel); } //找到当前路径 FileInfo file = new FileInfo(configPath); //判断有没有文件,有则打开文件,,没有创建后打开文件 StreamWriter sw = file.CreateText(); //ToJson接口将你的列表类传进去,,并自动转换为string类型 string json = JsonMapper.ToJson(r);
//避免中文字符乱码 Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})"); json = reg.Replace(json, delegate (Match m) { return ((char)System.Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); }); //将转换好的字符串存进文件, sw.WriteLine(json); //注意释放资源 sw.Close(); sw.Dispose(); }
输出的Json文件:
{"sites":[{"ModelName":"茶壶","FileSize":"264.1Kb","FileName":"茶壶.FBX","PicName":"建筑方块","Pos":["4.00","0.00","10.00"],"Roate":["0.00","0.00","0.00","1.00"],"Scale":["1.00","1.00","1.00"]},{"ModelName":"称重123","FileSize":"362.0Kb","FileName":"称重.FBX","PicName":"机甲底盘","Pos":["4.00","0.00","10.00"],"Roate":["0.00","0.00","0.00","1.00"],"Scale":["1.20","1.20","1.20"]}]}
写入Json的时候已经有读取Json的操作了,如果只是读取:
private ModelList modelDatalList = new ModelList(); private void UpdateData() { string path = Application.streamingAssetsPath + "/Config/JsonModel.json"; if (!File.Exists(path)) { return; } StreamReader streamreader = new StreamReader(Application.streamingAssetsPath + "/Config/JsonModel.json");//读取数据,转换成数据流 JsonReader js = new JsonReader(streamreader);//再转换成json数据 modelDatalList = JsonMapper.ToObject<ModelList>(js);//读取 streamreader.Close(); streamreader.Dispose(); }
把Json内容转化成ModelList类,输出modelDatalList.sites即可
原文:https://www.cnblogs.com/chenxiya/p/14208055.html