Protobuf-net& IL2CPP-不支持System.Reflection.Emit [英] Protobuf-net & IL2CPP - System.Reflection.Emit is not supported

查看:374
本文介绍了Protobuf-net& IL2CPP-不支持System.Reflection.Emit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用IL2CPP构建的protobuf-net和Android应用程序出现问题.

当我使用MONO代替IL2CPP进行开发时,一切工作都很好.现在,我需要使用IL2CPP来支持x64.我不知道IL2CPP不支持System.Reflection.Emit,而protobuf-net正在使用它.

是否有办法使protobuf-net与IL2CPP一起使用?

解决方案

我在iOS上遇到了同样的问题.您必须先编译ProtoModel.

using Assembly = UnityEditor.Compilation.Assembly;
private static void BuildMyProtoModel()
{
    RuntimeTypeModel typeModel = TypeModel.Create();

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

            // support ISerializationCallbackReceiver
            if (typeof(ISerializationCallbackReceiver).IsAssignableFrom(t))
            {
                MethodInfo beforeSerializeMethod = t.GetMethod("OnBeforeSerialize");
                MethodInfo afterDeserializeMethod = t.GetMethod("OnAfterDeserialize");

                metaType.SetCallbacks(beforeSerializeMethod, null, null, afterDeserializeMethod);
            }

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

    typeModel.Compile("MyProtoModel", "MyProtoModel.dll"); //build model
    string protoSchema = typeModel.GetSchema(null);//content for .proto file, you can generate a proto file for a specific type by passing it instead of null
}

private static IEnumerable<Type> GetTypes(IEnumerable<Assembly> assemblies)
{
    foreach (Assembly assembly in assemblies)
    {
        foreach (Type type in AppDomain.CurrentDomain.Load(assembly.name).GetTypes())
        {
            yield return type;
        }
    }
}

将MyProtoModel.dll从根目录复制到Plugin文件夹. 并像这样使用:

TypeModel typeModel = new MyProtoModel();

我创建了Protobuf-net&团结:

https://github.com/koshelevpavel/UniBufExample

https://github.com/koshelevpavel/UniBuf

但这只是实验性的,没有任何文件.

MonoBehaviour的小示例:

https://gist.github.com/koshelevpavel/8e2d62053fc79b2bf9e2105d18b056bc>I have a problem with the protobuf-net and Android app built with IL2CPP.

Everything worked fine when I used MONO instead of IL2CPP for development. Now I need to use IL2CPP for x64 support. I didn't know System.Reflection.Emit is not supported with IL2CPP and protobuf-net is using it.

Is there a way to make the protobuf-net work with IL2CPP?

解决方案

I got same problem on iOS. You have to compile ProtoModel before.

using Assembly = UnityEditor.Compilation.Assembly;
private static void BuildMyProtoModel()
{
    RuntimeTypeModel typeModel = TypeModel.Create();

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

            // support ISerializationCallbackReceiver
            if (typeof(ISerializationCallbackReceiver).IsAssignableFrom(t))
            {
                MethodInfo beforeSerializeMethod = t.GetMethod("OnBeforeSerialize");
                MethodInfo afterDeserializeMethod = t.GetMethod("OnAfterDeserialize");

                metaType.SetCallbacks(beforeSerializeMethod, null, null, afterDeserializeMethod);
            }

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

    typeModel.Compile("MyProtoModel", "MyProtoModel.dll"); //build model
    string protoSchema = typeModel.GetSchema(null);//content for .proto file, you can generate a proto file for a specific type by passing it instead of null
}

private static IEnumerable<Type> GetTypes(IEnumerable<Assembly> assemblies)
{
    foreach (Assembly assembly in assemblies)
    {
        foreach (Type type in AppDomain.CurrentDomain.Load(assembly.name).GetTypes())
        {
            yield return type;
        }
    }
}

Copy MyProtoModel.dll from root to Plugin folder. And use like this:

TypeModel typeModel = new MyProtoModel();

I create small project Protobuf-net & Unity:

https://github.com/koshelevpavel/UniBufExample

https://github.com/koshelevpavel/UniBuf

But it just experimental and it don't have any documents.

Small example MonoBehaviour:

https://gist.github.com/koshelevpavel/8e2d62053fc79b2bf9e2105d18b056bc

这篇关于Protobuf-net&amp; IL2CPP-不支持System.Reflection.Emit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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