Table of contents
Step 1: Download the source code
Step 4: Copy the library files to your project
Step 1: Download the source code
protobuf github source code https://github.com/protocolbuffers/protobuf download and unzip the source code to get the folder protobuf-main
Step 2: Run the C# build file
The source code of protobuf is in protobuf-main\csharp\src, but don’t open the sln project to build the dll by yourself. I tried it myself and there are many problems. It is recommended to use its build tools.
Open the powershell interface and run the protobuf-main\csharp\build_packages.bat file
I have an error here, the target dotnetSDK is missing, if you have installed it in advance, go directly to the fourth step.
Error message:
Step 3: Handle error reporting (if you have installed the corresponding SDK, no error will be reported)
If no error is reported, go to step 4.
To install the corresponding version, the global.json configuration of the protobuf-main folder must be consistent
What I downloaded here is .net6.0.100
After installing dotnet-sdk, execute again
Run the second step again:
PS C:\Users\HQ\Desktop\protobuf-main\csharp> C:\Users\HQ\Desktop\protobuf-main\csharp\build_packages.bat
C:\Users\HQ\Desktop\protobuf-main\csharp>dotnet restore src/Google.Protobuf.sln
欢迎使用 .NET 6.0!
---------------------
SDK 版本: 6.0.100
遥测
---------
.NET 工具会收集用法数据,帮助我们改善你的体验。它由 Microsoft 收集并与社区共享。你可通过使用喜欢的 shell 将 DOTNET_CLI_TELEMETRY_OPTOUT 环境变量设置为 "1" 或 "true" 来选择退出遥测。
阅读有关 .NET CLI 工具遥测的更多信息: https://aka.ms/dotnet-cli-telemetry
----------------
已安装 ASP.NET Core HTTPS 开发证书。
若要信任该证书,请运行 "dotnet dev-certs https --trust" (仅限 Windows 和 macOS)。
了解 HTTPS: https://aka.ms/dotnet-https
----------------
编写你的第一个应用: https://aka.ms/dotnet-hello-world
查找新增功能: https://aka.ms/dotnet-whats-new
浏览文档: https://aka.ms/dotnet-docs
在 GitHub 上报告问题和查找源: https://github.com/dotnet/core
使用 "dotnet --help" 查看可用命令或访问: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
正在确定要还原的项目…
已还原 C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.JsonDump\Google.Protobuf.JsonDump.csproj (用时 234 ms)。
已还原 C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.Test.TestProtos\Google.Protobuf.Test.TestProtos.csproj (用时 234 ms)。
已还原 C:\Users\HQ\Desktop\protobuf-main\csharp\src\AddressBook\AddressBook.csproj (用时 234 ms)。
已还原 C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj (用时 343 ms)。
C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.Conformance\Google.Protobuf.Conformance.csproj : warning NU1603: Google.Protobuf.Test 依赖于 NUnit3TestAdapter (>= 4.2.2),但找不到 NUnit3TestAdapter 4.2.2。 已解决 NUnit3TestAdapter 4.3.0 的近似最佳匹配。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.sln]
C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.Test\Google.Protobuf.Test.csproj : warning NU1603: Google.Protobuf.Test 依赖于 NUnit3TestAdapter (>= 4.2.2),但找不到 NUnit3TestAdapter 4.2.2。 已解决 NUnit3TestAdapter 4.3.0 的近似最佳匹配。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.sln]
已还原 C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.Conformance\Google.Protobuf.Conformance.csproj (用时 3.25 sec)。
已还原 C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.Test\Google.Protobuf.Test.csproj (用时 3.25 sec)。
C:\Users\HQ\Desktop\protobuf-main\csharp>dotnet pack -c Release src/Google.Protobuf.sln -p:ContinuousIntegrationBuild=true || goto :error
用于 .NET 的 Microsoft (R) 生成引擎版本 17.0.0+c9eb9dd64
版权所有(C) Microsoft Corporation。保留所有权利。
正在确定要还原的项目…
C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.Conformance\Google.Protobuf.Conformance.csproj : warning NU1603: Google.Protobuf.Test 依赖于 NUnit3TestAdapter (>= 4.2.2),但找不到 NUnit3TestAdapter 4.2.2。 已解决 NUnit3TestAdapter 4.3.0 的近似最佳匹配。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.sln]
C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.Test\Google.Protobuf.Test.csproj : warning NU1603: Google.Protobuf.Test 依赖于 NUnit3TestAdapter (>= 4.2.2),但找不到 NUnit3TestAdapter 4.2.2。 已解决 NUnit3TestAdapter 4.3.0 的近似最佳匹配。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf.sln]
所有项目均是最新的,无法还原。
C:\Users\HQ\.nuget\packages\microsoft.build.tasks.git\1.0.0\build\Microsoft.Build.Tasks.Git.targets(24,5): warning : 找 不到具有带目录“C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf”的工作目录的存储库。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
C:\Users\HQ\.nuget\packages\microsoft.build.tasks.git\1.0.0\build\Microsoft.Build.Tasks.Git.targets(47,5): warning : 找 不到具有带目录“C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf”的工作目录的存储库。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
C:\Users\HQ\.nuget\packages\microsoft.sourcelink.common\1.0.0\build\Microsoft.SourceLink.Common.targets(52,5): warning : 源代码管理信息不可用 - 生成的源链接为空。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
C:\Users\HQ\.nuget\packages\microsoft.build.tasks.git\1.0.0\build\Microsoft.Build.Tasks.Git.targets(24,5): warning : 找 不到具有带目录“C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf”的工作目录的存储库。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
C:\Users\HQ\.nuget\packages\microsoft.build.tasks.git\1.0.0\build\Microsoft.Build.Tasks.Git.targets(47,5): warning : 找 不到具有带目录“C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf”的工作目录的存储库。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
C:\Users\HQ\.nuget\packages\microsoft.sourcelink.common\1.0.0\build\Microsoft.SourceLink.Common.targets(52,5): warning : 源代码管理信息不可用 - 生成的源链接为空。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
C:\Users\HQ\.nuget\packages\microsoft.build.tasks.git\1.0.0\build\Microsoft.Build.Tasks.Git.targets(24,5): warning : 找 不到具有带目录“C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf”的工作目录的存储库。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
C:\Users\HQ\.nuget\packages\microsoft.build.tasks.git\1.0.0\build\Microsoft.Build.Tasks.Git.targets(47,5): warning : 找 不到具有带目录“C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf”的工作目录的存储库。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
C:\Users\HQ\.nuget\packages\microsoft.sourcelink.common\1.0.0\build\Microsoft.SourceLink.Common.targets(52,5): warning : 源代码管理信息不可用 - 生成的源链接为空。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
Google.Protobuf -> C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\bin\Release\netstandard1.1\Google.Protobuf.dll
Google.Protobuf -> C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\bin\Release\net45\Google.Protobuf.dll
Google.Protobuf -> C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\bin\Release\netstandard2.0\Google.Protobuf.dll
C:\Users\HQ\.nuget\packages\microsoft.build.tasks.git\1.0.0\build\Microsoft.Build.Tasks.Git.targets(24,5): warning : 找 不到具有带目录“C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf”的工作目录的存储库。 [C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\Google.Protobuf.csproj]
已成功创建包“C:\Users\HQ\Desktop\protobuf-main\csharp\src\Google.Protobuf\bin\Release\Google.Protobuf.3.24.0.nupkg” 。
C:\Users\HQ\Desktop\protobuf-main\csharp>goto :EOF
PS C:\Users\HQ\Desktop\protobuf-main\csharp>
Step 4: Copy the library files to your project
Library file path: protobuf-main\csharp\src\Google.Protobuf\bin\Release\net45
Put it in the ThirdParty folder, indicating the third-party library
Step 5: Test using protoc.exe to generate code
With the Google.Protobuf library, we must use it to serialize and deserialize data, how to use it? The premise of this is that we need to use protoc.exe to help us generate good C# code.
Open this protobuf-main\csharp\generate_protos.sh
It can be seen that some proto files are entered in the code, and finally, a C# file with the same name will be generated according to the configuration path.
but! ! ! I want to test the generation, and found that there is no protoc.exe file in the source code, then generate a hammer, and the operation will definitely report an error, "Unable to find protocol buffer compiler."
In other words, the following code sets the protoc compiler, none of which exists. . .
First go to github to download the protoc tool
Protoc official website download address https://github.com/protocolbuffers/protobuf/releases Download the source code tool zip compressed package, after pressurization, there is an executable program protoc.exe in the bin folder
Copy the protoc.exe file to the protobuf-main\csharp path, as shown in the figure
Let's start modifying the script that generates the file, let the sh code recognize the protoc.exe file and generate a test file for us
Open the generate_protos.sh file with notpad++
In the PROTOC_LOCATIONS variable, add "csharp/protoc.exe", this path is relative to the root path, so the csharp folder path is added in front.
Why test whether it can be generated, we delete all the pb.cs files in the target folder
Then execute the generate_protos.sh file
After testing, the generation process is normal.
Step 6: Start using protobuf
Copy the entire folder of the test code protobuf-main\csharp\src\Google.Protobuf.Test.TestProtos to Unity
Eliminate obj folder and csproj file
The protobuf-main\csharp\src\Google.Protobuf.Test.TestProtos folder is placed in Unity's Scripts folder
We randomly find a proto from the source folder for testing
Then randomly select a message in line 208
Create a new script TestProto.cs in Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Google.Protobuf;
using Google.Protobuf.TestProtos.Proto2;
public class TestProto : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
ForeignMessage message1 = new ForeignMessage();
message1.C = 1;
message1.D = 2;
byte[] result = message1.ToByteArray();
string messag1Byte = "";
for (int i = 0; i < result.Length; i++)
{
messag1Byte += result[i] + "|";
}
Debug.LogError(messag1Byte);
ForeignMessage message2 = ForeignMessage.Parser.ParseFrom(result);
Debug.LogError("message2.C: " + message2.C);
Debug.LogError("message2.D: " + message2.D);
}
}
View print results
Summarize:
This article downloaded two things, namely the Google.Protobuf source code and the C# version of the protoc.exe generation tool
First use the Google.Protobuf source code to generate a DLL and throw it into Unity
Use the protoc.exe tool to generate a C# script with the same name as the proto definition file
Then throw these generated C# scripts into the project, and you can use these scripts to serialize and deserialize class objects.