Using Protobuf on iOS Android devices (using DLL mode)

Source: Internet
Author: User

From: http://game.ceeger.com/forum/read.php?tid=13479

If your project can run in. Net 2.0 subset mode, see the methods in this post.

Address: http://game.ceeger.com/forum/read.php?tid=14359&fid=27

If you can only run under. Net 2.0, you will continue to look down.

=============================================================

Protobuf is a Google -Customized protocol that makes it easy to serialize and deserialize data. ( supports multiple inter-platform communication )

It can be used for local caching of data

and the transmission of network protocols, and so on.

To understand the protobuf protocol See the following two URLs:

http://code.google.com/p/protobuf/

Https://developers.google.com/protocol-buffers/docs/tutorials

Unity has two protobuf libraries that can be used protobuf-csharp-port, protobuf-net (the latter is more efficient )

Two libraries can work correctly on Android , but the following exception appears on iOS :

ExecutionEngineException:

Attempting to JIT compile method

JIT means compile at run time, while iOS is AOT (full) compilation, so any library that is JIT -only will not work on iOS .

To learn about the JIT and the AOT, see the following URLs :

Jit:http://en.wikipedia.org/wiki/just-in-time_compilation

Aot:http://en.wikipedia.org/wiki/aot_compiler

Http://www.mono-project.com/AOT

After an attempt, you can use protobuf-net precompilation to run on iOS and Android devices

Next, follow the steps, and finally attach the test project.

First, download protobuf-net and configuration

Go to Http://code.google.com/p/protobuf-net/downloads/list to download the latest release version of Protobuf-net (the latest version of this tutorial is r668)

After downloading to a directory of your hard drive, it is best to add precompile\precompile.exe and Protogen\protogen.exe two files to the environment variable, then easy to use.

We also need three files in the Coreonly\ios, which will be used later, and will eventually be put into unity engineering.

Second, generate CS code

Using the Protogen command line to generate code (both Precompile and Protogen can only be run under Windows, it may be possible to run this exe via mono on a Mac, but it has not been tried.) )

examples of use are as follows :

Protogen-i:test1.proto-i:test2.proto-i:test3.proto-o:output.cs-ns:com.fbmly.model

-I is an input file that can have multiple

-O output of CS file, can only have one: If-I has more than one will generate all the code into this CS file

The-ns namespace is best used if the default namespace that is not used every time the build is proto is the file name.

In general, just make all the data structures into a CS (or else use multiple-I, or write all the PROTOBUF definitions in a proto file), so that later operation is more convenient.

After this step you get the CS file after the build.

Third, compile DLL library

Use the MonoDevelop tool to compile the CS file generated in the previous step into a DLL library.

1. Create a new project (file->new->solution)

2. In the popup dialog box, select C # and select Library.

3. Fill in the name of the project below (this name is the name of the generated DLL, so be good)

4. Click on the forward (then one more time OK)

5. Delete the default generated MyClass.cs file, and if necessary, fill in the AssemblyInfo.cs with some copyright information.

6. Add the previous step to the project with the CS file generated by Protogen (right-click on the left project name to add)

7. Add Protobuf-net Reference Library

Click on the project's References->edit References

Then select the DLL library under the Coreonly\ios, double-click the library file to add to the right

Finally check the references inside if there is Protobuf-net.dll is correct.

8. When you are done, click Build->build All to generate the DLL in the current project.

Do not forget the choice of debug and release, after the test should be re-release and then compile once.

9. The bottom left tip is successful in generating DLL files in the current project directory (I'm TestModel.dll here)

This DLL holds all of your data structures.

Iv. Precompiled Serialization Library

The DLL file that generated the data in the previous step requires the DLL file in the previous step to generate a specially serialized DLL file.

The first thing is to ensure that the TestModel.dll and compile-time libraries in the same directory (the DLL file directory generated in the previous step, will automatically copy the Protobuf-net library files, so just remember this is OK.) For example, if you copy to another directory to do this, it will be important.

Then open the command line and navigate to the Bin\Debug or Bin\release directory under the DLL's project directory

Then execute the following command ( modified to the name you set ):

Precompile Testmodel.dll-o:protobufserializer.dll-t:com.fbmly.protobufserializer

TestModel.dll is the DLL file that was generated in the previous step,-O is the generated file name-T is the class name of the serialized class generated in ProtobufSerialize.dll supports the namespace, and you can write the class name directly without the namespace.

If there is an all-done hint at the end, it means that the build succeeded.


Copy Code

12345678910 D:\protobuf-net\Precompile\Model>precompile TestModel.dll -o:ProtobufSerializer.dll -t:com.fbmly.ProtobufSerializerprotobuf-net pre-compilerDetected framework: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727Resolved C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dllResolved C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dllResolved protobuf-net.dllAdding Fbmly1.Fbmly...Compiling com.fbmly.ProtobufSerializer to ProtobufSerializer.dll...All done

V. Testing in Unity

Copy the TestModel.dll, ProtobufSerializer.dll, and protobuf-net libraries that were generated in the third and fourth steps to Unity's project.

Now it's time to start testing.

Test code:


Copy Code

1234567891011121314151617181920212223242526 voidOnGUI( ){    if( GUILayout.Button ( "Test" , GUILayout.Width ( 200f ) , GUILayout.Height ( 200f ) ) )    {        Fbmly data = newFbmly ( );        data.mmm = 10;        ProtobufSerializer serializer = newProtobufSerializer ( );        //Serialize        byte[] buffer = null;        using( MemoryStream m = newMemoryStream ( ) )        {            serializer.Serialize ( m , data );            m.Position = 0;            intlength = (int)m.Length;            buffer = newbyte[length];            m.Read(buffer, 0 ,length);        }        Fbmly newData = null;        //Deserialize        using ( MemoryStream m = newMemoryStream ( buffer ) )        {            newData = serializer.Deserialize ( m , nulltypeof( Fbmly ) ) asFbmly;        }        Debug.Log ( "newData.mmm="+ newData.mmm );    }}

The test project has been tested on TOUCH4 and the project is set to. Net 2.0 Subset

If you're having errors on your real machine, you might want to set it up to. Net 2.0 to try.

This approach has been verified in the game project, and complex data is no problem (the game project is defined with more than 500 proto), both iOS and Android are available.

Finally attached to the project package: it is attached to a tool class, can be serialized when the use of the time only need to modify it, because it maintains a serialization class of the Singleton.

Protobuftest.unitypackage

or download it here:

http://pan.ceeger.com/viewfile.php?file_id=1824

(go) Use Protobuf on iOS Android devices (using DLL mode)

Related Article

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.