1.下载和编译

1.1 下载ProtoBuf源文件

  从 github 下载最新的 protoBuf 库,如下图所示 Releases · protocolbuffers/protobuf (github.com)

1.2 编译dll和导入

  解压后打开 /scharp/src 中的 sln 工程文件

  选择Release,Google.Protobuf,之后在生成中生成文件

  在以下路径中可以找到生成的所有文件 \csharp\src\Google.Protobuf\bin\Release\net45

  把所有文件都导入Plugins中,否则会报 Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform? 这个错误

1.3 使用protoc编译成目标语言

  下载应用程序用于将proto文件编译成c#需要的文件

  编写一个proto格式的脚本,命名为 test.proto

// 指定版本
syntax = "proto3";
// C#中的命名空间(namespace)
package ProtoTest; // 消息类
message MsgResult {
// 消息码
int32 code = 1;
// repeated 相当于一个数组
repeated int32 positions = 2;
}

  在该 exe 页面下打开一个 cmd 命令窗口,将 test.proto 编译成 csharp 的版本

protoc.exe --csharp_out=. test.proto

1.4 使用生成的脚本序列化和反序列化

  把csharp脚本导入到unity中,可以跟普通的序列化和反序列化一样使用

// 序列化
MsgResult serializeMsg = new ProtoTest.MsgResult()
{
Code = 100, Positions = { 1, 2, 3, 4, 5 }
};
var json = serializeMsg.ToString(); // 转 json
var byteStr = serializeMsg.ToByteString(); // 转 byte String
var byteArr = serializeMsg.ToByteArray(); // 转 byte Array // 反序列化
var deserialzeMsg = ProtoTest.MsgResult.Parser.ParseFrom(response.Data);

Unity 通信方案 - 使用 Google Protobuf 序列化数据的更多相关文章

  1. google protobuf序列化原理解析 (PHP示例)

    一.简介 Protocol Buffers是谷歌定义的一种跨语言.跨平台.可扩展的数据传输及存储的协议,因为将字段协议分别放在传输两端,传输数据中只包含数据本身,不需要包含字段说明,所以传输数据量小, ...

  2. Google protobuf序列化以及反序列化

    序列化的目的是将对象持久化到硬盘或者用于网络传输.java也提供了序列化技术,非常简单,只要实现Serializable接口即可.如下: public class commonService impl ...

  3. python protobuf序列化repeated运用

    下面是proto描述文件的定义 message Person { required string name = 1; required int32 id = 2; optional string em ...

  4. 【通信框架】Google的开源通信框架protobuf概述

    在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com    QQ:1494713801 一.作用 protobuf(Protocol Buffers)是Google内部 ...

  5. protobuf protocol-buffers 序列化数据 gobs pickling string XML 用C实现的cPickle比pickle快1000倍 protobuf2 protobuf3 差异

    场景: 浏览器请求--->python数据生成--->python-生成excel--->浏览器下载excel 目标: 重构为 浏览器请求--->python数据生成---&g ...

  6. Protobuf在Python中的应用(序列化数据)

    1.了解Protobuf Protocol Buffer是Google的语言中立的,平台中立的,可扩展机制的,用于序列化结构化数据 - 对比XML,但更小,更快,更简单.您可以定义数据的结构化,然后可 ...

  7. VS下使用Google Protobuf完成SOCKET通信

    如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 出处:如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 最近一 ...

  8. 孙荣辛|大数据穿针引线进阶必看——Google经典大数据知识

    大数据技术的发展是一个非常典型的技术工程的发展过程,荣辛通过对于谷歌经典论文的盘点,希望可以帮助工程师们看到技术的探索.选择过程,以及最终历史告诉我们什么是正确的选择. 何为大数据   "大 ...

  9. google protobuf使用

    下载的是github上的:https://github.com/google/protobuf If you get the source from github, you need to gener ...

  10. (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-1

    Google的Protobuf在业界非常流行,很多商业项目选择Protobuf作为编解码框架,这里一起回顾一下Protobuf    的优点.(1)在谷歌内部长期使用,产品成熟度高:(2)跨语言,支持 ...

随机推荐

  1. ImportError: lxml.html.clean module is now a separate project lxml_html_clean

    导包报错 from lxml_html_clean import Cleaner 解决报错:"ImportError: lxml.html.clean module is now a sep ...

  2. [转载]修复容器中的top指令以及/proc文件系统中的信息

    如何修复容器中的top指令以及/proc文件系统中的信息呢? 通过lxcfs解决Docker和Kubernetes环境中解决遗留应用无法识别容器资源限制的问题. wget https://copr-b ...

  3. 处理日期和时间的chrono库

    C++11中提供了日期和时间相关的库chrono,通过chrono库可以很方便地处理日期和时间,为程序的开发提供了便利.chrono库主要包含三种类型的类:时间间隔duration.时钟clocks. ...

  4. 关于springboot启动时,老找不到bean

    spring可以管理部分工具类,但是不可以管理接口,也就是@Controller,@Component,@Repository,@Service不能放接口上. 就相当于new一个一个实例 但是你在使用 ...

  5. 使用 certbot 通过 Let's Encrypt 申请免费证书,部署到 nginx 中,开启 https

    使用 certbot 可以很方便.快捷的通过 Let's Encrypt 申请免费的证书,并部署到 nginx 中,开启 https 在 Linux 通过命令安装 安装 Nginx sudo apt ...

  6. ChatGPT学习之旅 (3) Prompt进阶用法

    大家好,我是Edison. 上一篇:Hello Prompt 复习Prompt用法 还记得上一篇学到的黄金公式吗? 这里,我们先来复习一下,假如我们想要ChatGPT来扮演一个[私人营养师]为我们给出 ...

  7. Elastic学习之旅 (7) 聚合分析

    大家好,我是Edison. 上一篇:ES的Query DSL 什么是ES的聚合 Elastic除搜索之外,还提供针对ES数据的统计分析功能,具有较高的实时性. 通过聚合,我们可以得到一个数据的概览,它 ...

  8. Office 无法插入ActiveX控件的一个解决办法。

    微软4月中旬公告,基于安全考量,微软将默认关闭Microsoft 365及Office 2024中的ActiveX.造成"开发工具"下无法插入ActiveX控件. 想再打开Acti ...

  9. SS 集训做题

    9.11 主题:分块 9. 主题:计数 Problem A 矩乘板子 Problem B 观察到 N 很小.记 \(f_{i, S}\) 为第 \(i\) 列状态为 \(S\) 的方案数,\((2^8 ...

  10. 基于lede开发自己的路由器系统openWrt

    搭建linux环境 强烈推荐ubuntu,debian会遇到大量的依赖问题 安装编译依赖 更新软件包 sudo apt update -y 整体更新软件包 sudo apt full-upgrade ...