using HybridCLR; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Cysharp.Threading.Tasks; using Mono; using UnityEngine; public class LoadDll { private void Awake() { // Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。 //#if !UNITY_EDITOR // Assembly hotUpdateAss = Assembly.Load(File.ReadAllBytes($"{Application.streamingAssetsPath}/Hotfix.dll.bytes")); //#else // // Editor下无需加载,直接查找获得HotUpdate程序集 // Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Hotfix"); //#endif // game = hotUpdateAss.GetType("Hello"); // var instance = hotUpdateAss.CreateInstance("Game.Game"); // game = (Game.Game)instance; // game.Awake(); // this.game = new Game.Game(gameObject, EPlayMode.HostPlayMode, GameInitType.None); // this.game.GetMethod("Awake").Invoke(null, null); } public List aotDllNames = new List() { }; // public List dllNames = new List() // { // "Hotfix.dll", "Hotfix.pdb" // }; private Dictionary dlls = new Dictionary(); private Dictionary aotDlls = new Dictionary(); public async UniTask DownloadAsync() { if (true) // Dll { // Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。 #if !UNITY_EDITOR string hotfixDll = AssetsConst.Assets_Res_Codes_Hotfix_dll_bytes; var loadAssetAsync = await ResourceComponent.Instance.LoadAssetAsync(hotfixDll); this.dlls.Add(hotfixDll, loadAssetAsync); #else // Editor下无需加载,直接查找获得HotUpdate程序集 // Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Hotfix"); string hotfixDll = "Hotfix"; Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == hotfixDll); #endif } if (true) // AOTDll { #if !UNITY_EDITOR foreach (var dll in this.aotDllNames) { var loadAssetAsync = await ResourceComponent.Instance.LoadAssetAsync(dll); this.aotDlls.Add(dll, loadAssetAsync); } #endif } await UniTask.Yield(); } public void OnStart() { foreach (var kv in this.aotDlls) // 先加载aot内容,避免报错 { TextAsset textAsset = kv.Value; RuntimeApi.LoadMetadataForAOTAssembly(textAsset.bytes, HomologousImageMode.SuperSet); } // Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。 #if !UNITY_EDITOR var dllAsset = this.dlls[AssetsConst.Assets_Res_Codes_Hotfix_dll_bytes]; Assembly hotUpdateAss = Assembly.Load(dllAsset.bytes); #else // Editor下无需加载,直接查找获得HotUpdate程序集 Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Hotfix"); #endif var type = hotUpdateAss.GetType("Game.GameEntry"); object[] pa = new object[] { }; type.GetMethod("Init").Invoke(null, null); // var instance = Activator.CreateInstance(type, pa); // var game1 = instance as Game.Game; // // return game1; } // void Start() // { // this.game.Start(); // } // // private void Update() // { // this.game.Update(); // } // // private void OnDestroy() // { // this.game.OnDestroy(); // } }