栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

Unity 热更新之ILRuntime(一)

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity 热更新之ILRuntime(一)

Unity 热更新之ILRuntime(一) 一.ILRuntime环境配置
  • GitHub: ILRuntime链接
  • 创建文件夹Hotfix,Model,ThirdParty
  • 配置程序集

    三个程序集配置使用非安全代码
    告诉Unity,该文件夹或者子文件夹下的脚本都编译到同一个dll里面
  • 修改link文件
  
  
  
  
  

二.ILRuntime原理

ILRuntime借助Mono.Cecil库来读取DLL的PE信息,以及当中类型的所有信息,最终得到方法的IL汇编码,然后通过内置的IL解译执行虚拟机来执行DLL中的代码。

三.加载流程

  • 自动化插件实现自动拷贝Dll和Pdb文件
[InitializeOnLoad]
public class HotFixBuildEditor
{
    private const string rootPath = "Library/ScriptAssemblies";
    private const string hotFixDll = "Unity.HotFix.dll";
    private const string hotFixPdb = "Unity.HotFix.pdb";
    private const string targetPath = "Assets/Res/Code";

    static HotFixBuildEditor()
    {
        File.Copy(Path.Combine(rootPath, hotFixDll), Path.Combine(targetPath, hotFixDll + ".bytes"), true);
        File.Copy(Path.Combine(rootPath, hotFixPdb), Path.Combine(targetPath, hotFixPdb + ".bytes"), true);
        AssetDatabase.Refresh();
        Debug.Log("拷贝完成!");
    }
}
四.脚本中初始化加载DLL
public class HotFixManager : MonoBehaviour
{
    public GameObject obj;
    private MemoryStream _dllMs;
    private MemoryStream _pdbMs;
    private AppDomain _appDomain;

    void Start()
    {
        Load();
    }

    private void Load()
    {
        var dllBytes = obj.GetComponent().hotfixDll.bytes;
        var pdbBytes = obj.GetComponent().hotfixPdb.bytes;

#if ILRuntime
        Debug.Log("ILRuntime运行成功!!!");
        _dllMs = new MemoryStream(dllBytes);
        _pdbMs = new MemoryStream(pdbBytes);
        _appDomain = new AppDomain();
        _appDomain.LoadAssembly(_dllMs, _pdbMs, new PdbReaderProvider());
#else
        Assembly.Load(dllBytes, pdbBytes);
#endif
        Debug.Log("加载完成!");
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900260.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号