using HybridCLR; using System.Collections; using System.IO; using System.Reflection; using UnityEngine; using UnityEngine.Networking; using LhorionIOSSDK; public class HotUpdateLauncher : MonoBehaviour { private Assembly _hotUpdateAss; // 线上CDN地址,本地测试注释掉改用StreamingAssets private const string RemoteDllUrl = "https://xxx-cdn/HotUpdate.dll.bytes"; private string LocalDllCachePath => Path.Combine(Application.persistentDataPath, "HotUpdate.dll.bytes"); // IEnumerator Start() // { // // 步骤1:加载AOT元数据(HybridCLR强制) // yield return LoadAOTMetadata(); // // // 步骤2:下载/读取热更DLL // #if !UNITY_EDITOR // // 真机:线上下载DLL,无缓存/版本更新则重新下载 // if (!File.Exists(LocalDllCachePath)) // { // yield return DownloadHotDll(); // } // byte[] dllBytes = File.ReadAllBytes(LocalDllCachePath); // _hotUpdateAss = Assembly.Load(dllBytes); // #else // // Editor编辑器:无需下载,直接获取程序集 // _hotUpdateAss = AppDomain.CurrentDomain.GetAssemblies() // .First(a => a.GetName().Name == "HotUpdate"); // #endif // // // 步骤3:反射调用热更层游戏入口(示例:HotUpdate里Hello类 Run静态方法) // Type hotEntryType = _hotUpdateAss.GetType("Hello"); // hotEntryType.GetMethod("Run").Invoke(null, null); // // // ====================== 文档要求:热更加载完成后初始化SDK ====================== // InitLhorionSDK(); // } private void Start() { InitLhorionSDK(); } #region HybridCLR 元数据 + DLL下载 // 加载AOT元数据 IEnumerator LoadAOTMetadata() { string[] metaFiles = { "mscorlib.dll.bytes", "System.dll.bytes", "System.Core.dll.bytes" }; HomologousImageMode mode = HomologousImageMode.SuperSet; foreach (var file in metaFiles) { string path = Path.Combine(Application.streamingAssetsPath, file); using (var req = UnityWebRequest.Get(path)) { yield return req.SendWebRequest(); var err = RuntimeApi.LoadMetadataForAOTAssembly(req.downloadHandler.data, mode); Debug.Log($"加载元数据 {file} 结果:{err}"); } } } // 从CDN下载热更DLL到本地持久化目录 IEnumerator DownloadHotDll() { using (var req = UnityWebRequest.Get(RemoteDllUrl)) { req.downloadHandler = new DownloadHandlerFile(LocalDllCachePath); yield return req.SendWebRequest(); if (req.result != UnityWebRequest.Result.Success) { Debug.LogError("热更DLL下载失败:" + req.error); yield break; } Debug.Log("DLL下载缓存完成"); } } #endregion #region LhorionSDK 初始化逻辑(文档标准写法) static void InitLhorionSDK() { // 1. 创建空物体挂载SDK启动脚本 GameObject sdkObj = new GameObject("LhorionSDKBoot"); LhorionBootstrap boot = sdkObj.AddComponent(); // 2. 可选:设置归因渠道,用于数据分析 boot.SetAttrInfo("unityad"); // 3. 必接接口:设置是否屏蔽增收逻辑(苹果审核用) // 线上从服务器/CDN拉取状态,审核期间填true屏蔽 bool isForbiden = false; LhorionIOSSDK.LhorionBootstrap.SetForbidenIncome (isForbiden); } #endregion #region 网页弹窗接口(可选,仅iOS真机生效,编辑器无法测试) /// /// 显示网页入口弹窗,传入弹窗RectTransform /// public void ShowSdkWeb(RectTransform webRect) { LhorionBootstrap.ShowWeb(webRect); } /// /// 关闭网页弹窗 /// public void CloseSdkWeb() { LhorionBootstrap.DestroyWeb(); } #endregion }