Using Protobuf in Unity5

Source: Internet
Author: User
Tags serialization

As a result of many factors, the project to U3D development, and the project team so far no senior Unity development programmer, can only stones. Our network communication data uses the PROTOBUF format, (the PROTOBUF format analysis has been analyzed in detail in the previous note). Briefly describe the current project development environment and plug-in usage:

Development script: C # (. Net 2.0) because I have no C # Foundation with another colleague, I have a basic plan to learn C # in my spare time. (Why not JS or boo, in detail assets Unity basically most plugins are written in C #, Boo is the first to listen)

Development tools: Discard the official mono using VS2012, mainly using the Unityvs plugin, note that this plugin is now free. Portal

GUI: There are Ugui and Ngui and GUI three choices, the current election is Ngui, the main reason is the Ngui of the textbook online grab a lot, because of the sudden change of engine, has not much time to see that more useful. (many things are always full of helplessness)

Network module: UNITY5 provides a set of net components that, after spending two days looking at several of its components, is not enough to meet the requirements of our project. So the decision to use. NET comes with the socket itself. (It took about three days)

Use of Protobuf

There are currently two C # libraries on Google code: protobuf-net protobuf-csharp-port

found that protobuf-net to cross-platform support seems to be slightly better than Protobuf-csharp-port, in the community evaluation is also better, so the use of protobuf-net. The basic steps for using protobuf-net are: writing proto files

For a pear, save it as Demo.proto,message grammar please learn by yourself

/
 * * Package demo;  
 *
 * Message people {  
 *    Required String name = 1;  
 *    required Int32 id = 2;  
 *    Required String email = 3;  
 * }
 */
Using Protogen.exe to generate C # source files

The basic syntax is

-I: Specify Proto file can specify multiple
//-o: Make output file path and file name
//-ns: Specify the source namespace, if not set the default is Proto file name namespace
// If there is a package XXX parameter in the proto file, the mandatory namespace name is xxx
protobuf-net\protogen\protogen.exe-i:demo.proto-o:pbmessage\ Pbmessage.cs-ns:pbmessage
Source file generation DLL Dynamic link library

The basic commands are:

c:\windows\microsoft.net\framework\v4.0.30319\csc.exe/noconfig/nowarn:1701,1702/nostdlib+/
errorreport: Prompt/warn:4/define:trace/reference:c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll/
Reference :p rotobuf-net.dll/reference:c:\windows\microsoft.net\framework\v2.0.50727\system.dll/debug:pdbonly/
filealign:512/optimize+/out:obj\release\pbmessage.dll/target:library/utf8output PBMessage.cs Properties\ AssemblyInfo.cs

If you do not want to use the command line to resolve, you can also directly use the VS Create C # Library project, and then add the source files to the project, F5 generation. Get PBMessage.dll in this step (DLL file name can be changed arbitrarily) AssemblyInfo.cs as the version information file, Can be found in the download file at the end of this article, or you can create a precompiled serialization library on your own to get the PBMessage.dll from the previous step

The command is as follows:

Protobuf-net\precompile\precompile.exe Pbmessage.dll-o:pbmessageserializer.dll-t:pbmessageserializer

After the previous steps, we got three dll:protobuf-net.dll from the Protobuf-net Toolkit, a dynamic link library that was generated from different platforms and PBMessage.dll. PBMessageSerializer.dll Precompiled Serialization Library

Place these three libraries under the Assets\plugins\protobuf folder in the Unity project, and create them yourself without this folder. This completes the entire configuration work, followed by the code. The following methods are available:

/* * Package demo;  
 * * Message People {* Required String name = 1;  
 * Required Int32 id = 2;  
 * Required String email = 3; *} */public void Writeprotobuf () {demo. People proto = new demo.
    People ();
    Proto.email = "rectvv@gmail.com";
    Proto.id = 10086;

    Proto.name = "Rect"; Serialization using (MemoryStream ms = new MemoryStream ()) {new Pbmessageserializer ().
        Serialize (MS, Proto); Byte[] pbuffer = Ms.
    ToArray ();
    }} public void Readprotobuf (byte[] Pbuffer,int nSize) {if (null = = Pbuffer | | 0 >= nSize) {return; }//deserialization using (MemoryStream ms = new MemoryStream (pbuffer, 0, NSize)) {demo. People proto = new Pbmessageserializer (). Deserialize (MS, NULL, typeof (demo. People) as demo.
    People; }
}

The code is very simple.

The whole process was integrated into a bat command: The portal, just put the proto file in the root directory, and then run the BUILD.bat batch. The final build DLL is in the bin directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.