小鸟游星野
小鸟游星野
Published on 2025-05-01 / 14 Visits
0
0

Unity二进制C#自带的序列化与反序列化方法

序列化

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class Lesson5 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #region 知识点一 序列化类对象第一步—申明类对象
        //注意:如果要使用C#自带的序列化2进制方法
        //申明类时需要添加[System.Serializable]特性
        #endregion

        #region 知识点二 序列化类对象第二步—将对象进行2进制序列化

        Person p = new Person();
        //方法一:使用内存流得到2进制字节数组
        //主要用于得到字节数组 可以用于网络传输
        //新知识点
        //1.内存流对象
        //类名:MemoryStream
        //命名空间:System.IO
        //2.2进制格式化对象
        //类名:BinaryFormatter
        //命名空间:System.Runtime.Serialization.Formatters.Binary、
        //主要方法:序列化方法 Serialize
        using (MemoryStream ms = new MemoryStream())
        {
            //2进制格式化程序
            BinaryFormatter bf = new BinaryFormatter();
            //序列化对象 生成2进制字节数组 写入到内存流当中
            bf.Serialize(ms, p);
            //得到对象的2进制字节数组
            byte[] bytes = ms.GetBuffer();
            //存储字节
            File.WriteAllBytes(Application.dataPath + "/Lesson5.tang", bytes);
            //关闭内存流
            ms.Close();
        }

        //方法二:使用文件流进行存储
        //主要用于存储到文件中
        using (FileStream fs = new FileStream(Application.dataPath + "/Lesson5_2.tang", FileMode.OpenOrCreate, FileAccess.Write))
        {
            //2进制格式化程序
            BinaryFormatter bf = new BinaryFormatter();
            //序列化对象 生成2进制字节数组 写入到内存流当中
            bf.Serialize(fs, p);
            fs.Flush();
            fs.Close();
        }

        #endregion

        #region 总结
        //C#提供的类对象2进制序列化主要类是 BinaryFormatter
        //通过其中的序列化方法即可进行序列化生成字节数组
        #endregion
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

[System.Serializable]
public class Person
{
    public int age = 1;
    public string name = "唐老狮";
    public int[] ints = new int[] { 1, 2, 3, 4, 5 };
    public List<int> list = new List<int>() { 1, 2, 3, 4 };
    public Dictionary<int, string> dic = new Dictionary<int, string>() { { 1,"123"},{ 2,"1223"},{ 3,"435345" } };
    public StructTest st = new StructTest(2, "123");
    public ClssTest ct = new ClssTest();
}

[System.Serializable]
public struct StructTest
{
    public int i;
    public string s;

    public StructTest(int i, string s)
    {
        this.i = i;
        this.s = s;
    }
}

[System.Serializable]
public class ClssTest
{
    public int i = 1;
}

反序列化

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class Lesson6 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #region 知识点一 反序列化之 反序列化文件中数据
        //主要类
        //FileStream文件流类
        //BinaryFormatter 2进制格式化类
        //主要方法
        //Deserizlize
        //通过文件流打开指定的2进制数据文件
        using (FileStream fs = File.Open(Application.dataPath + "/Lesson5_2.tang", FileMode.Open, FileAccess.Read))
        {
            //申明一个 2进制格式化类
            BinaryFormatter bf = new BinaryFormatter();
            //反序列化
            Person p = bf.Deserialize(fs) as Person;

            fs.Close();
        }

        #endregion

        #region 知识点二 反序列化之 反序列化网络传输过来的2进制数据
        //主要类
        //MemoryStream内存流类
        //BinaryFormatter 2进制格式化类
        //主要方法
        //Deserizlize
        //目前没有网络传输 我们还是直接从文件中获取
        byte[] bytes = File.ReadAllBytes(Application.dataPath + "/Lesson5_2.tang");
        //申明内存流对象 一开始就把字节数组传输进去
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            //申明一个 2进制格式化类
            BinaryFormatter bf = new BinaryFormatter();
            //反序列化
            Person p = bf.Deserialize(ms) as Person;

            ms.Close();
        }

        #endregion
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}


Comment