سؤال

Here is my code:

using ProtoBuf;

[ProtoContract]
[ProtoInclude(500, typeof(SampleClassDrv))]
public class SampleClass
{
    [ProtoMember(1)] public int theInt;
    [ProtoMember(2)] public string[] items;
    public SampleClass(){}
    public SampleClass(int c) {this.theInt = c;}
}

[ProtoContract]
public class SampleClassDrv : SampleClass
{
    [ProtoMember(1)] public int theOtherInt;
    public SampleClassDrv(){}
    public SampleClassDrv(int b):base(1){this.theOtherInt=b;}
}

To compile my DLL I run the following code:

RuntimeTypeModel rModel = TypeModel.Create();
rModel.AllowParseableTypes = true;
rModel.AutoAddMissingTypes = true;

rModel.Add(typeof(SampleClass), true);
rModel.Add(typeof(SampleClassDrv), true);
rModel.Compile("MySerializer", "MySerializer.dll");

Finally I should be able to initialize by RuntimeTypeModel from the dll like so:

MySerializer serializer = new MySerializer();
serializer.Serialize(stream, object);

But Unity throws the following exception

Internal compiler error. See the console log for more information.
[...]
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

Interestingly enough if I go back and remove the line

[ProtoMember(2)] public string[] items; 

It works as expected...

It is also worth noting the RunTimeModel works as expected if used after adding the classes instead of attempting to use dll.

My environment:

  • Unity3D 4.3.4
  • Protobuf-net r668
  • using protbuf-net.dll in Full/unity

I would greatly appreciate if someone could point out the error in my ways.

EDIT

From the suggestion by Flamy I changed form string[] to List

[ProtoMember(2)] public List<string> items;

Sadly the error still persists.

Another Note

Also I decided to use a dll decompiler to see what is going on. I was not able to decompile the dll until the "string[] items" variable was removed.

SOLVED

I think it is related to some issue with compiling the DLL with Unity3D.

When I created the project in Visual Studios with the code I showed above everything seems to be working as expected. Which is a relief as this seems like it would be a huge issue if protobuf could not serialize string[].

I followed the article provided by BCCode to setup the visual studio project and compile the DLLs.

Now all I need to do is create the dll with my large scale project! Fingers Crossed

Thanks everyone for their help!

هل كانت مفيدة؟

المحلول

Are your references correct? %project dir%\Library\ScriptAssemblies\Assembly-CSharp.dll

Also take a look here: http://purdyjotut.blogspot.com/2013/10/using-protobuf-in-unity3d.html?m=1

نصائح أخرى

Serilizing array of strings using Binary serilization or a serilization method that uses binary format (protobuff in this case) will always result in so many issues. The reason is String by itself is an array of chars,which doesnt have a defined size, meaning it dont know where one string ends and the next starts... Usually we serialize string array one by one instead of the whole array. so I advice you to to use a property which does that (hopefully protobuff accepts attributes on properties!) or create a list of strings instead (though i haven't tested yet it should work!)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top