C# xml序列化 二进制序列化 unity中的asset序列化
程序员文章站
2022-04-03 12:29:15
...
序列化是将对象保存为xml或json或二进制文件;
反序列化则是读取文件信息,还原为对象;
先建一个存储数据的类,添加标签表示可序列化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Serialization;
[System.Serializable]
public class XmlTest
{
[XmlAttribute("Id")]
public int Id { get; set; }
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlElement("List")]
public List<int> List { get; set; }
}
xml序列化和二进制序列化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class XmlSerialize : MonoBehaviour
{
void Start()
{
XmlTest test = new XmlTest();
test.Id = 11;
test.Name = "xiaolin";
test.List = new List<int>();
test.List.Add(33);
test.List.Add(44);
test.List.Add(55);
test.List.Add(66);
//写入x.xml
//XmlSeri(test);
//读取x.xml
//XmlTest t = XmlDeSeri();
//Debug.Log(t.Id);
//Debug.Log(t.Name);
//foreach (var item in t.List)
//{
// Debug.Log(item);
//}
//写入binary.bytes
//BinarySeri(test);
//读取binary.bytes
//BinaryDeSeri();
//XmlTest t = BinaryDeSeri();
//Debug.Log(t.Id);
//Debug.Log(t.Name);
//foreach (var item in t.List)
//{
// Debug.Log(item);
//}
}
/// <summary>
/// 类转xml
/// </summary>
private void XmlSeri(XmlTest test)
{
FileStream fs = new FileStream(Application.dataPath + "/x.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
XmlSerializer xs = new XmlSerializer(test.GetType());
xs.Serialize(sw, test);
sw.Close();
fs.Close();
}
/// <summary>
/// xml转类
/// </summary>
private XmlTest XmlDeSeri()
{
FileStream fs = new FileStream(Application.dataPath + "/x.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
XmlSerializer xs = new XmlSerializer(typeof(XmlTest));
XmlTest test = (XmlTest)xs.Deserialize(fs);
fs.Close();
return test;
}
/// <summary>
/// 类转二进制
/// </summary>
private void BinarySeri(XmlTest test)
{
FileStream fs = new FileStream(Application.dataPath + "/binary.bytes", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, test);
fs.Close();
}
/// <summary>
/// 二进制转类
/// </summary>
private XmlTest BinaryDeSeri()
{
TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/binary.bytes");
MemoryStream ms = new MemoryStream(textAsset.bytes);
BinaryFormatter bf = new BinaryFormatter();
XmlTest test = (XmlTest)bf.Deserialize(ms);
ms.Close();
return test;
}
}
unity中的asset序列化
先新建一个存储数据的类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "assets", menuName = "创建asset", order = 1)]
public class TestAsset : ScriptableObject
{
public int Id;
public int Name;
public List<int> List;
}
在unity中右键可以创建一个asset,然后在asset上输入存储的数据,这样就存起来了。代码读取数据如下:
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class XmlSerialize : MonoBehaviour
{
void Start()
{
//读取asset
ReadTestAsset();
}
/// <summary>
/// 读取unity中特有的asset
/// </summary>
private void ReadTestAsset()
{
TestAsset testAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TestAsset>("Assets/Scenes/序列化/assets.asset");
Debug.Log(testAsset.Id);
Debug.Log(testAsset.Name);
foreach (var item in testAsset.List)
{
Debug.Log(item);
}
}
}
上一篇: Unity 序列化问题
下一篇: Unity序列化