当前位置:网站首页>Protobuf 使用
Protobuf 使用
2022-04-23 06:28:00 【[奋斗不止]】
Protobuf 使用
Protobuf 概念以及干什么的就不在这里说了,看这篇文章的应该都了解了,不了解的可以自己查一下。
总共分三步
第一步:下载配置 Protobuf 环境
Protobuf github 下载链接
里面有各个版本的 package,记得要选 release,根据自己需求找到适合的版本
我这里使用的是 Protocol Buffers v3.0.0 Version 3.0.0链接
下拉找到 Assets 标签展开如下

我是在 Windows 系统上操作,所以我选择下载 protoc-3.0.0-win32.zip 解压后找到 bin 文件夹下面有个 protoc.exe,在环境变量 path 添加 protoc.exe 所在路径,然后打开 cmd 命令行 cd 到 protoc.exe 所在目录,然后输入 protoc --version 能正确显示 protobuf版本就 OK 了

第二步:使用Protobuf 生成对应语言使用的脚本
创建一个文件夹,在文件夹中新建 xxx.proto 文件,编辑如下 apc.proto
syntax = "proto3";
message ABC{
string name = 1;
string message_name = 2;
};
第一行 syntax = “proto3”; 指定你正在使用 proto3 语法
message ABC 指定一个类,类名是 ABC
*.proto 也可以相互引用,新建 ddd.proto 编辑如下
syntax = "proto3";
import "apc.proto";
message Npc
{
string damage = 1;
int32 range = 2;
ABC abc = 3;
};
import “apc.proto”; 意义:在 ddd.proto 中导入 abc.proto
可以在 ddd.proto 中使用 ABC 类了
开始生成对应语言 如 C++、C#、Java
生成 C++,命令行输入
protoc.exe -I=F:\Protobuf\proto --cpp_out=F:\Protobuf\Out F:\Protobuf\proto\apc.proto

-I=F:\Protobuf\proto 指定 *.proto 文件所在路径:注意:-I 是 横杠 和 大写字母 I
–cpp_out=F:\Protobuf\Out 指定导出的是C++ 文件,导出路径是F:\Protobuf\Out
最后 F:\Protobuf\proto\apc.proto 表示需要导出的是哪一个 .proto 文件
导出结果为一个.h 和一个 .cc 文件 apc.pb.cc apc.pb.h
生成 C# ,命令行输入
protoc.exe -I=F:\Protobuf\proto --csharp_out=F:\Protobuf\Out F:\Protobuf\proto\apc.proto
导出结果为一个 .cs 文件 Apc.cs
生成 Java,命令行输入
protoc.exe -I F:\Protobuf\proto --java_out=F:\Protobuf\Out F:\Protobuf\proto\apc.proto
导出结果为一个 .java 文件 Apc.java
执行三次,最终生成文件如下

第三步:项目中如何使用生成的 C++、Java、和 C#
我主要使用 C# 语言,需要在 Assets 下找到 C# 对应的 Package : protobuf-csharp-3.0.0.zip 或者 protobuf-csharp-3.0.0.tar.gz,这两个是一样的,我下载的是 protobuf-csharp-3.0.0.zip

下载 protobuf-csharp-3.0.0.zip 后解压,目录如下

按目录找到文件 protobuf-3.0.0\csharp\src\Google.Protobuf.sln

然后用 Visual Studio 打开 Google.Protobuf.sln, 我用的是 VS2017
如下选择 Google.protobuf -> 鼠标右键 -> 生成

生成成功后会在如下路径:protobuf-3.0.0\csharp\src\Google.Protobuf\bin\Release\net45
三个文件 Google.Protobuf.dll ,Google.Protobuf.pdb , Google.Protobuf.xml

将这三个文件放入自己的工程,我是在Unity 中使用,需要放在 Plugins 文件夹中

同时需要将 第二步生成的 C# 文件 Apc.cs 当到Unity中一个脚本目录即可,
C#代码调用如下
void Start()
{
// 实例化对象
ABC aBC = new ABC();
aBC.Name = "123";
aBC.MessageName = "ABC";
// 将对象序列化为 byte 数组
byte[] byteData = Serialize(aBC);
// 反序列化,将 byte 数组 转换回 类对象
ABC ddd = Deserialize<ABC>(byteData);
Debug.LogError(ddd.Name + " " + ddd.MessageName);
}
// 序列化:将 protobuf 类对象转换为 byte 数组
public static byte[] Serialize(IMessage msg)
{
using (MemoryStream ms = new MemoryStream())
{
CodedOutputStream outPut = new CodedOutputStream(ms);
outPut.WriteMessage(msg);
outPut.Flush();
byte[] byteData = ms.ToArray();
return byteData;
}
}
// 反序列化:将 byte 数组 转换为 对应的 protobuf 类对象
public static T Deserialize<T>(byte[] byteData) where T : IMessage, new()
{
CodedInputStream stream = new CodedInputStream(byteData);
T msg = new T();
stream.ReadMessage(msg);
return msg;
}
注意:第三步需要根据自己的语言下载编译对应的 Package 和 使用 .proto 生成对应语言的 脚本
版权声明
本文为[[奋斗不止]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/LIQIANGEASTSUN/article/details/123667502
边栏推荐
- MySQL8.0 安装/卸载 教程【Window10版】
- H5 local storage data sessionstorage, localstorage
- 取得所有点列表中的最大值GetMaxPoint
- NodeJS(一) 事件驱动编程
- ABAP CDS VIEW WITH ASSOCIATION示例
- C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
- [self motivation series] what really hinders you?
- 14. Transaction processing
- Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
- 将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
猜你喜欢

Use of command line parameter passing library argparse

Design optimization of MySQL database

【NLP笔记】CRF原理初探

Dropping Pixels for Adversarial Robustness

'NPM' is not an internal or external command, nor is it a runnable program or batch file

The page displays the current time in real time

Implementation of MySQL persistence

js之DOM事件

Django uses MySQL database to solve error reporting

FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
随机推荐
js之DOM学习三种创建元素的方式
Super classic & Programming Guide (red and blue book) - Reading Notes
Simple random roll call lottery (written under JS)
The difference and application of VR, AR and MR, as well as some implementation principles of AR technology
NodeJS(二)同步读取文件和异步读取文件
Install and configure Taobao image NPM (cnpm)
快速排序
反转链表练习
3. Sort statement
SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
SAP 导出Excel文件打开显示:“xxx“的文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?
Apache Hudi 如何加速传统的批处理模式?
Unity screen adaptation
SAP Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。
C# 读取注册表
SampleCameraFilter
js案例之求最大值,反转数组,冒泡排序
Unity ugui determines the solution of clicking on the UI and 3D objects
Window10版MySQL设置远程访问权限后不起效果
保研准备经验贴——18届(2021年)中南计科推免到浙大工院