ILRuntime学习笔记(三)——使用委托

ILRuntime中的委托

1.由于IL2CPP的特性,使用到不同参数组合的委托需要注册委托适配器
2.如果使用非Action/Function类型委托,需要注册委托转换器

3.尽量避免不必要的跨域委托调用
4.尽量使用Action/Function这两个万能的委托

一、自定义无返回值无参委托

主工程:
1.类外定义委托

public delegate void TestDelegateMethodNone();

2.注册委托转换器

    void InitializeILRuntime()
    {
        appdomain.DelegateManager.RegisterDelegateConvertor<TestDelegateMethodNone>((action) =>
        {
            return new TestDelegateMethodNone(() =>
            {
                ((System.Action)action)();
            });
        });

    }

3.初始化并调用

    void OnHotFixLoaded()
    {
        appdomain.Invoke("HotFix_Project.TestDelegate", "Init", null, null);
        TestMethodNoneDelegate();
    }

热更工程:
1.类内定义委托

static TestDelegateMethodNone delegateMethodNone;

2.初始化函数

       public static void Init()
        {
            HelloWorld.TestMethodNoneDelegate = MethodNone;
        }

3.定义方法

        static void MethodNone()
        {
            UnityEngine.Debug.Log("!! TestDelegate.MethodNone!!!");
        }

二、自定义无返回值有参委托

主工程:
1.类外定义委托

public delegate void TestDelegateMethod(int a);

2.注册委托转换器

		//TestDelegateMethod, 这个委托类型为有个参数为int的方法,注册仅需要注册不同的参数搭配即可
        appdomain.DelegateManager.RegisterMethodDelegate<int>();

        appdomain.DelegateManager.RegisterDelegateConvertor<TestDelegateMethod>((action) =>
        {
            //转换器的目的是把Action或者Func转换成正确的类型,这里则是把Action<int>转换成TestDelegateMethod
            return new TestDelegateMethod((a) =>
            {
                //调用委托实例
                ((System.Action<int>)action)(a);
            });
        });

3.初始化并调用

    void OnHotFixLoaded()
    {
        appdomain.Invoke("HotFix_Project.TestDelegate", "Init", null, null);
        appdomain.Invoke("HotFix_Project.TestDelegate", "RunTest", null, null);
        TestMethodDelegate(789);
    }

热更工程:
1.类内定义委托

        static TestDelegateMethod delegateMethod;

2.初始化函数

       public static void Init()
        {
 			HelloWorld.TestMethodDelegate = Method;
        }

3.定义方法

        static void Method(int a)
        {
            UnityEngine.Debug.Log("!! TestDelegate.Method, a = " + a);
        }   
        public static void RunTest()
        {
            HelloWorld.TestMethodDelegate(123);
        }

三、自定义有返回值有参委托

主工程:
1.类外定义委托

public delegate string TestDelegateFunction(int a);

2.注册委托转换器

		//带返回值的委托的话需要用RegisterFunctionDelegate,返回类型为最后一个
		appdomain.DelegateManager.RegisterFunctionDelegate<int, string>();

		//对于TestDelegateFunction同理,只是是将Func<int, string>转换成TestDelegateFunction
        appdomain.DelegateManager.RegisterDelegateConvertor<TestDelegateFunction>((action) =>
        {
            return new TestDelegateFunction((a) =>
            {
                return ((System.Func<int, string>)action)(a);
            });
        });

3.初始化并调用

    void OnHotFixLoaded()
    {
        appdomain.Invoke("HotFix_Project.TestDelegate", "Init", null, null);
        appdomain.Invoke("HotFix_Project.TestDelegate", "RunTest", null, null);
        var str = TestFunctionDelegate(098);
        Debug.Log("!! OnHotFixLoaded str = " + str);
    }

热更工程:
1.类内定义委托

        static TestDelegateFunction delegateFunc;

2.初始化函数

       public static void Init()
        {
 			HelloWorld.TestMethodDelegate = Method;
        }

3.定义方法

        static string Function(int a)
        {
            return a.ToString();
        } 
        public static void RunTest()
        {
            var res = HelloWorld.TestFunctionDelegate(456);
            UnityEngine.Debug.Log("!! TestDelegate.RunTest2 res = " + res);
        }

四、事件调用

主工程:
1.类外定义委托

    public static System.Action<string> TestActionDelegate;

2.注册委托转换器

        //Action<string> 的参数为一个string
        appdomain.DelegateManager.RegisterMethodDelegate<string>();

3.初始化并调用

    void OnHotFixLoaded()
    {
        appdomain.Invoke("HotFix_Project.TestDelegate", "Init", null, null);
        TestActionDelegate("你好");

热更工程:
1.类内定义委托

	static Action<string> delegateAction;

2.初始化函数

       public static void Init()
        {
 			 HelloWorld.TestActionDelegate = Action;
        }

3.定义方法

        static void Action(string a)
        {
            UnityEngine.Debug.Log("!! TestDelegate.Action, a = " + a);
        } 

运行结果:
在这里插入图片描述

五、UGUI常用

        //下面再举一个这个Demo中没有用到,但是UGUI经常遇到的一个委托,例如UnityAction<float>
        appdomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Events.UnityAction<float>>((action) =>
        {
            return new UnityEngine.Events.UnityAction<float>((a) =>
            {
                ((System.Action<float>)action)(a);
            });
        });

1.完全在热更DLL内部使用的委托,直接可用,不需要做任何处理
2.这是因为iOS的IL2CPP模式下,不能动态生成类型,为了避免出现不可预知的问题,我们没有通过反射的方式创建委托实例,因此需要手动进行一些注册
3.如果没有注册委托适配器,运行时会报错并提示需要的注册代码,直接复制粘贴到ILRuntime初始化的地方
4.运行成功,我们可以看见,用Action或者Func当作委托类型的话,可以避免写转换器,所以项目中在不必要的情况下尽量只用Action和Func
5.另外应该尽量减少不必要的跨域委托调用,如果委托只在热更DLL中用,是不需要进行任何注册的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本课程主要是针对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可以根据上面的方式,自行添加依赖的库... 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值