102 lines
4.2 KiB
C#
102 lines
4.2 KiB
C#
|
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
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// MiniMap数据写操作
|
|||
|
/// </summary>
|
|||
|
public class WorldMapDataSerializer
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 把WorldMapInfo数据写入文件中
|
|||
|
/// </summary>
|
|||
|
/// <param name="file"></param>
|
|||
|
/// <param name="data"></param>
|
|||
|
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 = "<?xml version=\"{0}\" encoding=\"{1}\"?>";
|
|||
|
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<WorldMapInfoData> 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);
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|