您的位置:首页 > 移动开发 > Unity3D

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

2018-04-09 11:04 1651 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity ET框架