简体   繁体   中英

ProtocolBuffer inside Unity3D targeting iOS

I'm trying to use Marc Gravell's protobuf-net C# protocol buffer implementation, in a Unity3D project targeting iOS devices.

I managed to create a C# file (let's call it MyFile.cs ) from a protocol buffer file MyFile.proto using protogen .

Once imported the file and protobuf-net.dll (I'm using the dll inside unity folder), I can successfully deserialize a previously serialized file (I'm doing the serialization from python code and all works fine):

MyType myType;
using (var file = File.OpenRead("Assets/MyTypeBin")) 
{
  myType = ProtoBuf.Serializer.Deserialize<MyType>(file) ;
}

The above solution works fine on the simulator, but fails when targeting the iOS device (es.ipad).

I tried to switch from the unity/protobuf-net.dll to CoreOnly/ios/protobuf-net.dll but with the last one it fails to compile.It seems that Deserialize method is not declared inside the core dll:

error CS0117: ProtoBuf.Serializer' does not contain a definition for Deserialize'

I've read this article , but it seems quite outdated. It refers to an old implementation of protobuf-net . I hope something changed in the meanwhile.

My question are:

  1. is it possible to use protobuf-net r602 targeting an iOS device from Unity3D , without

    creating a library dll (assembly) of the model classes you want to serialize/deserialize

    like suggested in the linked blog post? I'd like to have a faster workflow, possibly importing directly C# file generated with protogen inside a Unity3D project

  2. If yes, how? which dll do I need to reference? Which are the steps to make the above snippet to work again on a iOS device?

I believe this is the solution:

On iOS, there is a problem with Apple does not allow JIT compile on runtime which prevents protobuf usage. It can be solved with Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes"); before any protobuf use, but it works only on .NET 4.6. (I tried on 3.5 and the error about JIT still comes up)

https://github.com/5argon/protobuf-unity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM