序列化范例

using UnityEngine;
using System.Collections;
using System;

[Serializable]
public class test
{
    public int n1 = 0;
    public int n2 = 5;
    // [NonSerialized]
    public string str = null;
}
using UnityEngine;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class get : MonoBehaviour {

    // Use this for initialization
    void Start () {
        test obj = new test();
        obj.n1 = 1;
        obj.n2 = 2;
        obj.str = "简单的字符串";
        
        IFormatter formater = new BinaryFormatter();
        Stream stream = new FileStream("c:/SaveDtat.bin", FileMode.Create, FileAccess.Write, FileShare.None);
        formater.Serialize(stream, obj);
        stream.Close();
        
        
        IFormatter formatter = new BinaryFormatter();
        Stream reader_stream = new FileStream("c:/SaveDtat.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
        test readerObj = (test)formatter.Deserialize(reader_stream);
        reader_stream.Close();
        
        Debug.Log(readerObj.n1);
        Debug.Log(readerObj.n2);
        Debug.Log(readerObj.str);
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}
原文地址:https://www.cnblogs.com/softimagewht/p/3885579.html