2.ILRuntimeManager逻辑处理
2.1 知识点
重新生成热更工程

重新生成热更工程,因为热更工程是要放到ab包中,所以把后缀改成txt,这样ab包才能识别,把它放到AB包中。
重写ILRuntimeManager
主要修改的地方是从AB中加载dll和pdb文件。
/// <summary>
/// 启动 ILRuntime:从 AB 异步拉取 DLL、PDB,转 MemoryStream 后 LoadAssembly
/// </summary>
public void StartILRuntime(UnityAction callBack, UnityAction<string> infoCallBack)
{
if (!isStart)
{
isStart = true;
appDomain = new AppDomain(ILRuntimeJITFlags.JITOnDemand);
infoCallBack?.Invoke("开始更新 dll 文件");
AssetBundleManager.Instance.LoadAssetBundleResourceAsync<TextAsset>("dll_res", "HotFix_Project.dll",
(dll) =>
{
infoCallBack?.Invoke("开始更新 pdb 文件");
AssetBundleManager.Instance.LoadAssetBundleResourceAsync<TextAsset>("dll_res", "HotFix_Project.pdb",
(pdb) =>
{
dllStream = new MemoryStream(dll.bytes);
pdbStream = new MemoryStream(pdb.bytes);
appDomain.LoadAssembly(dllStream, pdbStream, new PdbReaderProvider());
infoCallBack?.Invoke("开始初始化 ILRuntime");
InitILRuntime();
if (isDebug)
{
infoCallBack?.Invoke("开始等待调试器接入");
StartCoroutine(WaitDebugger(callBack));
}
else
{
infoCallBack?.Invoke("开始执行回调");
callBack?.Invoke();
}
});
});
}
}
2.2 知识点代码
using ILRuntime.Mono.Cecil.Pdb;
using ILRuntime.Runtime;
using ILRuntime.Runtime.Enviorment;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using BaseFramework;
using UnityEngine;
using UnityEngine.Events;
public class ILRuntimeManager : BaseSingletonInMonoBehaviour<ILRuntimeManager>
{
public AppDomain appDomain;
//dll文件和pdb文件的流对象
private MemoryStream dllStream;
private MemoryStream pdbStream;
//是否已经加载了对应的文件
private bool isStart = false;
//是否是调试模式
private bool isDebug = false;
/// <summary>
/// 启动 ILRuntime,从 AB 异步加载 dll、pdb 并完成初始化
/// </summary>
public void StartILRuntime(UnityAction callBack, UnityAction<string> infoCallBack)
{
if (!isStart)
{
isStart = true;
appDomain = new AppDomain(ILRuntimeJITFlags.JITOnDemand);
infoCallBack?.Invoke("开始更新 dll 文件");
AssetBundleManager.Instance.LoadAssetBundleResourceAsync<TextAsset>("dll_res", "HotFix_Project.dll",
(dll) =>
{
infoCallBack?.Invoke("开始更新 pdb 文件");
AssetBundleManager.Instance.LoadAssetBundleResourceAsync<TextAsset>("dll_res", "HotFix_Project.pdb",
(pdb) =>
{
dllStream = new MemoryStream(dll.bytes);
pdbStream = new MemoryStream(pdb.bytes);
appDomain.LoadAssembly(dllStream, pdbStream, new PdbReaderProvider());
infoCallBack?.Invoke("开始初始化 ILRuntime");
InitILRuntime();
if (isDebug)
{
infoCallBack?.Invoke("开始等待调试器接入");
StartCoroutine(WaitDebugger(callBack));
}
else
{
infoCallBack?.Invoke("开始执行回调");
callBack?.Invoke();
}
});
});
}
}
private void InitILRuntime()
{
//如果想使用Unity自带的性能调试窗口 调试ILRuntime相关内容 就需要加入该行代码
appDomain.UnityMainThreadID = Thread.CurrentThread.ManagedThreadId;
}
IEnumerator WaitDebugger(UnityAction callBack)
{
while (!appDomain.DebugService.IsDebuggerAttached)
yield return null;
yield return new WaitForSeconds(1f);
callBack?.Invoke();
}
/// <summary>
/// 停止 ILRuntime,关闭流并卸载
/// </summary>
public void StopILRuntime()
{
if (dllStream != null)
dllStream.Close();
if (pdbStream != null)
pdbStream.Close();
dllStream = null;
pdbStream = null;
appDomain = null;
isStart = false;
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com