How to use Google Protobuf in unity

Preparation

unity 2019.4.x
Protobuff 3.20.3

What is Protobuf used for?

Protobuf is the abbreviation of Protocol Buffers. It is a data description language developed by Google. It is a lightweight and efficient structured data storage format that can be used for structured data serialization, or serialization. It is very suitable for data storage or RPC data exchange format. A language-independent, platform-independent, and extensible serialized structured data format that can be used in communication protocols, data storage and other fields. proto2 provides APIs in three languages: C++, Java, and Python. All languages ​​supported in proto3!

Mainly used for (data) communication protocols, data storage, etc.

Advantages of Protobuf

Protobuf is like XML and Json, but it is smaller, faster, and simpler. You can define your own data structure and then use the code generated by the code generator to read and write this data structure. You can even update data structures without redeploying the program. Use Protobuf to describe the data structure once, and your structured data can be easily read and written in a variety of different languages ​​or from a variety of different data streams.

It has a great feature, that is, it has good "backward" compatibility, and people can upgrade the data structure without breaking the deployed programs that rely on the "old" data format. In this way, your program does not have to worry about large-scale code reconstruction or migration caused by changes in message structure. Because adding a new field in the message will not cause any changes to the already published program.

Protobuf has clearer semantics and does not require anything like an XML parser (because the Protobuf compiler will compile the .proto file to generate the corresponding data access class to serialize and deserialize Protobuf data).

There is no need to learn a complex document object model to use Protobuf. Protobuf's programming model is friendly and easy to learn. At the same time, it has good documentation and examples. For people who like simple things, Protobuf is more attractive than other technologies.

Download and install

Download 3.20.3 Google Protobuff download here

Insert image description here
Insert image description here
After downloading, unzip it to your fixed folder!

Main basic types (others can be checked by yourself)

.prototype java type C++ types Remark
double double double
float float float
int32 int int32 Use edge-length encoding. It is not efficient enough when encoding negative numbers. If the field may have negative numbers, use sint32
sint32 long int32 Use edge-length encoding. Signed integer value. More efficient than int32 when encoding
sint64 long int64 Use edge-length encoding. Signed integer value. More efficient than int62 when encoding
string String string A string must be UTF8 or 7-bit ASCII encoded text
bool boolean bool

special fields

English Chinese Remark
enum Enumerations (numbers starting from zero) work by specifying a "defined sequence of values" for the field. enum Sex{Man=0;Women=1}
message Message body, class in C# messge User{}
repeated Array or collection, C# List OR [ ] repeated User user= 0
// Comment //Comments here
extend Expand extend User{}
package Package names Namespaces in C# to prevent naming conflicts

Write .proto file

binWe find the folder in the folder we just unzipped
Insert image description hereand create a new txt text name Person.proto. Remember to change the suffix .txt to .proto
and enter the code.

syntax="proto3";//指定了正在使用proto3语法,如果没指定编译器默认使用proto2语法
package TestGoogleProtoBuff;//等于C#中命名空间

message personInfo
{
	string name=1;
	int32 age=2;
	int64 money=3;
	message PhoneNumber{
		string number=1;
		PhoneType type=2;
	}
	repeated PhoneNumber phone=5;
}

enum PhoneType{
	HOME=0;
	WORK=1;
	MOBILE=2;
}

Then create it in this folder ExProtProto.bat, enter the command and save it.

Insert image description here
Double-click and we find that the file ExProtProto.batis generatedPerson.cs
Insert image description here

Use in Unity

Then we open unity, create a new folder Proto, and place the just-generated Person.cscode in the Proto folder.

Insert image description here
At this time, the unity console will become red in large numbers! Person.csThis is caused by missing references.
We open it with vs Person.cs, click on the top menu bar Tools>NuGet Package Manager>Manage the NuGet management package
Insert image description here
version of the solution. You must select the corresponding version we downloaded at the beginning, otherwise it will be incompatible! Choose 3.20.3 here. Then click Install. After the installation is completed, we open the root directory of the unity project.
Insert image description here

Respectively put
.\Packages\Google.Protobuf.3.20.3\lib\netstandard2.0\Google.Protobuf.dll
.\Packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll
.\ Packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll
.\Packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe .dll
These dll files are placed in the Assets/Plugins folder, wait for Unity to recompile, and then clear the console to eliminate the error.

Note: The dll file in netstandard2.0 is chosen here because the api version in unity2019.4.x corresponds to .NET Standard 2.0. Different versions of unity need to find different net versions to correspond to, otherwise there will be conflicts, generally higher versions The unityapi version is netstandard2.1
Insert image description here

Insert image description here

Then start writing serialization and deserialization tools. In fact, it is similar to the Json that everyone is familiar with. In network communication, the front-end serializes the entity object data into a string in Json format and passes it to the back-end. Then the received back-end data is Deserialize it into an object that can be used by the front end. Note that proto will serialize the instance into binary data format!

ProtoBufffer.csWe create a new script in unity

using Google.Protobuf;
public class ProtoBufffer
{
    
    
    public static byte[] Serialize(IMessage message)
    {
    
    
        return message.ToByteArray();
    }

    public static T DeSerialize<T>(byte[] packet)where T:IMessage, new()
    {
    
    
        IMessage message = new T();
        try
        {
    
    
            return (T)message.Descriptor.Parser.ParseFrom(packet);
        }
        catch (System.Exception e)
        {
    
    
            throw;
        }
    }
}

After creating a Test.csscript to test the functionality, drag the script onto the main camera.

using TestGoogleProtoBuff;
using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        personInfo info = new personInfo();
        info.Name = "ys";
        info.Age = 50;
        info.Money = 9999;
        info.Phone.Add(new personInfo.Types.PhoneNumber {
    
     Number = "12123",Type=PhoneType.Home});
        byte[] msg = ProtoBufffer.Serialize(info);
        Debug.Log(msg.Length);

        personInfo deinfo  = ProtoBufffer.DeSerialize<personInfo>(msg);
        Debug.Log("姓名:"+deinfo.Name);
        Debug.Log("年龄:"+deinfo.Age);
        Debug.Log("资产:" + deinfo.Money);
        Debug.Log($"{
      
      deinfo.Phone[0].Type}的电话号:{
      
      deinfo.Phone[0].Number}");
    }
}

Run unity and view console messages. Serialization and deserialization successful!
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_43298513/article/details/135462197