Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Network Transport Protocol protobuf in Unity3d

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of the network transmission protocol protobuf in Unity3d. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Google Protocol Buffer (Protobuf for short) is a mixed language data standard within Google, which has more than 48162 message format definitions and more than 12183 .proto files. They are used in RPC systems and continuous data storage systems.

Protocol Buffers 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. Can be used in communication protocols, data storage and other fields of language-independent, platform-independent, extensible serialized structural data format. API in C++, Java and Python is currently available.

Google protocol buffers has a wide range of applications, but for unity3d developers, the greatest use of protobuf is that it can be used to transfer data content when writing online games. For example, in online games, players log in and need to transmit the user name account and password password to the server. The server verifies successfully and sends the player a piece of data (such as int an or enum a). After receiving the data, the player will judge according to the value of a. A login 1 indicates that you can log in, and a password 0 indicates that you cannot log in. In this process, Protobuf is used to transfer network data between the client and the server.

If you have not used protobuf, download demo, and then follow this article to practice, then the simple use of protobuf should be no problem; if you have been using protobuf, but has been written by someone else, then study this article demo, you can understand the use of protobuf in unity3d.

Github address of demo: single010203/Unity-Network-Protobuf

After the download is completed, decompress to get the engineering structure diagram shown in figure 1-figure 2 below:

Fig. 1 networktest engineering structure diagram

Fig. 2 unity3d drawing

The project realizes the process from user login to room entry in online games, from user login-> user entry hall-> user entry room to user exit room-> user exit hall-> user exit. A total of 6 operations represent six message transfers between the client and the server. Next, we follow the steps to practice this process.

1 write .proto file

Open the protoToCs folder shown in figure 1, create a new EnterLiveHallMsg.proto file in this folder, and enter the contents shown in figure 3 below:

EnterLiveHallReq sends login request data structure on behalf of the user

EnterLiveHallEsp represents the result returned by the server.

Figure 3. Writing a .proto file

The relevant proto files in the folder have been written, of course, you can customize and write as many as you like.

2 generate .cs class

Find the batch file in the protoToCs folder: protoToCs.bat, which invokes the protogen.exe tool, which generates the corresponding .cs class files based on all the proto files in the current folder.

Double-click the "protoToCs.bat" file, pop up the command line tool, after the completion of the execution, we just wrote the EnterLiveHallMsg.proto file next to the EnterLiveHallMsg.cs file, the file is automatically generated, can not be changed, so we completed the automatic generation of the cs class operation.

Figure 4 generates the cs file.

3 data import into the project

Copy the EnterLiveHallMsg.proto you just wrote and the automatically generated EnterLiveHallMsg.cs into the project, as shown in figure 5. At this point, we can call the data structure in EnterLiveHallMsg.cs in the project to write the function.

Fig. 5 Network data in the project

4 how to use the generated cs file

The three most important classes in the functional level of the project are SendPacket.cs/ReceivePacket.cs/StartLiveRoom.cs.

The internal logic function is relatively simple, which only implements a single example of a customer who logs in to the hall and enters the room.

Figure 6 Login Hall Operation on "sample Analysis of Network Transport Protocol protobuf in Unity3d" this article ends here. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report