unity3D中使用二进制的方法存储数据(BinaryFormatter序列化与反序列化)

时间:2024-05-22 07:01:17

首先呢,要说一下,在unity3d中存储数据的方法有很多。json,xml,playerprefs等是最长用的,都有优缺点吧,今天要说的二进制存储简单好用,堪称较好的存储方法。
1:我们稍微了解下BinaryFormatter序列化与反序列化:

BinaryFormatte序列化:将对象转化成二进制,BinaryFormatte反序列化就是将二进制转化为对象;

命名空间: System.Runtime.Serialization.Formatters;
unity3D中使用二进制的方法存储数据(BinaryFormatter序列化与反序列化)
废话不多说,直接上代码,比较清晰的去看吧。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;
public class DataManager : MonoBehaviour
{
    public Text t;
    public static string path;
    public string pathname = "my_path.bin";

    [System.Serializable]
    public class Zhuan_ye
    {
        public int ID;
        public string name;
        public List<Ban_ji> ban_ji = new List<Ban_ji>();
    }
    [System.Serializable]
    public class Ban_ji
    {
        public int ID;
        public string name;
        public int studentNumber;
    }

    private void Awake()
    {
        //班级
        List<Ban_ji> ban_jis = new List<Ban_ji>();
        for (int i = 0; i < 5; i++)
        {
            Ban_ji ban_ji=new Ban_ji();
            ban_ji.ID = i + 1;
            ban_ji.name = "name:" + (i + 1);
            ban_ji.studentNumber = i;
            ban_jis.Add(ban_ji);
        }

       Zhuan_ye zhuan_ye=new Zhuan_ye();
       zhuan_ye.ID = 100;
       zhuan_ye.name = "yaogaoshang";
       zhuan_ye.ban_ji = ban_jis;

        SaveBin(zhuan_ye);
        ReadS();



    }
    /// <summary>
    /// 将获取的二进制文件中的对象打印出来
    /// </summary>
    void ReadS()
    {
        Zhuan_ye zhuan_Ye = OpenBin();

        print("专业:" + zhuan_Ye.ID + "   " + zhuan_Ye.name);
        List<Ban_ji> BANS = zhuan_Ye.ban_ji;
        foreach (Ban_ji item in BANS)
        {
            print("\n");
            print(item.ID + "--" + item.name + "---" + item.studentNumber);
        }
    }
    /// <summary>
    /// 将对象存储在一个二进制文件中
    /// </summary>
    private void SaveBin(Zhuan_ye zhuan_ye)
    {
        try
        {
            path = getpath();
            FileStream file = null;
            BinaryFormatter bf = new BinaryFormatter();
            file = File.Open(path, FileMode.Create);
            bf.Serialize(file, zhuan_ye);
            file.Close();
            print("成功存储");
        }
        catch (System.Exception ex)
        {
           Debug.LogError("存储失败----"+ex.Message);
        }
        //string.IsNullOrEmpty(path):基本等价与path==null||path.length==0::没用到,但想到。

    }
    /// <summary>
    /// 返回二进制文件的反序列化对象
    /// </summary>
    /// <returns>The bin.</returns>
    public Zhuan_ye OpenBin()
    {
        path = getpath();
        Zhuan_ye zhuan_ye = null;
        FileStream file = null;
        BinaryFormatter bf = new BinaryFormatter();
        file = File.Open(path, FileMode.Open);
        zhuan_ye = (Zhuan_ye)bf.Deserialize(file);
        file.Close();
        return zhuan_ye;
    }
    /// <summary>
    /// 相对路径
    /// </summary>
    /// <returns>The getpath.</returns>
    public string getpath()
    {
        path = Application.dataPath + "/" +pathname;
        return path;
    }
}

代码中注释都比较清楚,最重要的是存和取的方法,一看就懂。运行后文件存在了这里:
unity3D中使用二进制的方法存储数据(BinaryFormatter序列化与反序列化)
目前我还没能找到方法不经过代码,直接阅读这个my_path文件下的内容。等找到方法会与大家分享的。