序列化其实就是把一个C#类的东西 序列化保存成xml 或者 二进制 格式
首先创建一个类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Serialization;
public enum gamestate
{
gamewait,
gamestart,
gameover,
}
[System.Serializable]
public class testserilize{
[XmlAttribute("id")]//这个是xml 的属性
public int ID { get; set; }
[XmlAttribute("Name")]
public string name { get; set; }
[XmlElement("List")]//xml节点名字
public List<int> list { get; set; }
[XmlElement("enu")]//xml节点名字
public gamestate game { get; set; }
[XmlElement]//xml节点名字 如果没有则会以 变量名作为节点名字
public string nam { get; set; }
}
下面是序列化和反序列化的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class resourcetest : MonoBehaviour {
// Use this for initialization
void Start () {
// xmlserilize();//xml序列化
// xmldeserilizerTest(); //测试 xml 反序列化
// binary();//二进制序列化
binaryDesrilizerTest();//测试 二进制反序列化
}
// Update is called once per frame
void Update ()
{
}
void xmldeserilizerTest()//xml测试 反序列化得到的结果
{
testserilize testserilize = xmlDesilize();
Debug.Log(testserilize.name + " " + testserilize.ID);
foreach (int a in testserilize.list)
{
Debug.Log(a);
}
gamestate shit = testserilize.game;
Debug.Log(shit);
Debug.Log(testserilize.nam);
}
void binaryDesrilizerTest()//二进制 反序列化得到的结果
{
testserilize testserilize = binaryDesirilize();
Debug.Log(testserilize.name + " " + testserilize.ID);
foreach (int a in testserilize.list)
{
Debug.Log(a);
}
gamestate shit = testserilize.game;
Debug.Log(shit);
Debug.Log(testserilize.nam);
}
void xmlserilize()//初始化 创建类
{
testserilize ts = new testserilize();
ts.ID = 100;
ts.name = "测试";
ts.list = new List<int>();
ts.list.Add(1);
ts.list.Add(2);
ts.game = gamestate.gamestart;
ts.nam = "学生";
xmlSerilize(ts);
}
void xmlSerilize(testserilize testserilize)//xml序列化
{
FileStream fs = new FileStream(Application.dataPath + "/test.xml", FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
XmlSerializer xs = new XmlSerializer(testserilize.GetType());
xs.Serialize(fs, testserilize);
sw.Close();
fs.Close();
//一下这种写法也可以 可以少写sw 写入流
//FileStream fs = new FileStream(Application.dataPath + "/test.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
//XmlSerializer xs = new XmlSerializer(testserilize.GetType());
//xs.Serialize(fs, testserilize);
//fs.Close();
}
testserilize xmlDesilize()//xml反序列化
{
FileStream fs = new FileStream(Application.dataPath + "/test.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
XmlSerializer xs = new XmlSerializer(typeof(testserilize));
testserilize testserilize = (testserilize)xs.Deserialize(fs);
fs.Close();
return testserilize;
}
void binary()
{
testserilize testserilize = new testserilize();
testserilize.ID = 200;
testserilize.name = "二进制";
testserilize.list = new List<int>();
testserilize.list.Add(2);
testserilize.list.Add(3);
testserilize.game = gamestate.gameover;
testserilize.nam = "哈哈哈";
binarySerilize(testserilize);
}
void binarySerilize(testserilize serilize)//二进制的序列化 利用BinaryFormatter这个类进行序列化和反序列化
{
FileStream fs = new FileStream(Application.dataPath + "/test.bytes", FileMode.Create, FileAccess.ReadWrite,FileShare.ReadWrite);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, serilize);
fs.Close();
}
testserilize binaryDesirilize()//二进制的反序列化
{
TextAsset testAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/test.bytes");
MemoryStream ms = new MemoryStream(testAsset.bytes);//将文本读取到这个MemoryStream流下面
BinaryFormatter bf = new BinaryFormatter();
testserilize testserilize = (testserilize)bf.Deserialize(ms);
ms.Close();
return testserilize;
}
}