- Home /
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
Answer by GeraldOBBI · Dec 14, 2011 at 07:06 PM
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.
So I guess protobuf-net thoughtfully added thread-safety and broke the Unity web player in the process. We should get in touch with the developer to ensure the latest version can be made compatible with the web player.
protobuf-net v2 r580 has a unity .dll "specific to unity (avoids missing "interlocked" methods)". there is a new precompiler too. very cool stuff!
Answer by marc.gravell · Oct 12, 2012 at 11:58 AM
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.
Answer by riffraff · Mar 19, 2012 at 08:38 PM
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.
Hi, I've gotten further using PLAT_NO_INTERLOC$$anonymous$$ED, but I'm still getting exceptions from using floats on webplayer builds.
VerificationException: Error verifying ProtoBuf.ProtoWriter:WriteSingle (single,ProtoBuf.ProtoWriter): Invalid type (Float64) at stack for conversion operation. Numeric type expected at 0x0011 at $$anonymous$$ySerializer.Write (.SerializableQuat , ProtoBuf.ProtoWriter ) [0x00000] in :0
Any ideas? Thanks in advance.
I got it working when I also set it to FEAT_SAF$$anonymous$$ ( Along the way, I had set the project to NET 2.0 only and removed binary formatter and xml defines )
Answer by riffraff · Mar 19, 2012 at 08:38 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062046im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Webplayer deserialization fails 1 Answer
Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers
Web Player Serialization Broken 0 Answers
Sending binary data causes unexpected problems on server. 1 Answer
Help with Binary serialization/de-serialization of List items and the WEBPLAYER 1 Answer