• ILRuntime使用指南


    前言
    • 最近闲着没事做,于是用ILRuntime做了一个小游戏
    • 中间遇到一些坑,对于ILRuntime的认识更清楚了。
    • 其它技巧
    • 自动转换DLL
    设置引用文件夹
    • 我们在热更项目里面写代码的时候需要用到Unity的DLL,可是这些DLL引用起来查找就很麻烦。
    • 这时候可以设置文件夹
    • 编辑器就会自动提示需要引用的DLL在这里插入图片描述
    实际环境如何引用DLL的
    • 这一点我很疑惑,比如我在热更里面用到了Unity的某个DLL
    • 可是在主工程里面又没有用到,那我在打包编译的时候,这个DLL就不会被放进去,那我热更的时候解释器找不到DLL怎么办
    • 最后我发现只需要CLR自动绑定就可以了
    • 大概原理就是把引用的DLL做一些包装,外加反射快速一些
    [MenuItem("ILRuntime/Generate CLR Binding Code by Analysis")]
    	static void GenerateCLRBindingByAnalysis()
    	{
    		//用新的分析热更dll调用引用来生成绑定代码
    		ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain();
    		var bytes = File.ReadAllBytes(DefaultPath.LoadDllPath);
    		using (var dllStream = new MemoryStream(bytes))
    		{
    			domain.LoadAssembly(dllStream);
    
    			//Crossbind Adapter is needed to generate the correct binding code
    			InitILRuntime(domain);
    			ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, "Assets/ILRuntime/Generated");
    		}
    
    		AssetDatabase.Refresh();
    	}
    
    	static void InitILRuntime(ILRuntime.Runtime.Enviorment.AppDomain domain)
    	{
    		domain.RegisterCrossBindingAdaptor(new CoroutineAdapter());
    		domain.RegisterCrossBindingAdaptor(new IAsyncStateMachineClassInheritanceAdaptor());
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 需要初始化绑定
    		ILRuntime.Runtime.Generated.CLRBindings.Initialize(appdomain);
    
    
    • 1
    • 2
    写适配器
    • 我本来以为只有当热更项目需要用到主工程的类和文件时候才需要写适配器
    • 其实对于ILRuntime不支持的一些功能,也需要自己写适配器
    • 比如async await需要写适配器,不过我在ILRuntime的issue里面找到一个写好的脚本
      异步适配器脚本
    • 协程也需要用适配器,而且要调用Unity主工程来调用
    • 协程适配器
    • 使用Unity的事件也需要写委托转换器
    	appdomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Events.UnityAction>((act) =>
    		{
    			return new UnityEngine.Events.UnityAction(() =>
    			{
    				((Action)act)();
    			});
    		});
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 其它各种类型的委托也需要自己定义
    	appdomain.DelegateManager.RegisterMethodDelegate<UnityEngine.GameObject>();
    		appdomain.DelegateManager.RegisterMethodDelegate<AsyncOperationHandle, Exception>();
    		appdomain.DelegateManager.RegisterMethodDelegate<bool>();
    		appdomain.DelegateManager.RegisterMethodDelegate<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance>();
    
    • 1
    • 2
    • 3
    • 4
    使用ILRuntime的Debug功能
    • 我只说一点
    • 按钮在这里在这里插入图片描述
    • 如果你链接不上项目,就把VS和unity全部关了再打开一遍就可以
  • 相关阅读:
    计算机毕业设计(附源码)python智慧校园防疫管理平台
    渗透测试之Web安全系列教程(二)
    某校帮签到小程序m 加密参数解析
    指针和数组笔试题解析
    【计算机网络】广域网协议分析
    c语言分层理解(动态通讯录的实现)
    C++学习笔记(十二)
    k8s 拉取镜像报错 no basic auth credentials
    模拟栈(模板)
    VoLTE基础自学系列 | VoLTE实战分析之VoLTE注册流程
  • 原文地址:https://blog.csdn.net/qq_33574890/article/details/128061020