Unity5热更新ILRuntime 使用 Protobuf3.0

Unity5热更新ILRuntime 使用 Protobuf3.0


须知:

1.pb3官方用到了C#很多的新语法.所以在unity主工程中直接撸码是不可以的.还好github上面有同僚作了framework35版的.
2.ILrt中的类目前是不能多继承主程序中的多接口的这是值得注意的地方.也就说repeat目前是没法用的.也是本文不完美的地方.只能用除了repeat之外的功能.具体见下文.
3.亲测ios是运行没有问题的.有问题给本人留言.

原料获取:

1.https://github.com/bitcraftCoLtd/protobuf3-for-unity 获取该处的pb3.
2.https://github.com/Ourpalm/ILRuntime 获取最新的ILrt源码.

原理:

首先说明一下我说的DLL指的就是ILrt的DLL热更新环境. 主程序指的是Unity环境.pb指的是上面给的github里的pb. ILrt指的是ILRuntime.
pb会用proto文件生成一个对应语言的code代码这是大家都知道的.问题主要集中在ILrt要支持这个code代码.由于ILrt多继承接口的限制所以repeat我是没有解决的.其他的正常使用还没发现有什么问题.
本使用方法是把pb的源码全部放到DLL中的.

1.第一步
PB的源码全部copy到DLL中.ILrt是支持高版本的语法的所以这里直接你选4.5是可以直接编译通过的,如果你想实现dll放到主程序也能跑起.那么需要你在工程中填入宏 DOTNET35. 这是pb里给的宏.具体看pb的github页面.可以降到35.
2.第二步
编译你的proto文件放到DLL项目中
3.第三步
由于你的pb里面有继承主程序里的接口所以需要写adapter适配他们.见下文.
完成上面三步应该就可以运行了.祝你好运.有问题请到ILrt的官方群讨论.QQ群: 512079820

//适配文件放到主程序中
using System;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;
using ILRuntime.CLR.Method;
using System.IO;
using System.Collections.Generic;
using System.Collections;

public class Adapter_Protobuf : CrossBindingAdaptor
{
    public override Type BaseCLRType
    {
        get
        {
            return null;
        }
    }

    public override Type[] BaseCLRTypes
    {
        get
        {
            return new Type[] {typeof(IEquatable<ILTypeInstance>), typeof(IComparable<ILTypeInstance>), typeof(IEnumerable<System.Byte>)};
        }
    }

    public override Type AdaptorType
    {
        get
        {
            return typeof(Adaptor);
        }
    }

    public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
    {
        return new Adaptor(appdomain, instance);
    }

    internal class Adaptor : IEquatable<ILTypeInstance>, IComparable<ILTypeInstance>, IEnumerable<System.Byte>, CrossBindingAdaptorType
    {
        ILTypeInstance instance;
        ILRuntime.Runtime.Enviorment.AppDomain appdomain;

        public Adaptor()
        {

        }

        public Adaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
        {
            this.appdomain = appdomain;
            this.instance = instance;
        }

        public object[] data1 = new object[1];

        public ILTypeInstance ILInstance { get { return instance; } }

        IMethod mEquals = null;
        bool mEqualsGot = false;
        public bool Equals(ILTypeInstance other)
        {
            if (!mEqualsGot)
            {
                mEquals = instance.Type.GetMethod("Equals", 1);
                if (mEquals == null)
                {
                    mEquals = instance.Type.GetMethod("System.IEquatable.Equals", 1);
                }
                mEqualsGot = true;
            }
            if (mEquals != null)
            {
                data1[0] = other;
                return (bool)appdomain.Invoke(mEquals, instance, data1);
            }
            return false;
        }

        IMethod mCompareTo = null;
        bool mCompareToGot = false;
        public int CompareTo(ILTypeInstance other)
        {
            if (!mCompareToGot)
            {
                mCompareTo = instance.Type.GetMethod("CompareTo", 1);
                if (mCompareTo == null)
                {
                    mCompareTo = instance.Type.GetMethod("System.IComparable.CompareTo", 1);
                }
                mCompareToGot = true;
            }
            if (mCompareTo != null)
            {
                data1[0] = other;
                return (int)appdomain.Invoke(mCompareTo, instance, data1);
            }
            return 0;
        }

        public IEnumerator<byte> GetEnumerator()
        {
            IMethod method = null;
            method = instance.Type.GetMethod("GetEnumerator", 0);
            if (method == null)
            {
                method = instance.Type.GetMethod("System.Collections.IEnumerable.GetEnumerator", 0);
            }
            if (method != null)
            {
                var res = appdomain.Invoke(method, instance, null);
                return (IEnumerator<byte>)res;
            }
            return null;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            IMethod method = null;
            method = instance.Type.GetMethod("GetEnumerator", 0);
            if (method == null)
            {
                method = instance.Type.GetMethod("System.Collections.IEnumerable.GetEnumerator", 0);
            }
            if (method != null)
            {
                var res = appdomain.Invoke(method, instance, null);
                return (IEnumerator)res;
            }
            return null;
        }
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
本课程主要是针对ILRuntime设计一个独立的脚本热更新框,框架的优势:1.将代码热更脱离Assetbundle资源热更,独立的部分更适用于各种不同的框架。2.加快项目的逻辑更新,bug修复.(后期修bug,多数情况下并不用动到资源,只需要更新脚本代码,无需重走资源打包发布流程,大大提升效率)3.提供热更模式和正常开发模式的快速切换接口,可以让队友像平常一样去开发.4.不依赖市面上的任何AB框架,完全兼容市面上各种不同的AB框架.5.重点:希望通过它,帮助你学习、了解ILRuntime真正在项目中的应用.框架的将提供以下这些接口,这些接口将从0开始,在Unity里将C#脚本编译成dll,然后将dll放到服务器上,再将dll下载下来,进行加载,到最后从Unity主工程调用热更新的代码逻辑.1.Create hotfixdll接口将热更部分的代码 编译成dll生成dll版本配置(MD5)2.更新对比接口本地跟服务器的dll进行版本对比3.下载热更dll下载dll本身的文件下载版本记录文件4.加载热更dll加载dll实例化:AppDomain初始化:注册跨域继承适配器注册委托适配器LitJson重定向调用性能优化(CLR绑定功能)调用热更接口Hotfix.HotfixApplication.Main 进入热更逻辑5.ILMonoBehaviour用于监听组件的生命周期,实际是桥接(调用)热更的逻辑AwakeStartEnableUpdateLateUpdate.......6.添加其他常用的库DOTweenLitJsonSpineGoogle.ProtobufTextAnimation可以根据上面的方式,自行添加依赖的库... 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值