Unity 热更新之ILRuntime(一)

Unity 热更新之ILRuntime(一)

一.ILRuntime环境配置

  • GitHub: ILRuntime链接
  • 创建文件夹Hotfix,Model,ThirdParty
  • 配置程序集
    在这里插入图片描述
    三个程序集配置使用非安全代码
    告诉Unity,该文件夹或者子文件夹下的脚本都编译到同一个dll里面
  • 修改link文件
<linker>  
  <assembly fullname="Unity.Model" preserve="all"/>
  <assembly fullname="Unity.ThirdParty" preserve="all"/>
  <assembly fullname="UnityEngine" preserve="all"/>
  <assembly fullname="System" preserve="all"/>
</linker>

二.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<CodeReferece>().hotfixDll.bytes;
        var pdbBytes = obj.GetComponent<CodeReferece>().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("加载完成!");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Unity游戏开发中的热更新框架,可以使用以下的设计思路和方法进行实现。 首先,热更新包含代码热更、表格数据热更和美术资源热更三部分。代码热更可以通过替换lua脚本后开启lua解释器实现。表格数据热更可以在启动界面完成后,替换表格数据资源。美术资源分为图片资源和模型资源热更,可以在热更完毕后进入游戏。 其次,可以使用MD5效验文件版本的方式来判断资源的更新情况。检查版本控制内的资源,如果有变动,则替换并下载新的资源。这样可以确保只下载需要更新的资源,提高效率。 另外,在本地网络空间部署时,可以根据具体项目的需求进行调整。可以使用Asset Bundle+ILRuntime等技术来实现热更新功能。ILRuntime是一个可以在运行时动态加载和执行C#脚本的开源解决方案,通过它可以实现代码层面的热更新。 综上所述,以上是Unity游戏开发中热更新框架的一种设计思路和实现方法,可以根据具体项目的需求进行调整和优化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [超详细的Unity3D热更新框架,附示例链接,小白也能看的懂](https://blog.csdn.net/Tel17610887670/article/details/109099480)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [Unity 3D模型展示框架篇之Addressables+ILRuntime热更(完结篇)](https://blog.csdn.net/yxl219/article/details/126304884)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值