Unity | 集成 Protobuf(proto 转 cs 插件及序列化与反序列化)

news/2024/10/22 18:33:03/

1. 添加 dll

1. 下载 protobuf 源码

根据需要下载 protobuf 指定版本的源码,这里以 v3.21.12(protobuf-csharp-3.21.12.zip)为例:

下载地址:「https://github.com/protocolbuffers/protobuf/releases」

2. 下载 Visual Studio 2022

曾尝试使用 Visual Studio 2021 版本编译,但是会报错,更新版本后,即可编译成功

下载地址:「https://visualstudio.microsoft.com/zh-hans/downloads/」

3. 编译 dll

1. 使用 Visual Studio 2022 打开 protobuf 项目中的 Google.Protobuf.sln

2. 编译模式选择:Release

3. Google.Protobuf 右键选择生成 Google.Protobuf

4. 拷贝 dll

编译生成的 dll 在 csharp/src/Google.Protobuf/bin/Release 目录下

将 net45 目录下所有的 dll 文件拷贝到 Unity 指定目录中

2. proto 转 cs

1. 下载编译器

在 protobuf 源码链接中下载对应的平台的编译器(mac: protoc-21.12-osx-x86_64.zip,windows:protoc-21.12-win64.zip)

2. 命令行转换

在终端中进入到编译器所在的目录:bin/protoc,然后执行对应的命令

  • 转换指定 proto 文件:

./protoc ./Addressbook.proto --csharp_out=./
  • 转换指定目录下所有 proto 文件:

./protoc ./*.proto --csharp_out=./

将生成的 cs 文件,放入 Unity 项目中

3. 插件转换

除了直接使用命令行外,也可以在 Unity 中编写插件,利用 Process 执行命令行,转换 proto 文件:

public class ProtoToClass
{[MenuItem("RavenKit/Proto To Class")]public static void GenerateClassFromProto(){string rootPath = Environment.CurrentDirectory;string protoPath = Path.Combine(rootPath, "Proto/");string protoc = Path.Combine(protoPath,RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "protoc.exe" : "protoc");string outPath = EditorPrefs.GetString(Application.identifier + "-ProtoClassPath");if (string.IsNullOrEmpty(outPath)){outPath = EditorUtility.OpenFolderPanel("选择存储目录", Application.dataPath, "");if (!string.IsNullOrEmpty(outPath)){EditorPrefs.SetString(Application.identifier + "-ProtoClassPath", outPath);}}string[] protoFiles = Directory.GetFiles(protoPath, "*.proto");foreach (var variable in protoFiles){string argument = $"--csharp_out={outPath} --proto_path={protoPath} {variable}";Run(protoc, argument);}AssetDatabase.Refresh();}public static Process Run(string exe, string arguments){ProcessStartInfo info = new ProcessStartInfo{FileName = exe,Arguments = arguments,CreateNoWindow = true,UseShellExecute = false,RedirectStandardOutput = true,RedirectStandardError = true,};Process process = Process.Start(info);process.WaitForExit();if (process.ExitCode != 0){Debug.LogError($"Failed to Run {arguments}. Exit code: " + process.ExitCode);}return process;}
}

3. 序列化及反序列化

使用方法在官方的源码中也有详细的示例:

byte[] bytes;
// Create a new person
Person person = new Person
{Id = 1,Name = "Foo",Email = "foo@bar",Phones = { new Person.Types.PhoneNumber { Number = "555-1212" } }
};
using (MemoryStream stream = new MemoryStream())
{// Save the person to a streamperson.WriteTo(stream);bytes = stream.ToArray();
}
Person copy = Person.Parser.ParseFrom(bytes);AddressBook book = new AddressBook
{People = { copy }
};
bytes = book.ToByteArray();
// And read the address book back again
AddressBook restored = AddressBook.Parser.ParseFrom(bytes);// The message performs a deep-comparison on equality:
if (restored.People.Count != 1 || !person.Equals(restored.People[0]))
{throw new Exception("There is a bad person in here!");
}

反序列化时,除了使用 ParseFrom 方法

AddressBook.Parser.ParseFrom(bytes) 

也可以使用 MergeFrom 方法

message.MergeFrom(data);

将序列化和反序列化封装为通用的函数:

public static class ProtobufUtility
{/// <summary>/// 序列化/// </summary>/// <param name="msg"></param>/// <returns></returns>public static byte[] Serialize(IMessage message){using (MemoryStream stream = new MemoryStream()){message.WriteTo(stream);byte[] result = ms.ToArray();return result;}}/// <summary>/// 反序列化/// </summary>/// <typeparam name="T"></typeparam>/// <param name="data"></param>/// <returns></returns>public static T Deserialize<T>(byte[] data) where T : IMessage, new(){T message = new T();// message = (T)message.Descriptor.Parser.ParseFrom(data);message.MergeFrom(data);return msg;}
}


http://www.ppmy.cn/news/1442524.html

相关文章

Springboot 中RedisTemplate使用scan来获取所有的key底层做了哪些事情

直接上代码&#xff0c;围绕着代码来讨论 redisTemplate.execute((RedisCallback<Object>) (connection) -> {Cursor<byte[]> scan connection.scan(ScanOptions.scanOptions().count(2).match("*").build());scan.forEachRemaining((bytes) -> {…

有关钱包相关开发的库和依赖

Trezor 钱包 GitHub 组织&#xff1a;https://github.com/trezor说明&#xff1a;Trezor 是一款硬件加密货币钱包&#xff0c;它的团队开发了与助记词相关的许多工具和库。 Electrum 钱包 GitHub 仓库&#xff1a;https://github.com/spesmilo/electrum说明&#xff1a;Electru…

mongodb 数据库基本操作详解及示例

MongoDB 是一个基于分布式文件存储的 NoSQL 数据库&#xff0c;由 C 语言编写。相较于关系型数据库&#xff0c;MongoDB 更加灵活并且有着良好的扩展性。 以下是 MongoDB 的一些基本操作&#xff1a; 启动 MongoDB Server&#xff1a; 在终端中执行 mongod 命令来启动 MongoDB …

Flutter开发好用插件url_launcher详解-启动 URL

文章目录 url_launcher介绍安装用法错误处理自定义行为其他功能 url_launcher介绍 url_launcher 是一个 Flutter 插件&#xff0c;用于启动 URL。它支持网络、电话、短信和电子邮件方案。您可以使用它从您的 Flutter 应用程序中打开网站、拨打号码、发送短信或撰写电子邮件。 …

ajax使用案例

1.index.jsp页面&#xff1a; 下边是采用JavaScript的ajax发出异步请求。 <% page language"java" contentType"text/html; charsetUTF-8" pageEncoding"UTF-8"%> <%String basePath request.getScheme()"://" request.ge…

# 从浅入深 学习 SpringCloud 微服务架构(六)Feign(3)

从浅入深 学习 SpringCloud 微服务架构&#xff08;六&#xff09;Feign&#xff08;3&#xff09; 一、组件的使用方式总结 1、注册中心 1&#xff09; Eureka 搭建注册中心 引入依赖 spring-cloud-starter-netflix-eureka-server。 配置 EurekaServer。 通过 EnableEure…

【论文笔记】基于预训练模型的持续学习(Continual Learning)(增量学习,Incremental Learning)

论文链接&#xff1a;Continual Learning with Pre-Trained Models: A Survey 代码链接&#xff1a;Github: LAMDA-PILOT 持续学习&#xff08;Continual Learning, CL&#xff09;旨在使模型在学习新知识的同时能够保留原来的知识信息了&#xff0c;然而现实任务中&#xff…

Java23种设计模式-行为型模式之命令模式

命令模式&#xff08;Command Pattern&#xff09;&#xff1a;将一个请求封装为一个对象&#xff0c;从而允许用户使用不同的请求、队列或日志请求来参数化其他对象。命令模式也支持可撤销的操作。它通常用于解耦执行操作的对象与知道如何实现操作的对象。 基本组成&#xff…