Unity fails to load protobuf assembly for unclear reasons

I am working on a game that needs to share code with a separate .NET program. Therefore, a significant part of the game’s code is in an external assembly (the “core” assembly) that is separately compiled with VS2015 and included in Unity’s asset structure as well as referenced by the other program. This has worked fine up to now, without any strange problems.

Now, I want to start using protobuf-net for serialization. I’m using the “full/unity” protobuf assembly. Because this may eventually have to run on iOS, I am precompiling the protobuf type model as a separate assembly which is also referenced by the core assembly. Unit tests run through VS2015 confirm that my protobuf integration is working, that the precompiled serializer is working as intended and producing consistent results, etc.

However, when I include the core assembly with the precompiler serializer in Unity, Unity will no longer load the core assembly, which renders the entire game inoperational. Furthermore, trying to start the game in this state just plain crashes Unity.

The error Unity spits out is, as usual, not helpful:

FileNotFoundException: Could not load file or assembly 'DomCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

It’s just a dozen lines of that followed by errors from inside Assembly-CSharp, which are cascade errors resulting directly from the failure to load the assembly in the first place. There is no information regarding which dependency fails to load (although it almost certainly is the precompiled serializer), nor what the reason for the failure is.

I have confirmed that removing the precompiled serializer and using the reflecting runtime type model works as expected inside Unity, but this is not an acceptable solution because the runtime type model will not work on iOS. Additionally, remember that the precompiled serializer does work from inside VS2015, just not in Unity.

All involved assemblies are compiled against .NET 2.0. The only system assemblies referenced are mscorlib (2.0), System (2.0), and System.Core (3.5). This is the same regardless of whether I use the precompiled serializer or not.

I have gone through the various assemblies’ dependencies with ILSpy to confirm that there are no strange or missing dependencies, and it confirms the above regarding the expected versions of the involved assemblies.

As the message presented by Unity is unhelpful, and nothing about the assembly itself seems wrong, and nor does it fail when run through VS, I don’t know how to proceed with this issue.

I am using Unity 5.1.2f1 Personal.

Hi, although not 100% sure, I think this is a unity mono bug which is caused by too many ProtoMember or maybe too many class members of the ProtoContract classes in total.

To get rid of the error, please try to make the ProtoContract classes less members in total.