- 使用www加载DLL热更文件与PDB日志文件(发布后不需要查看打印热更的日志,可以去掉PDB文件);
#if UNITY_ANDROID
WWW www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.dll");
#else
WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.dll");
#endif
while (!www.isDone)
yield return null;
if (!string.IsNullOrEmpty(www.error))
UnityEngine.Debug.LogError(www.error);
byte[] dll = www.bytes;
www.Dispose();
//PDB文件是调试数据库,如需要在日志中显示报错的行号,则必须提供PDB文件,不过由于会额外耗用内存,正式发布时请将PDB去掉,下面LoadAssembly的时候pdb传null即可
#if UNITY_ANDROID
www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.pdb");
#else
www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.pdb");
#endif
while (!www.isDone)
yield return null;
if (!string.IsNullOrEmpty(www.error))
UnityEngine.Debug.LogError(www.error);
byte[] pdb = www.bytes;
fs = new MemoryStream(dll);
p = new MemoryStream(pdb);
- 实例化ILRuntime的AppDomain沙盒,并使用沙盒加载对应的热更文件与日志,同时将Unity应用主线程告诉沙盒;
//首先实例化
appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
//加载热更文件与日志
appdomain.LoadAssembly(fs, p, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
//由于Unity的Profiler接口只允许在主线程使用,需要告诉ILRuntime主线程的线程ID
appdomain.UnityMainThreadID = Thread.CurrentThread.ManagedThreadId;
- 使用沙盒调用热更方法
//调用带参数的静态方法appdomain.Invoke("类名", "方法名", 对象引用, 参数列表);
appdomain.Invoke("HotFix_Project.InstanceClass", "StaticFunTest2", null, 123);
更多方法调用方式请参考Demo工程中的Invocation.cs类。