序列化和反序列化

博客介绍了在Unity中实现序列化和反序列化的相关内容。展示了可序列化类的定义,使用[Serializable]关键字证明类可序列化,还给出了序列化和反序列化所需的头文件引用。

序列化和反序列化
此文件在unity中实现
using UnityEngine;
using System.Collections;
using System;

[Serializable]//关键字 证明下面的类是可序列化的,在system的命名空间里
public class class1
{
public string Name;
public int Age;
}


using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;//序列化和反序列化的头文件

public class classxuliehua : MonoBehaviour
{

public string Path = "E:\\dsfg.dat";
// Use this for initialization
void Start()
{
    if (File.Exists(Path))
    {
        class1 nee = read();
        Debug.Log(nee.Age + " " + nee.Name);
    }
    else
    {
        class1 cls = new class1();
        cls.Age = 18;
        cls.Name = "赵钱孙";
        creat(cls);

    }

}
void creat(class1 cls)
{
    FileStream Fstream = new FileStream(Path, FileMode.Create);//开启一个流
    BinaryFormatter Binary = new BinaryFormatter();//定义一个二进制的类
    Binary.Serialize(Fstream, cls);//二进制序列化把序列化的文件写入流里
    Fstream.Close();//关闭流

}
class1 read()
{
    FileStream Fstream = new FileStream(Path, FileMode.Open);//开启流并打开路径文件
    BinaryFormatter reserve = new BinaryFormatter();
    class1 newclass = (class1)reserve.Deserialize(Fstream);//反序列化这个流,输入一个(object)文件
    //Fstream.Close();
    return newclass;	
}

// Update is called once per frame

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Glunn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值