using UnityEngine; using System.Collections; using System; using System.Collections.Generic; using System.IO; using Cysharp.Threading.Tasks; public class LaunchInHot : SingletonMonobehaviour { static int step = 0; public static LaunchStage m_CurrentStage = LaunchStage.None; public static LaunchProgressInfo progressInfo { get; private set; } /// /// 外部设置进度信息(用于 Launch 流水线结束后继续显示 config 加载进度) /// public static void SetProgressInfo(LaunchProgressInfo info) { progressInfo = info; } float timer = 0f; Queue tasks = new Queue(); LaunchTask currentTask = null; bool launchComplete = false; float surplusProgress = 0f; float surplusTime = 0f; bool launchStartFinish = false; public Action OnApplicationOut = null; async void Start() { launchStartFinish = false; try { await StartAsync(); } catch (Exception ex) { Debug.LogError($"[LaunchInHot] Start 初始化异常,游戏无法继续: {ex}"); return; // launchStartFinish 保持 false,Update 会一直等待(不会进入 Main.Init) } launchStartFinish = true; } async UniTask StartAsync() { System.Net.ServicePointManager.DefaultConnectionLimit = 100;//设置http最大连接数 Application.backgroundLoadingPriority = ThreadPriority.High; Screen.sleepTimeout = SleepTimeout.NeverSleep; await VersionConfig.GetAsync(); // 确保 VersionConfig.config 在 SDKUtils.Init() 前已加载 SDKUtils.Instance.Init(); //原sdk接口 if (!AssetSource.isUseAssetBundle) { await InitSystemMgr(); UIManager.Instance.OpenWindowAsync().Forget(); } OperationLogCollect.Instance.RecordLauchEvent(7); // US1: Add YooAsset initialization task — replaces AssetBundleInitTask var yooAssetInitTask = new YooAssetInitTask(); tasks.Enqueue(yooAssetInitTask); // AssetBundleInitTask removed — YooAssetInitTask handles all resource system initialization #if UNITY_WEBGL // WebGL 不使用 BuiltInAssetCopyTask(无需拷贝内置资源), // 但仍需在 YooAsset 初始化后调用 InitSystemMgr 初始化 UIManager 等。 var webGLSystemInitTask = new WebGLSystemInitTask(); tasks.Enqueue(webGLSystemInitTask); #else var builtInAssetCopyTask = new BuiltInAssetCopyTask(); tasks.Enqueue(builtInAssetCopyTask); #endif var requestPermissionStart = new RequestPermissionStart(); var initSettingTask = new InitSettingTask(); var sdkInitedTask = new SDKInitedTask(); //var assetCopyTask = new AssetCopyTask(); //var assetDecompressTask = new AssetDecompressTask(); var getVersionInfoTask = new GetVersionInfoTask(); var checkAssetValidTask = new CheckAssetValidTask(); var downLoadAssetTask = new DownLoadAssetTask(); // AssetBundleInitTask removed — replaced by YooAssetInitTask var configInitTask = new ConfigInitTask(); var launchFadeOutTask = new LaunchFadeOutTask(); tasks.Enqueue(requestPermissionStart); tasks.Enqueue(initSettingTask); if (!Application.isEditor) { tasks.Enqueue(sdkInitedTask); } switch (Application.platform) { case RuntimePlatform.OSXEditor: case RuntimePlatform.WindowsEditor: tasks.Enqueue(getVersionInfoTask); break; case RuntimePlatform.Android: tasks.Enqueue(getVersionInfoTask); //tasks.Enqueue(assetCopyTask); //tasks.Enqueue(assetDecompressTask); break; case RuntimePlatform.IPhonePlayer: tasks.Enqueue(getVersionInfoTask); //tasks.Enqueue(assetCopyTask); break; case RuntimePlatform.WindowsPlayer: //tasks.Enqueue(assetCopyTask); tasks.Enqueue(getVersionInfoTask); break; case RuntimePlatform.WebGLPlayer: tasks.Enqueue(getVersionInfoTask); break; } tasks.Enqueue(checkAssetValidTask); tasks.Enqueue(downLoadAssetTask); tasks.Enqueue(configInitTask); tasks.Enqueue(launchFadeOutTask); CalculateExpectTotalTime(); } public async UniTask InitSystemMgr() { SpriteAtlasHandler.Register(); ResManager.Instance.Init(); await UIManager.Instance.Init(); await StageManager.Instance.Init(); await LoginManager.Instance.Init(); } void Update() { if (!launchStartFinish) { return; } if (!launchComplete) { if (currentTask == null) { if (tasks.Count > 0) { currentTask = tasks.Dequeue(); Debug.LogError("start currentTask is " + currentTask.GetType().Name); currentTask.Begin(); } else { Debug.LogError("launchComplete = true"); launchComplete = true; } } if (currentTask != null) { currentTask.Update(); } if (currentTask != null && currentTask.done) { currentTask.End(); CalculateExpectTotalTime(); currentTask = null; } if (m_CurrentStage == LaunchStage.DownLoad) { progressInfo = new LaunchProgressInfo(m_CurrentStage, 1, progressInfo.totalProgress, 0f); } else { timer += Time.deltaTime; var progress = progressInfo.totalProgress + surplusProgress * (Time.deltaTime / surplusTime); progress = Mathf.Clamp(progress, 0, 0.98f); var partProgress = 0f; if (currentTask == null) { partProgress = 0f; } else { var temp = currentTask.timer / Mathf.Min(2f, currentTask.duration); step = (int)temp + 1; partProgress = temp - (int)temp; } progressInfo = new LaunchProgressInfo(m_CurrentStage, step, Mathf.Clamp01(progress), partProgress); } } // Debug.LogError("currentTask is null " + (currentTask == null).ToString() + " isLaunchComplete " + launchComplete.ToString()); // if (currentTask != null) // { if (launchComplete) { UnityEngine.Debug.LogFormat("启动耗时:{0}", timer); progressInfo = new LaunchProgressInfo(m_CurrentStage, 1, 1f, 1f); this.enabled = false; Main.Init(); // StageLoad.Instance.PushSceneLoadCommand(new StageLoad.StageLoadCommand() // { // toMapId = 1, // toLineId = 0, // needEmpty = false, // needLoadResource = true, // serverType = ServerType.Main, // isClientLoadMap = true // }); } // } } public static int GetLaunchStage() { return (int)m_CurrentStage; } /// /// 计算总的预期时间 /// void CalculateExpectTotalTime() { surplusTime = 0f; foreach (var item in tasks) { surplusTime += item.expectTime; } surplusProgress = 1 - progressInfo.totalProgress; } private void OnApplicationQuit() { OnApplicationOut?.Invoke(); } }