yyl
2026-05-11 51b0f6ed9f4e1d3bb6f8144470b46908c7699a96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.IO;
using Cysharp.Threading.Tasks;
 
public class LaunchInHot : SingletonMonobehaviour<LaunchInHot>
{
    static int step = 0;
    public static LaunchStage m_CurrentStage = LaunchStage.None;
    public static LaunchProgressInfo progressInfo { get; private set; }
 
    /// <summary>
    /// 外部设置进度信息(用于 Launch 流水线结束后继续显示 config 加载进度)
    /// </summary>
    public static void SetProgressInfo(LaunchProgressInfo info)
    {
        progressInfo = info;
    }
 
    float timer = 0f;
    Queue<LaunchTask> tasks = new Queue<LaunchTask>();
    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<LaunchWin>().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;
    }
 
    /// <summary>
    /// 计算总的预期时间
    /// </summary>
    void CalculateExpectTotalTime()
    {
        surplusTime = 0f;
        foreach (var item in tasks)
        {
            surplusTime += item.expectTime;
        }
 
        surplusProgress = 1 - progressInfo.totalProgress;
    }
 
 
    private void OnApplicationQuit()
    {
        OnApplicationOut?.Invoke();
    }
 
 
}