序列化

类 XML互转 和 类与二进制互转

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

public class ResourcesTest : MonoBehaviour
{
    void Start()
    {
        //SerilizeTest();
        //DeSerilizerTest();
        //BinarySerTest();
        //BinaryDeSerTest();
  `        ReadTestAssets()
    }
    #region XML 
    //=============序列化================
    /// 
    /// xml 序列化
    /// 
    void SerilizeTest()
    {
        TestSerilize testSerilize = new TestSerilize();
        testSerilize.Id = 1;
        testSerilize.Name = "学习!";
        testSerilize.List = new List();
        testSerilize.List.Add(888);
        testSerilize.List.Add(999);
        XmlSerilize(testSerilize);
    }

    void XmlSerilize(TestSerilize testSerilize)
    {
        FileStream fileStream = new FileStream(Application.dataPath + "/test.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        StreamWriter sw = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
        XmlSerializer xml = new XmlSerializer(testSerilize.GetType());
        xml.Serialize(sw, testSerilize);
        sw.Close();
        fileStream.Close();
    }
    //=============返序列化================
    /// 
    /// 反序列化
    /// 
    void DeSerilizerTest()
    {
        TestSerilize testSerilize = XmlSerilize();
        Debug.Log(testSerilize.Id + "-----" + testSerilize.Name);
        for (int i = 0; i < testSerilize.List.Count; i++)
        {
            Debug.Log(testSerilize.List[i]);
        }
    }
    /// 
    /// xml 反序列化
    /// 
    /// 
    TestSerilize XmlSerilize()
    {
        FileStream fileStream = new FileStream(Application.dataPath + "/test.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
        XmlSerializer xs = new XmlSerializer(typeof(TestSerilize));
        TestSerilize testSerilize = (TestSerilize)xs.Deserialize(fileStream);
        fileStream.Close();
        return testSerilize;
    }
    #endregion


    #region 二进制
     //=============序列化================
    void BinarySerTest()
    {
        TestSerilize testSerilize = new TestSerilize();
        testSerilize.Id = 10;
        testSerilize.Name = "二进制学习!";
        testSerilize.List = new List();
        testSerilize.List.Add(666);
        testSerilize.List.Add(777);

        BinarySerilize(testSerilize);
    }

    void BinarySerilize(TestSerilize testSerilize)
    {
        FileStream fs = new FileStream(Application.dataPath + "/test.bytes", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, testSerilize);
        fs.Close();

    }
   //=============反序列化================

    void BinaryDeSerTest()
    {
        TestSerilize testSerilize = BinaryDeSerilize();
        Debug.Log(testSerilize.Id + "-----" + testSerilize.Name);
        for (int i = 0; i < testSerilize.List.Count; i++)
        {
            Debug.Log(testSerilize.List[i]);
        }
    }

    TestSerilize BinaryDeSerilize()
    {
        TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/test.bytes");
        MemoryStream stream = new MemoryStream(textAsset.bytes);
        BinaryFormatter bf = new BinaryFormatter();
        TestSerilize testSerilize = (TestSerilize)bf.Deserialize(stream);
        stream.Close();
        return testSerilize;
    }
    #endregion

  #region Assets 序列化
    void ReadTestAssets()
    {
        AssetsSerilize assets = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Script/TestAssets.asset");
        Debug.Log(assets.Id);
        Debug.Log(assets.Name);
        for (int i = 0; i < assets.TestList.Count; i++)
        {
            Debug.Log(assets.TestList[i]);
        }
    }
    #endregion
}

TestSerilize 类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Serialization;

[System.Serializable]
public class TestSerilize 
{
    [XmlAttribute("Id")]
    public int Id { get; set; }
    [XmlAttribute("Name")]
    public string Name { get; set; }
    [XmlElement("List")]
    public List List { get; set; }

}

AssetsSerilize类

using System.Collections.Generic;
using UnityEngine;


[CreateAssetMenu(fileName ="TestAssets",menuName ="CreatAssets",order = 0)]
public class AssetsSerilize : ScriptableObject
{

    public int Id;

    public string Name;

    public List TestList;
}

AssetsSerilize示例图

你可能感兴趣的:(序列化)