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中用,是不需要进行任何注册的

猜你喜欢

转载自blog.csdn.net/weixin_45724919/article/details/129471502