首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不支持Protobuf-net & IL2CPP - System.Reflection.Emit

不支持Protobuf-net & IL2CPP - System.Reflection.Emit
EN

Stack Overflow用户
提问于 2019-08-29 16:51:54
回答 1查看 2.1K关注 0票数 0

我对用IL2CPP构建的protobuf和Android应用程序有一个问题。

当我使用MONO而不是IL2CPP进行开发时,一切都很好。现在,我需要使用IL2CPP来支持x64。我不知道System.Reflection.Emit不受IL2CPP的支持,而protobuf也在使用它。

是否有一种使原型网络与IL2CPP?一起工作的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-30 06:57:16

我在iOS上也遇到了同样的问题。您之前必须编译ProtoModel。

代码语言:javascript
运行
复制
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从根复制到插件文件夹。然后像这样使用:

代码语言:javascript
运行
复制
TypeModel typeModel = new MyProtoModel();

我创建了一个小型项目Protobuf& Unity:

https://github.com/koshelevpavel/UniBufExample

https://github.com/koshelevpavel/UniBuf

但它只是实验性的,而且没有任何文件。

小示例MonoBehaviour:

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57714689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档