• [unity]对象的序列化


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

    类的序列化和反序列化

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.IO;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7. [Serializable]
    8. public class MyData
    9. {
    10. public int Number { get; set; }
    11. public string Text { get; set; }
    12. }
    13. public class Save : MonoBehaviour
    14. {
    15. // Start is called before the first frame update
    16. void Start()
    17. {
    18. MyData data = new MyData() { Number = 123, Text = "Hello, world!" };
    19. // 保存数据到硬盘
    20. BinaryFormatter formatter = new BinaryFormatter();
    21. using (FileStream stream = new FileStream("D://mydata.bin", FileMode.Create))
    22. {
    23. formatter.Serialize(stream, data);
    24. }
    25. // 从硬盘读取数据
    26. MyData deserializedData;
    27. using (FileStream stream = new FileStream("D://mydata.bin", FileMode.Open))
    28. {
    29. deserializedData = (MyData)formatter.Deserialize(stream);
    30. }
    31. Debug.Log(deserializedData.Number+" , "+deserializedData.Text);
    32. }
    33. // Update is called once per frame
    34. void Update()
    35. {
    36. }
    37. }

    数组的序列化和反序列化

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. // 序列化和反序列化用到的
    5. using System;
    6. using System.IO;
    7. using System.Runtime.Serialization.Formatters.Binary;
    8. public class Save : MonoBehaviour
    9. {
    10. // Start is called before the first frame update
    11. void Start()
    12. {
    13. // // 01.序列化
    14. // int[] intArray1 = { 1, 2, 3, 4, 5 };
    15. // int[] intArray2 = { 10, 20, 30, 40, 50 };
    16. // // 创建一个二进制格式化器
    17. // BinaryFormatter formatter = new BinaryFormatter();
    18. // // 创建一个文件流
    19. // using (FileStream stream = new FileStream("D://test.bin", FileMode.Create))
    20. // {
    21. // // 序列化第一个数组
    22. // formatter.Serialize(stream, intArray1);
    23. // // 序列化第二个数组
    24. // formatter.Serialize(stream, intArray2);
    25. // }
    26. // 02.反序列化
    27. // 创建一个二进制格式化器
    28. BinaryFormatter formatter = new BinaryFormatter();
    29. // 创建一个文件流
    30. using (FileStream stream = new FileStream("D://test.bin", FileMode.Open))
    31. {
    32. // 反序列化第一个数组
    33. int[] intArray1 = (int[])formatter.Deserialize(stream);
    34. // 反序列化第二个数组
    35. int[] intArray2 = (int[])formatter.Deserialize(stream);
    36. // 打印数组内容
    37. foreach (int i in intArray1)
    38. {
    39. Debug.Log(i);
    40. }
    41. foreach (int i in intArray2)
    42. {
    43. Debug.Log(i);
    44. }
    45. }
    46. }
    47. // Update is called once per frame
    48. void Update()
    49. {
    50. }
    51. }

  • 相关阅读:
    5米DEM高程数据分析和对比
    Pro_06丨重心拐点与高低波出场
    LeetCode 731. My Calendar II【设计,有序映射,差分;线段树】中等
    【Java 进阶篇】HTML DOM样式控制详解
    docker容器安装gdb
    Springboot毕设项目高校心理咨询系统y34td(java+VUE+Mybatis+Maven+Mysql)
    JSP 装饰公司网站myeclipse开发mysql数据库MVC模式java编程网页设计系统采用mvc框架
    leetcode 215:数组中的第K个最大元素
    npm ERR! While resolving: ruoyi@3.8.5npm ERR! Found: webpack@5.89.0
    http https socket rpc grpc有啥区别联系
  • 原文地址:https://blog.csdn.net/averagePerson/article/details/133442117