换平台确实是一个头疼的问题,本来在pc用.net的json处理数据很是顺手的,但是发布web版本后,发现他不支持。后面找了好几个开源json都不能很好的支持web,或者不能支持List等。于是我就想着自己利用xml写一个序列化与反序列化用来存储数据或者解析数据。这里我只测试了pc与web平台,移动端的没有测试。
自定义xml序列化脚本,因为可能使用到中文,所以设置utf-8编码:
using UnityEngine; using System.Collections; using System.Text; using System.IO; using System.Xml; using System.Xml.Serialization; using System; /// <summary> /// XML序列化 /// </summary> public class CustomXMLData { private static CustomXMLData xmlData; /// <summary> /// 获取本类对象 /// </summary> /// <returns></returns> public static CustomXMLData getInstance() { if (xmlData == null) { xmlData = new CustomXMLData(); } return xmlData; } /// <summary> /// 序列化--内存流 /// </summary> /// <param name="pObject">对象</param> /// <param name="t">类型</param> /// <returns></returns> public 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 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 object DeserializeObject(string pXmlizedString,Type t) { XmlSerializer xs = new XmlSerializer(t); MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString)); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8); return xs.Deserialize(memoryStream); } /// <summary> /// 字符串转字节数组 /// </summary> /// <param name="pXmlString">字符内容</param> /// <returns></returns> private 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 void SerializeObjectXML(object pObject,Type t,string XMLPath) { XmlWriterSettings ws = new XmlWriterSettings(); ws.Encoding = Encoding.UTF8; XmlWriter xmTextWriter = XmlWriter.Create(XMLPath,ws); XmlSerializer xmlFormat = new XmlSerializer(t); xmlFormat.Serialize(xmTextWriter,pObject); } }
测试脚本:
using UnityEngine; using System.Collections; public class Test : MonoBehavIoUr { // Use this for initialization void Start () { //定义Player对象 Player player = new Player("学号ID",50,26.5f); //序列化 通过内存流转为字符串,可以将字符串存于数据库中存储操作或其他操作 string serStr=CustomXMLData.getInstance().SerializeObject(player,typeof(Player)); //反序列化 将字符串解析为Player对象 Player tempPlayer = (Player)CustomXMLData.getInstance().DeserializeObject(serStr,typeof(Player)); //序列化 创建本地xml文件 CustomXMLData.getInstance().SerializeObjectXML(player,typeof(Player),Application.dataPath + "/1.xml"); //读取xml的内容 并反序列化 StartCoroutine(ReadXml(Application.dataPath + "/1.xml")); } // Update is called once per frame void Update () { } /// <summary> /// 读取xml的内容 并反序列化 /// </summary> /// <param name="xmlPath">xml文件的路径</param> /// <returns></returns> IEnumerator ReadXml(string xmlPath) { Player tempPlayer=null; WWW w = new WWW(xmlPath); yield return w; if(w.error==null) tempPlayer = (Player)CustomXMLData.getInstance().DeserializeObject(w.text,typeof(Player)); } }