- 山地→林地: 场景/AB/数据库/SAMap/本地化/旧想定XML全部改名 - 想定编辑: 深拷贝selectRecord防止引用污染, 设置地形下拉框匹配原文件 - 想定下发: 深拷贝后发送独立副本 - XmlHelper: FileStream改using, 新增字符串反序列化重载 - RecordDetailPanel: OnStart重新订阅LanguageChanged, 完整翻译标题/用时/视频/操作记录 - AppraiseWindowBase: OnLanguageChanged重新翻译标题+操作记录 - LocalizeOperateMsg: 旧消息回退Get()翻译 - 分组提示: ShowSetGroupError用Get()翻译, 新增4条分组错误翻译 - 学员端地形名: SetFormatted+DisplayName实时翻译 - 丘陵: 重新生成地图含道路材质, 重建AB
107 lines
3.3 KiB
C#
107 lines
3.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Xml;
|
|
using System.Xml.Serialization;
|
|
using UnityEngine;
|
|
|
|
public static class XmlHelper
|
|
{
|
|
/// <summary>
|
|
/// 序列化--内存流
|
|
/// </summary>
|
|
/// <param name="pObject">对象</param>
|
|
/// <param name="t">类型</param>
|
|
/// <returns></returns>
|
|
public static string SerializeObject(object pObject, Type t)
|
|
{
|
|
string XmlizedString = null;
|
|
MemoryStream memoryStream = new MemoryStream();
|
|
XmlSerializer xs = new XmlSerializer(t);
|
|
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
|
|
xs.Serialize(xmlTextWriter, pObject);
|
|
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
|
|
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
|
|
//Debug.Log("" + XmlizedString);
|
|
return XmlizedString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 字节转string
|
|
/// </summary>
|
|
/// <param name="characters">字节数组</param>
|
|
/// <returns></returns>
|
|
private static string UTF8ByteArrayToString(byte[] characters)
|
|
{
|
|
UTF8Encoding encoding = new UTF8Encoding();
|
|
string constructedString = encoding.GetString(characters);
|
|
return (constructedString);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 反序列化
|
|
/// </summary>
|
|
/// <param name="pXmlizedString">string内容</param>
|
|
/// <param name="t">类型</param>
|
|
/// <returns></returns>
|
|
public static object DeserializeObject(string filePath, Type t)
|
|
{
|
|
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
|
{
|
|
XmlSerializer xs = new XmlSerializer(t);
|
|
return xs.Deserialize(fileStream);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从XML字符串反序列化对象
|
|
/// </summary>
|
|
public static object DeserializeObject(string xmlString, Type t, bool fromString)
|
|
{
|
|
using (MemoryStream memoryStream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(xmlString)))
|
|
{
|
|
XmlSerializer xs = new XmlSerializer(t);
|
|
return xs.Deserialize(memoryStream);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 字符串转字节数组
|
|
/// </summary>
|
|
/// <param name="pXmlString">字符内容</param>
|
|
/// <returns></returns>
|
|
private static byte[] StringToUTF8ByteArray(string pXmlString)
|
|
{
|
|
UTF8Encoding encoding = new UTF8Encoding();
|
|
byte[] byteArray = encoding.GetBytes(pXmlString);
|
|
return byteArray;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 序列化--XML文件
|
|
/// </summary>
|
|
/// <param name="pObject">对象</param>
|
|
/// <param name="t">类型</param>
|
|
/// <param name="XMLPath">生成的xml路径</param>
|
|
public static bool SerializeObjectXML(object pObject, Type t, string XMLPath)
|
|
{
|
|
XmlWriterSettings ws = new XmlWriterSettings();
|
|
ws.Encoding = Encoding.UTF8;
|
|
try
|
|
{
|
|
XmlWriter xmTextWriter = XmlWriter.Create(XMLPath, ws);
|
|
XmlSerializer xmlFormat = new XmlSerializer(t);
|
|
xmlFormat.Serialize(xmTextWriter, pObject);
|
|
xmTextWriter.Close();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.Log(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
}
|