当前位置: 首页 > news >正文

[unity]对象的序列化

抽象的图纸叫类,包含具体数据的叫对象。

类的序列化和反序列化

using System.Collections;
using System.Collections.Generic;
using UnityEngine;using System;  
using System.IO;  
using System.Runtime.Serialization.Formatters.Binary;  [Serializable]   
public class MyData  
{  public int Number { get; set; }  public string Text { get; set; }  
}  public class Save : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){MyData data = new MyData() { Number = 123, Text = "Hello, world!" };  // 保存数据到硬盘  BinaryFormatter formatter = new BinaryFormatter();  using (FileStream stream = new FileStream("D://mydata.bin", FileMode.Create))  {  formatter.Serialize(stream, data);  }  // 从硬盘读取数据  MyData deserializedData;  using (FileStream stream = new FileStream("D://mydata.bin", FileMode.Open))  {  deserializedData = (MyData)formatter.Deserialize(stream);  }  Debug.Log(deserializedData.Number+" , "+deserializedData.Text);}// Update is called once per framevoid Update(){}
}

数组的序列化和反序列化

using System.Collections;
using System.Collections.Generic;
using UnityEngine;// 序列化和反序列化用到的
using System;  
using System.IO;  
using System.Runtime.Serialization.Formatters.Binary;  public class Save : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){// // 01.序列化// int[] intArray1 = { 1, 2, 3, 4, 5 };  // int[] intArray2 = { 10, 20, 30, 40, 50 };  // // 创建一个二进制格式化器  // BinaryFormatter formatter = new BinaryFormatter();  // // 创建一个文件流  // using (FileStream stream = new FileStream("D://test.bin", FileMode.Create))  // {  //     // 序列化第一个数组  //     formatter.Serialize(stream, intArray1);  //     // 序列化第二个数组  //     formatter.Serialize(stream, intArray2);  // }// 02.反序列化// 创建一个二进制格式化器  BinaryFormatter formatter = new BinaryFormatter();  // 创建一个文件流  using (FileStream stream = new FileStream("D://test.bin", FileMode.Open))  {  // 反序列化第一个数组  int[] intArray1 = (int[])formatter.Deserialize(stream);  // 反序列化第二个数组  int[] intArray2 = (int[])formatter.Deserialize(stream);  // 打印数组内容  foreach (int i in intArray1)  {  Debug.Log(i);  }  foreach (int i in intArray2)  {  Debug.Log(i);  }  }}// Update is called once per framevoid Update(){}
}

http://www.lryc.cn/news/179207.html

相关文章:

  • java开发岗位面试
  • 坠落防护 挂点装置
  • 关于 自定义的RabbitMQ的RabbitMessageContainer注解-实现原理
  • uniapp快速入门系列(1)- 概述与基础知识
  • 国密国际SSL双证书解决方案,满足企事业单位国产国密SSL证书要求
  • LabVIEW开发虚拟与现实融合的数字电子技术渐进式实验系统
  • 机器学习之单层神经网络的训练:增量规则(Delta Rule)
  • C# Task任务详解
  • 百度网盘的扩容
  • Android 悬浮窗
  • 3.物联网射频识别,(高频)RFID应用ISO14443-2协议
  • 数据分析笔记1
  • paramiko 3
  • 基于Dlib训练自已的人脸数据集提高人脸识别的准确率
  • Git 详细安装教程(详解 Git 安装过程的每一个步骤
  • kafka伪集群部署,使用KRAFT模式
  • 【双指针遍历】N数之和问题
  • Qt的QObject类
  • 【图论C++】链式前向星(图(树)的存储)
  • 16.PWM输入捕获示例程序(输入捕获模式测频率PWMI模式测频率和占空比)
  • pip version 更新
  • Oracle - 多区间按权重取值逻辑
  • 本次CTF·泰山杯网络安全的基础知识部分(二)
  • MyBatis 映射文件(Mapper XML):配置与使用
  • 基于 SpringBoot 的大学生租房网站
  • BL808学习日志-0-概念理解
  • CISSP学习笔记:业务连续性计划
  • .NET Nuget包推荐安装
  • 【文献阅读】Pocket2Mol : 基于3D蛋白质口袋的高效分子采样 + CrossDocked数据集说明
  • TrustRadius 评论:为什么 Splashtop 优于 LogMeIn