Protobuf
什么是Protobuf?
Protobuf(Protocal buffer)是google的一种数据交换的格式,基于二进制,跨语言、跨平台。
在Unity中使用Protobuf
两种方式:
1.导入Unity专用的Protobuf-net资源包,用C#代码定义对象。
2.配置.net的Protobuf环境,用原生的.proto文件定义对象。
我使用第一种方法使用unity版本2018.03
要导入资源包下载https://download.csdn.net/download/u011480667/12243334
首先 使用Protobuf对类序列化和反序列化操作
如何又使用Json对类序列化和反序列化操作
代码如下:
方法类
using System.IO;
using ProtoBuf;
public class PBParser
{
/// <summary> 序列化 </summary>
public static byte[] To<T>(T entity)
{
byte[] result = null;
if (entity != null)
{
using (MemoryStream stream = new MemoryStream())
{
Serializer.Serialize<T>(stream, enti