Custom protobuf-net RuntimeTypeModel in Unity3D - Could not load file or assembly

此生再无相见时 提交于 2019-12-11 19:07:29

问题


I am trying to build a custom protobuf-net RuntimeTypeModel in Unity3D with this function:

private static RuntimeTypeModel GetModel()
{
    RuntimeTypeModel typeModel = TypeModel.Create();

    foreach (var t in GetTypes(CompilationPipeline.GetAssemblies()))
    {
        var contract = t.GetCustomAttributes(typeof(ProtoContractAttribute), false);
        if (contract.Length > 0)
        {
            typeModel.Add(t, true);

            //add unity types
            typeModel.Add(typeof(Vector2), false).Add("x", "y");
            typeModel.Add(typeof(Vector3), false).Add("x", "y", "z");
        }
    }

    return typeModel;
}

The model built but when it try to use it I keep getting assembly errors in Unity Editor. - Could not load file or assembly UnityEngine.UI, UnityEngine.Purchasing, Assembly-CSharp etc. No errors when using RuntimeTypeModel.Default.

The strange thing is errors show only after second time I enter play mode after Unity starts. First play is without errors, when I exit the play mode still no errors in the console. Only when I enter the play mode second time the errors show and stay till I restart the Unity.

I have no idea how to improve the code to get a proper RuntimeTypeModel that wouldn't cause the errors. The code for building RuntimeTypeModel is from this answer to my other question.

EDIT: I am not using the following lines but it doesn't matter for the issue:

//add unity types
typeModel.Add(typeof(Vector2), false).Add("x", "y");
typeModel.Add(typeof(Vector3), false).Add("x", "y", "z");

回答1:


New RuntimeTypeModel works without a problem on the android device. So at the moment I decided to change model from MyProtoModel to RuntimeTypeModel.Default when in editor with a simple #if #else statement.

I don't get errors in the editor and I can still use my new model on android. I am not happy with makeshift solutions and still hope for a better answer.



来源:https://stackoverflow.com/questions/57760099/custom-protobuf-net-runtimetypemodel-in-unity3d-could-not-load-file-or-assembl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!