using UnityEngine; using System; using System.Linq; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Converters; namespace YLBattle { public class MapParse { static public void ExportMapData(MapData data) { //找到服务器物理路径 //string serverAppPath = Request.PhysicalApplicationPath.ToString(); string serverAppPath = @"_" + data.id; //构成配置文件路径 //string con_file_path =@"" + @"Assets/Resources/Map/MapConfig" + serverAppPath + @".json"; string con_file_path =@"" + @"Assets/Resources/" + data.name + @".json"; if (!File.Exists(con_file_path)) { File.Create(con_file_path); } //把模型数据写到文件 using (StreamWriter sw = new StreamWriter(con_file_path)) { try { JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new JavaScriptDateTimeConverter()); serializer.NullValueHandling = NullValueHandling.Ignore; //构建Json.net的写入流 JsonWriter writer = new JsonTextWriter(sw); //把模型数据序列化并写入Json.net的JsonWriter流中 serializer.Serialize(writer, data); //ser.Serialize(writer, ht); writer.Close(); sw.Close(); } catch (Exception ex) { ex.Message.ToString(); } } } static public MapData ImportMapFile(string filePath) { string json = ""; TextAsset text = Resources.Load(filePath); json = text.text; if (string.IsNullOrEmpty(json)) return null; MapData map = JsonConvert.DeserializeObject(json); Debug.Log("加载地图文件:id " + map.id.ToString()); return map; // //读取json文件 // using (StreamReader sr = new StreamReader(filePath)) // { // try // { // JsonSerializer serializer = new JsonSerializer(); // serializer.Converters.Add(new JavaScriptDateTimeConverter()); // serializer.NullValueHandling = NullValueHandling.Ignore; // //构建Json.net的读取流 // JsonReader reader = new JsonTextReader(sr); // //对读取出的Json.net的reader流进行反序列化,并装载到模型中 // MapData map = serializer.Deserialize(reader); // Debug.Log("加载地图文件:id " + map.id.ToString()); // return map; // } // catch (Exception ex) // { // ex.Message.ToString(); // } //} //return null; } } }