yyl
2025-06-09 b9751b2f076ee050fe5b685e91ae4fc4469b1015
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
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.IO;
 
public class LaunchInHot : SingletonMonobehaviour<LaunchInHot>
{
    static int step = 0;
    public static LaunchStage m_CurrentStage = LaunchStage.None;
    public static LaunchProgressInfo progressInfo { get; private set; }
 
    float timer = 0f;
    Queue<LaunchTask> tasks = new Queue<LaunchTask>();
    LaunchTask currentTask = null;
    bool launchComplete = false;
    float surplusProgress = 0f;
    float surplusTime = 0f;
 
    public Action OnApplicationOut = null;
 
 
    void Start()
    {
        System.Net.ServicePointManager.DefaultConnectionLimit = 100;//设置http最大连接数
        Application.backgroundLoadingPriority = ThreadPriority.High;
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
 
        InitSystemMgr();
        SDKUtils.Instance.Init();  //原sdk接口
 
        //  这里还没开始加载配置 但是提前加载了供LaunchWin使用
        LanguageConfig.ForceInit();
        InitialFunctionConfig.ForceInit();
 
        UIManager.Instance.OpenWindow<LaunchWin>();
 
        var builtInAssetCopyTask = new BuiltInAssetCopyTask();
        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();
        var assetBundleInitTask = new AssetBundleInitTask();
        var configInitTask = new ConfigInitTask();
        var launchFadeOutTask = new LaunchFadeOutTask();
 
        tasks.Enqueue(builtInAssetCopyTask);
        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;
        }
 
        tasks.Enqueue(checkAssetValidTask);
        tasks.Enqueue(downLoadAssetTask);
        tasks.Enqueue(assetBundleInitTask);
        tasks.Enqueue(configInitTask);
        tasks.Enqueue(launchFadeOutTask);
 
        CalculateExpectTotalTime();
 
    }
 
    protected void InitSystemMgr()
    {
        ResManager.Instance.Init();
        UIManager.Instance.Init();
        StageManager.Instance.Init();
        LoginManager.Instance.Init();
    }
 
    void Update()
    {
        if (!launchComplete)
        {
            if (currentTask == null)
            {
                if (tasks.Count > 0)
                {
                    currentTask = tasks.Dequeue();
                    currentTask.Begin();
                }
                else
                {
                    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();
    }
 
 
}