Cross-platform use protobuf-net

Source: Internet
Author: User

Protobuf is now more and more used in mobile games, and its serialization and deserialization are very handy for developers, and how do we use unity development? Here's a summary for you:

The first step: Copy the "protobuf-net" folder to unity.

: https://github.com/mgravell/protobuf-net

I will be available to you in the back for download.

The second step: Create a new file smcs.rsp, the content is-unsafe, note is not underlined.

Step three: If Unity also reported wrong, we need to set the project to. Net 2.0 subset.

By setting up our programs above, you can run both on Android and iOS.

Another solution: Can not use unsafe, in the playersettings srcripting Define symbols plus macro Feat_safe can also be compiled in Xcode through.

If you change to. Net 2.0 also reported error, you can add GMCS.RSP content to the same-unsafe as above.

How we use PROTOBUF in our programs, here's an example:

For example, we need to transfer messages between the server and the client, so we can define:

Enumeration structure:

Enum Enumlogingameresult
{
logingame_success = 0;
Logingame_error = 1;
}

Message structure definition

Message Logingame
{
Required UInt32 Uaid = 1; Account ID
Optional Enumchartype type = 2;
};

Message structure, required meaning must be defined, optional is optional, that is, when we use this message, optional can be ignored. And one is repeated. This is the list table, which is an array.

The function of the corresponding file can be called by the import "file name" between messages.

For example, I have defined an enumeration value or a message in the Common.proto file,

I need to write the import "Common.proto" in the Login.proto, and then I can use the message in the Common.proto or the enumeration value in the Login.proto.

The benefit of this definition is that C + + or C # can be used together.

How to use it, we can do it by defining batches, such as generation. CC and. H. The batch process is as follows:

@echo off

Set tool=. \tools

rem===============

REM Support

Set Proto=.\common\common.proto

%tool%\protoc.exe--proto_path=.\common--cpp_out=.\common%proto%

Pause

The batch process for generating C # files is as follows:

@echo off
Set tool=. \3party\protobuf-net\net

REM ===============================================
REM Support
Set Proto=login.proto
%tool%\protogen.exe-i:%proto%-o:%proto%.cs-q

Pause

The above is about the use of protobuf, I have its source code uploaded to the 51CTO Download Center, if necessary, you can download, directly put it in unity inside the use.

This article is from the "Kaiyukan mobile" blog, so be sure to keep this source http://jxwgame.blog.51cto.com/943299/1596354

Cross-platform use protobuf-net

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.