using System; using System.IO; using System.Security; using System.Text; using UnityEngine; using SceneEditor.Proxy.Plugin; using Thousandto.Core.Base; using MonoXmlUtils = UnityEngine.Gonbest.MagicCube.MonoXmlUtils; using FFileReader = UnityEngine.Gonbest.MagicCube.FFileReader; namespace Thousandto.Code.Logic { /// /// MiniMap数据写操作 /// public class WorldMapDataSerializer { /// /// 把WorldMapInfo数据写入文件中 /// /// /// public static void WriteWorldMap(string file, WorldMapInfoData data) { SecurityElement root = new SecurityElement("WorldData"); root.AddAttribute("X",data.Size.x.ToString()); root.AddAttribute("Z", data.Size.y.ToString()); root.AddAttribute("Scale", data.WorldMapScale.ToString()); root.AddAttribute("TempData", data.TempData.ToString()); foreach (var key in data.MiniMapDict.Keys) { var map = data.MiniMapDict[key]; var se = new SecurityElement("MiniMap"); se.AddAttribute("Name", key); se.AddAttribute("X", map.Size.x.ToString()); se.AddAttribute("Z", map.Size.y.ToString()); se.AddAttribute("CX", map.CameraPosition.x.ToString()); se.AddAttribute("CZ", map.CameraPosition.y.ToString()); root.AddChild(se); } var sb = new StringBuilder(); sb.Length = 0; string CN_XML_HEAD = ""; sb.AppendLine(string.Format(CN_XML_HEAD, "1.0", "utf-8")); sb.AppendLine(root.ToString()); File.WriteAllText(file, sb.ToString()); } //从文件中读取数据 public static void ReadWorldMap(string file, Action callBack) { FFileReader.ReadTextAsync(file, text => { WorldMapInfoData result = new WorldMapInfoData(); if (!string.IsNullOrEmpty(text)) { var root = MonoXmlUtils.GetRootNodeFromString(text); if (root != null) { var sx = Convert.ToSingle(root.Attribute("X")); var sz = Convert.ToSingle(root.Attribute("Z")); var scale = Convert.ToSingle(root.Attribute("Scale")); var temp = Convert.ToSingle(root.Attribute("TempData")); result.Size = new UnityEngine.Vector2(sx, sz); result.WorldMapScale = scale; result.TempData = temp; if (root.Children != null) { foreach (SecurityElement child in root.Children) { var name = child.Attribute("Name"); if (!string.IsNullOrEmpty(name)) { var miniMap = new MiniMapInfoData(); var msx = Convert.ToSingle(child.Attribute("X")); var msz = Convert.ToSingle(child.Attribute("Z")); var mpx = Convert.ToSingle(child.Attribute("CX")); var mpz = Convert.ToSingle(child.Attribute("CZ")); miniMap.MapName = name; miniMap.Size = new UnityEngine.Vector2(msx, msz); miniMap.CameraPosition = new UnityEngine.Vector2(mpx, mpz); result.MiniMapDict[name] = miniMap; } } } } } else { UnityEngine.Debug.LogError(string.Format("read file error!!:{0}", file)); } if(callBack != null) callBack(result); }); } } }