Does protobuf-net work in the Unity web player?

We recently started using the protobuf-net implementation of Google Protocol Buffers for binary serialization of data going from our Unity client to our game server. Protobuf works great for us in the editor and in standalone builds, and this thread indicates it can be made to work on iOS too, but we are having trouble with the web player.

Before we dive deeper, has anyone been able to use protobuf-net in the Unity web player?

If we know it can work then we’ll keep trying to sort it out, but anyone knows it can’t work then we’d like to give up quickly!

Here’s the error we’re getting – BufferPool.GetBuffer is internal to the protobuf-net library, and the method that’s causing the trouble is a call to Interlocked.Exchange, which is a standard .NET threading call. Quite possibly the Unity implementation of System.Threading for the web player is incomplete. We could start massaging protobuf-net to get rid of these calls, but would prefer not to if there is a better alternative.

MethodAccessException: Attempt to access a private/protected method failed.
at System.Security.SecurityManager.ThrowException (System.Exception ex) 0x00000 in :0 
at ProtoBuf.BufferPool.GetBuffer () 0x00000 in :0 
at ProtoBuf.ProtoWriter..ctor (System.IO.Stream dest, ProtoBuf.Meta.TypeModel model, ProtoBuf.SerializationContext context) 0x00000 in :0 

Hi all; I’m the author of protobuf-net, and I’d like to clarify:

Firstly, there is (or maybe at some point, was) an annoying glitch where-by the Interlocked methods are (/were) not public, which is where the unavailable method exceptions came from; the download from google-code includes a “unity” build which automatically compensates for this (and doesn’t use unsafe code, etc), so as long as you use the “unity” build, you should be fine.

If any other problems occur, I will do my best to investigate, but I’m not a unity expert (plus I don’t have a license, so the “pro” features are out of my reach unless the unity folks donate me a license for support purposes). I do, however, hear that it generally works fine.

Edit: it was brought to my attention that the “unity” build was not including the “no unsafe code” symbol, which caused problems on web-player. This has been fixed, with r594 now available on google-code as a pre-built binary that is confirmed working.

It appears that protobuf-net introduced the Interlocked.Exchange in order to create a pool of buffers. Earlier versions of proto-buf simply return a new byte with the required size.

We were able to get proto-buf working with our webplayer build using the .Net 2.0 version of the dll at revision proto-buf r282.

Simplest solution is to compile recent version of protobuf-net with flag PLAT_NO_INTERLOCKED. I tested it under Unity 3.5 + WebPlayer and it is working fine.

I can not made revision 282 to work with Unity3.5, so I took the most recent version of proto-buffers from svn, and compiled it with flag PLAT_NO_INTERLOCKED. It is working fine with Unity3.5 and WebPlayer.