C# xml序列化 二进制序列化 unity中的asset序列化

4 篇文章 0 订阅

序列化是将对象保存为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);
        }
    }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值