Unity3D中的自定义protobuf-net RuntimeTypeModel-无法加载文件或程序集 [英] Custom protobuf-net RuntimeTypeModel in Unity3D - Could not load file or assembly

查看:122
本文介绍了Unity3D中的自定义protobuf-net RuntimeTypeModel-无法加载文件或程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下功能在Unity3D中构建自定义的protobuf-net RuntimeTypeModel:

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;
}

该模型已构建,但是当尝试使用它时,我在Unity Editor中不断收到装配错误. -无法加载文件或程序集UnityEngine.UI,UnityEngine.Purchasing,Assembly-CSharp等.使用RuntimeTypeModel.Default时没有错误.

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.

奇怪的是,只有在Unity启动后第二次进入播放模式后才会显示错误.第一次播放没有错误,当我退出播放模式时,控制台中仍然没有错误.仅当我第二次进入播放模式时,错误才会显示并一直存在,直到我重新启动Unity.

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.

我不知道如何改进代码以获得不会导致错误的适当RuntimeTypeModel.用于构建RuntimeTypeModel的代码来自,它回答了我的另一个问题.

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.

我没有使用以下几行,但与该问题无关紧要:

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");

推荐答案

新的RuntimeTypeModel可以在android设备上正常运行.因此,目前我决定在编辑器中使用简单的#if #else语句将模型从MyProtoModel更改为RuntimeTypeModel.Default.

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.

我在编辑器中没有出现错误,并且我仍然可以在android上使用我的新模型.我对临时解决方案不满意,但仍然希望有一个更好的答案.

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.

这篇关于Unity3D中的自定义protobuf-net RuntimeTypeModel-无法加载文件或程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆