MapParse.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using System;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Converters;
  10. namespace YLBattle
  11. {
  12. public class MapParse
  13. {
  14. static public void ExportMapData(MapData data)
  15. {
  16. //找到服务器物理路径
  17. //string serverAppPath = Request.PhysicalApplicationPath.ToString();
  18. string serverAppPath = @"_" + data.id;
  19. //构成配置文件路径
  20. //string con_file_path =@"" + @"Assets/Resources/Map/MapConfig" + serverAppPath + @".json";
  21. string con_file_path =@"" + @"Assets/Resources/" + data.name + @".json";
  22. if (!File.Exists(con_file_path))
  23. {
  24. File.Create(con_file_path);
  25. }
  26. //把模型数据写到文件
  27. using (StreamWriter sw = new StreamWriter(con_file_path))
  28. {
  29. try
  30. {
  31. JsonSerializer serializer = new JsonSerializer();
  32. serializer.Converters.Add(new JavaScriptDateTimeConverter());
  33. serializer.NullValueHandling = NullValueHandling.Ignore;
  34. //构建Json.net的写入流
  35. JsonWriter writer = new JsonTextWriter(sw);
  36. //把模型数据序列化并写入Json.net的JsonWriter流中
  37. serializer.Serialize(writer, data);
  38. //ser.Serialize(writer, ht);
  39. writer.Close();
  40. sw.Close();
  41. }
  42. catch (Exception ex)
  43. {
  44. ex.Message.ToString();
  45. }
  46. }
  47. }
  48. static public MapData ImportMapFile(string filePath)
  49. {
  50. string json = "";
  51. TextAsset text = Resources.Load<TextAsset>(filePath);
  52. json = text.text;
  53. if (string.IsNullOrEmpty(json)) return null;
  54. MapData map = JsonConvert.DeserializeObject<MapData>(json);
  55. Debug.Log("加载地图文件:id " + map.id.ToString());
  56. return map;
  57. // //读取json文件
  58. // using (StreamReader sr = new StreamReader(filePath))
  59. // {
  60. // try
  61. // {
  62. // JsonSerializer serializer = new JsonSerializer();
  63. // serializer.Converters.Add(new JavaScriptDateTimeConverter());
  64. // serializer.NullValueHandling = NullValueHandling.Ignore;
  65. // //构建Json.net的读取流
  66. // JsonReader reader = new JsonTextReader(sr);
  67. // //对读取出的Json.net的reader流进行反序列化,并装载到模型中
  68. // MapData map = serializer.Deserialize<MapData>(reader);
  69. // Debug.Log("加载地图文件:id " + map.id.ToString());
  70. // return map;
  71. // }
  72. // catch (Exception ex)
  73. // {
  74. // ex.Message.ToString();
  75. // }
  76. //}
  77. //return null;
  78. }
  79. }
  80. }