using HybridCLR; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Cysharp.Threading.Tasks; using UnityEngine; using YooAsset; namespace Unity.Loader { public class Loader { public List aotDllNames = new List() { LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_mscorlib_dll_bytes, LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_Sirenix_Utilities_dll_bytes, LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_UniTask_dll_bytes, LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_YooAsset_dll_bytes, LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_UnityEngine_CoreModule_dll_bytes, }; private Dictionary dlls = new Dictionary(); private Dictionary aotDlls = new Dictionary(); public async UniTask DownloadAsync() { if (true) // Dll { string dllName = LoaderAssetConst.Assets_DemoGame_GameRes_Codes_Hotfix_dll_bytes; // Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。 #if !UNITY_EDITOR string hotfixDll = dllName; var loadAssetAsync = await ResourcesComponent.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 ResourcesComponent.Instance.LoadAssetAsync(dll); this.aotDlls.Add(dll, loadAssetAsync); } #endif } await UniTask.Yield(); } public void OnStart(GameObject go, EPlayMode playMode) { foreach (var kv in this.aotDlls) // 先加载aot内容,避免报错 { TextAsset textAsset = kv.Value; // Debug.Log($":{kv.Key}. text: {textAsset.text}"); var errorCode = RuntimeApi.LoadMetadataForAOTAssembly(textAsset.bytes, HomologousImageMode.SuperSet); Debug.Log($"LoadMetadataForAOTAssembly:{kv.Key}. ret: {errorCode}"); } string dllName = LoaderAssetConst.Assets_DemoGame_GameRes_Codes_Hotfix_dll_bytes; // Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。 #if !UNITY_EDITOR var dllAsset = this.dlls[dllName]; 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("ZC.GameEntry"); this.Create(); object[] pa = new object[] { go, playMode }; type.GetMethod("Init").Invoke(null, pa); } /// /// 创建实例,防止引用缺失 /// private void Create() { // ?? SystemInfo info = new SystemInfo(); // Compass compass = new Compass(); Input input = new Input(); // Debug.Log($"info : {info.GetType()}"); } } }