ILRuntime入门04 回调方法与委托的使用

简要介绍

如果只在Unity项目或者只在热更DLL工程中使用回调或者委托,那么按正常C#代码编写就行。如果你需要将委托实例传给ILRuntime外部使用,那则根据情况,你需要额外添加适配器或者转换器。如果为了避免写转换器,在项目中就应该尽量使用回调Action和Func。

委托使用流程:

  1. 实例化ILRuntime的AppDomain,加载热更dll和pdb,第一步和上一节文章一样。
//AppDomain是ILRuntime的入口,最好是在一个单例类中保存,整个游戏全局就一个,这里为了示例方便,每个例子里面都单独做了一个
    //大家在正式项目中请全局只创建一个AppDomain
    AppDomain appdomain;
    System.IO.MemoryStream fs;
    System.IO.MemoryStream p;

    void Start()
    {
        StartCoroutine(LoadHotFixAssembly());
    }

    IEnumerator LoadHotFixAssembly()
    {
        //首先实例化ILRuntime的AppDomain,AppDomain是一个应用程序域,每个AppDomain都是一个独立的沙盒
        appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
        //正常项目中应该是自行从其他地方下载dll,或者打包在AssetBundle中读取,平时开发以及为了演示方便直接从StreammingAssets中读取,
        //正式发布的时候需要大家自行从其他地方读取dll

        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //这个DLL文件是直接编译HotFix_Project.sln生成的,已经在项目中设置好输出目录为StreamingAssets,在VS里直接编译即可生成到对应目录,无需手动拷贝
        //工程目录在Assets\Samples\ILRuntime\1.6\Demo\HotFix_Project~
#if UNITY_ANDROID
        WWW www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.dll");
#else
        WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.dll");
#endif
        while (!www.isDone)
            yield return null;
        if (!string.IsNullOrEmpty(www.error))
            UnityEngine.Debug.LogError(www.error);
        byte[] dll = www.bytes;
        www.Dispose();

        //PDB文件是调试数据库,如需要在日志中显示报错的行号,则必须提供PDB文件,不过由于会额外耗用内存,正式发布时请将PDB去掉,下面LoadAssembly的时候pdb传null即可
#if UNITY_ANDROID
        www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.pdb");
#else
        www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.pdb");
#endif
        while (!www.isDone)
            yield return null;
        if (!string.IsNullOrEmpty(www.error))
            UnityEngine.Debug.LogError(www.error);
        byte[] pdb = www.bytes;
        fs = new MemoryStream(dll);
        p = new MemoryStream(pdb);
        try
        {
            appdomain.LoadAssembly(fs, p, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
        }
        catch
        {
            Debug.LogError("加载热更DLL失败,请确保已经通过VS打开Assets/Samples/ILRuntime/1.6/Demo/HotFix_Project/HotFix_Project.sln编译过热更DLL");
        }

#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
        //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler
        appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
#endif

        InitializeILRuntime();
        OnHotFixLoaded();
    }
  1. 声明委托和回调方法
public delegate string UnityDelegateFunction(int a);

public class TestRunTime : MonoBehaviour
{
    public static UnityDelegateFunction TestFunctionDelegate;
    public static System.Action<string> TestActionDelegate;
 

3.初始化注册热更回调和委托,并编写转换器

 /// <summary>
    /// 初始化热更资源
    /// </summary>
    void InitializeILRuntime()
    {
        //ILRuntime的委托和方法回调注册
        Debug.Log("注册ILRuntime委托和方法回调");
        //带返回值的委托的话需要用RegisterFunctionDelegate,返回类型为最后一个
        appdomain.DelegateManager.RegisterFunctionDelegate<int, string>();
        //Action<string> 的参数为一个string
        appdomain.DelegateManager.RegisterMethodDelegate<string>();

        //---注册转换器---
        Debug.Log("ILRuntime跨域使用需要注册委托的转换器");
        //ILRuntime内部是用Action和Func这两个系统内置的委托类型来创建实例的,所以其他的委托类型都需要写转换器
        appdomain.DelegateManager.RegisterDelegateConvertor<UnityDelegateFunction>((action) =>
        {
            return new UnityDelegateFunction((a) =>
            {
                return ((System.Func<int, string>)action)(a);
            });
        });

    }
  1. 在unity中调用热更回调和委托

    void OnHotFixLoaded()
    {
        appdomain.Invoke("HotFix_Project.TestUnity", "Initialize2", null, null);
        appdomain.Invoke("HotFix_Project.TestUnity", "RunTest2", null, null);

        var str = TestFunctionDelegate(098);
        Debug.Log("!! OnHotFixLoaded str = " + str);
        TestActionDelegate("Hello From Unity Main Project");
    }
  1. 编写热更回调和委托方法
    由于热更工程引用了unity的dll,所以unity新建的类和声明的公有变量方法都能访问得到。
using System;
using System.Collections.Generic;

namespace HotFix_Project
{
    public class TestUnity
    {
        public static void Initialize2()
        {
            TestRunTime.TestFunctionDelegate = Function;
            TestRunTime.TestActionDelegate = Action;
        }

        public static void RunTest2()
        {
            var res = TestRunTime.TestFunctionDelegate(456);
            UnityEngine.Debug.Log("!! TestRunTime.RunTest2 res = " + res);
            TestRunTime.TestActionDelegate("rrr");
        }

        static string Function(int a)
        {
            return a.ToString();
        }

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

运行Unity,就能正常把热更中的回调和委托的内容打印出来了。
热更回调委托打印内容
参考代码文件(免费):https://download.csdn.net/download/HiggsParticleHYX/21132822

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值