ET框架---HotFix.HotFix学习笔记

HotFix学习笔记

请大家关注我的微博:@NormanLin_BadPixel坏像素


从现在开始,关于热更部分的代码,我们很可能得分两部分来讲,一种是在开发的情况下,另一种则是在发布后的情况下。将会大量用到Unity的宏定义

#if ILRuntime
    private ILRuntime.Runtime.Enviorment.AppDomain appDomain;
#else
    private Assembly assembly;
#endif

比如这里,如果是在热更情况下,我们会用到ILRuntime,否则,我们需要用到相关的程序集。

我们看到,在ModelInit方法里,我们先调用了LoadHotfixAssembly

public void LoadHotfixAssembly()
{
    Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
#if ILRuntime
    this.appDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
    GameObject code = Game.Scene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("code.unity3d", "Code");
    byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
    byte[] mdbBytes = code.Get<TextAsset>("Hotfix.pdb").bytes;

    using (MemoryStream fs = new MemoryStream(assBytes))
    using (MemoryStream p = new MemoryStream(mdbBytes))
    {
        this.appDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
    }

    this.start = new ILStaticMethod(this.appDomain, "Hotfix.Init", "Start", 0);
#else
    GameObject code = Game.Scene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("code.unity3d", "Code");
    byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
    byte[] mdbBytes = code.Get<TextAsset>("Hotfix.mdb").bytes;
    this.assembly = Assembly.Load(assBytes, mdbBytes);

    Type hotfixInit = this.assembly.GetType("Hotfix.Init");
    this.start = new MonoStaticMethod(hotfixInit, "Start");
#endif
    Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
}

我们看到,这里就是给热更的域添加内容的地方。通过我们之前学的资源管理组件AssetBundle的一些知识,我们获取到Code这个预制体,并从其身上获取到”Hotfix.dll”跟”Hotfix.pdb”,这些程序集相关的内容,然后把这些程序集信息加载入ILRuntime的域中。

之后,把HotFixstart方法重定向到热更域中的Hotfix.Init.Start方法。我们可以去看一下ILStaticMethod类,其实就是一个指向指定热更域中一个指定方法的类,并继承IStaticMethod,有Run方法。当我们去调用Run方法的时候,其实是去调用热更域中的方法。

如果不用热更,则是直接加载程序集。同样的指定start方法。关于MonoStaticMethod,大家也去看一下吧,全是C#反射的知识。

当我们加载完我们需要的东西后,记得卸载用完了的资源。

Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");

当我们调用GotoHotfix的时候。

public void GotoHotfix()
{
#if ILRuntime
    ILHelper.InitILRuntime(this.appDomain);
#endif
    this.start.Run();
}

看来我们需要先初始化一下ILRuntime。里面的代码多是我看不懂的。。。大家可以先去看看ILRuntime教程

当我们开始看

this.start.Run();

我们就需要进入HotFix程序集里面了。

HotFix.Init

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值