using ILRuntime.Runtime.Enviorment; using System.Collections; using System.IO; using UnityEngine; using FairyGUI; using System.Reflection; using GFGGame.Launcher; //using ILRuntime.Generated.CrossBinding; using System; using VEngine; using System.Linq; //下面这行为了取消使用WWW的警告,Unity2018以后推荐使用UnityWebRequest,处于兼容性考虑Demo依然使用WWW #pragma warning disable CS0618 namespace GFGGame { class ILRuntimeLauncher : SingletonMonoBase { ILRuntime.Runtime.Enviorment.AppDomain appdomain; System.IO.MemoryStream dllStream; System.IO.MemoryStream pdbStream; public void LoadAssembly(byte[] assBytes, byte[] pdbBytes) { //首先实例化ILRuntime的AppDomain,AppDomain是一个应用程序域,每个AppDomain都是一个独立的沙盒 appdomain = new ILRuntime.Runtime.Enviorment.AppDomain(); dllStream = new MemoryStream(assBytes); pdbStream = new MemoryStream(pdbBytes); try { appdomain.LoadAssembly(dllStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider()); } catch { Debug.LogError("加载热更DLL失败,请确保已经编译过热更DLL"); } InitializeILRuntime(); OnILHotFixLoaded(); } void InitializeILRuntime() { #if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE) //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId; appdomain.DebugService.StartDebugService(56000); #endif //在使用LitJson前,需要对LitJson进行注册 ILRuntimeHelper.RegisterILRuntimeCLRRedirection(appdomain); //这里做一些ILRuntime的注册,如委托适配器,值类型绑定等等 appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterFunctionDelegate(); appdomain.DelegateManager.RegisterFunctionDelegate(); appdomain.DelegateManager.RegisterMethodDelegate(); appdomain.DelegateManager.RegisterDelegateConvertor((action) => { return new EventCallback0(() => { ((System.Action)action)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((action) => { return new EventCallback1((e) => { ((System.Action)action)(e); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new UnityEngine.Application.LogCallback((condition, stackTrace, type) => { ((Action)act)(condition, stackTrace, type); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.TimerCallback((param) => { ((Action)act)(param); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.ListItemRenderer((index, item) => { ((Action)act)(index, item); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.GTweenCallback1((tweener) => { ((Action)act)(tweener); }); }); appdomain.DelegateManager.RegisterDelegateConvertor>((act) => { return new System.Comparison((x, y) => { return ((Func)act)(x, y); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.GTweenCallback(() => { ((Action)act)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.PlayCompleteCallback(() => { ((Action)act)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new FairyGUI.TransitionHook(() => { ((Action)act)(); }); }); appdomain.DelegateManager.RegisterDelegateConvertor>((act) => { return new System.Comparison((x, y) => { return ((Func)act)(x, y); }); }); appdomain.DelegateManager.RegisterDelegateConvertor((act) => { return new System.Timers.ElapsedEventHandler((sender, e) => { ((Action)act)(sender, e); }); }); //appdomain.RegisterCrossBindingAdaptor(new ScriptableObjectAdapter()); //初始化CLR绑定请放在初始化的最后一步!! ILRuntime.Runtime.Generated.CLRBindings.Initialize(appdomain); } void OnILHotFixLoaded() { HotUpdateCodeLoader.Instance.allTypes = appdomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray(); //第一次方法调用 appdomain.Invoke("GFGGame.HotUpdate.HotUpdateEntry", "Start", null, null); } } }