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

Protobuf 学习简记(三)Unity C#中的序列化与反序列化

Protobuf 学习简记(三)Unity C#中的序列化与反序列化

    • 对文本的序列化与反序列化
    • 内存二进制流的序列化与反序列化
      • 方法一
      • 方法二
    • 参考链接

对文本的序列化与反序列化

private void Text()
{TestMsg1 myTestMsg = new TestMsg1();myTestMsg.TestInt32 = 1;myTestMsg.ArrString.Add("wy");myTestMsg.ArrString.Add("pnb");myTestMsg.ArrString.Add("lzq");myTestMsg.Map1.Add(1, "ywj");myTestMsg.Map1.Add(2, "zzs");//序列化string path = Application.persistentDataPath + "/testMsg.msg";using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)){myTestMsg.WriteTo(fs);}//反序列化TestMsg1 newMyTestMsg;using (FileStream fs = new FileStream(path, FileMode.Open)){newMyTestMsg = TestMsg1.Parser.ParseFrom(fs);}Debug.Log(newMyTestMsg.TestInt32);Debug.Log(newMyTestMsg.ArrString);Debug.Log(newMyTestMsg.ArrString.Count);Debug.Log(newMyTestMsg.Map1[1]);Debug.Log(newMyTestMsg.Map1[2]);
}

内存二进制流的序列化与反序列化

方法一

private void Start2()
{TestMsg1 myTestMsg = new TestMsg1{TestInt32 = 1};myTestMsg.ArrString.Add("wy");myTestMsg.ArrString.Add("pnb");myTestMsg.ArrString.Add("lzq");myTestMsg.Map1.Add(1, "ywj");myTestMsg.Map1.Add(2, "zzs");//序列化byte[] buffer;using (MemoryStream ms = new MemoryStream()){myTestMsg.WriteTo(ms);buffer = ms.ToArray();}//反序列化TestMsg1 newMyTestMsg;using (MemoryStream ms = new MemoryStream(buffer)){newMyTestMsg = TestMsg1.Parser.ParseFrom(ms);}Debug.Log(newMyTestMsg.TestInt32);Debug.Log(newMyTestMsg.ArrString);Debug.Log(newMyTestMsg.ArrString.Count);Debug.Log(newMyTestMsg.Map1[1]);Debug.Log(newMyTestMsg.Map1[2]);
}

方法二

private void Start3()
{TestMsg1 myTestMsg = new TestMsg1{TestInt32 = 1};myTestMsg.ArrString.Add("wy");myTestMsg.ArrString.Add("pnb");myTestMsg.ArrString.Add("lzq");myTestMsg.Map1.Add(1, "ywj");myTestMsg.Map1.Add(2, "zzs");byte[] buffer = myTestMsg.ToByteArray();//序列化TestMsg1 newMyTestMsg = TestMsg1.Parser.ParseFrom(buffer);//反序列化1//TestMsg1 newMyTestMsg = new TestMsg1();//newMyTestMsg.MergeFrom(buffer);//反序列化2Debug.Log(newMyTestMsg.TestInt32);Debug.Log(newMyTestMsg.ArrString);Debug.Log(newMyTestMsg.ArrString.Count);Debug.Log(newMyTestMsg.Map1[1]);Debug.Log(newMyTestMsg.Map1[2]);
}

反序列化1与反序列化2都可以正常使用。

参考链接

  1. https://blog.csdn.net/zzzsss123333/article/details/125505066
  2. https://blog.csdn.net/u011723630/article/details/127464374

另外有《unity中使用protobuf-net库》的文章示例:

  1. https://www.jb51.cc/unity/3755981.html
  2. https://stackoverflow.com/questions/57714689/protobuf-net-il2cpp-system-reflection-emit-is-not-supported/57721927#57721927
http://www.lryc.cn/news/18136.html

相关文章:

  • Flask入门(10):Flask使用SQLAlchemy
  • 我的 System Verilog 学习记录(4)
  • Git : 本地分支与远程分支的映射关系
  • 运维必看|跨国公司几千员工稳定访问Office365,怎么实现?
  • Python GDAL读取栅格数据并基于质量评估波段QA对指定数据加以筛选掩膜
  • Vue3:有关v-model的用法
  • CF1692C Where‘s the Bishop? 题解
  • Jenkins集成Allure报告
  • 【数据结构】AVL树
  • 这一次我不再低调,老板法拉利的车牌有我的汗水
  • 通过连接另一个数组的子数组得到一个数组
  • 公派访问学者的申请条件
  • 多点电容触摸屏实验
  • 【算法与数据结构(C语言)】栈和队列
  • Uni-app使用vant和uview组件
  • 2023年PMP考试应该注意些什么?
  • selenium环境安装及使用
  • 高性能低功耗4口高速USB2.0 HUB 完美替代FE1.1S和FE8.1
  • Go全栈学习(一)基础语法
  • centos7搭建svn配置
  • 趣味三角——第12章——tanx
  • Java - 数据结构,栈
  • 某餐厅系统网络故障分析案例
  • 华为OD机试题,用 Java 解【密室逃生游戏】问题
  • 如何重命名SQL Server数据库
  • 联想昭阳E5-ITL电脑开机后绿屏怎么U盘重装系统?
  • 车载开发知识交流【学习路线】
  • 【读书笔记】《深入浅出数据分析》第二章 检验你的理论
  • pyflink学习笔记(一):table_apisql
  • GCC 编译器套件说明