yyl
9 天以前 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;
using System.IO;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
using ProjSG.Resource;
using YooAsset;
 
public enum StageName
{
    Login,
    Game,
}
 
public class StageManager : Singleton<StageManager>, IManager
{
    public StageName currentStage;
 
    public Action AfterLoadingGameScene;
 
    public Action BeforeLoadingGameScene;
 
    // public Action OnSwitchAccount;
    private LaunchWinData launchWinData = null;
 
    public UniTask Init()
    {
        UIManager.Instance.OnCloseWindow += OnCloseWindow;
        return UniTask.CompletedTask;
    }
 
    public void Release()
    {
        AfterLoadingGameScene = null;
        BeforeLoadingGameScene = null;
        UIManager.Instance.OnCloseWindow -= OnCloseWindow;
    }
 
    public async UniTaskVoid ToLoginScene()
    {
        UIManager.Instance.DestroyAllUI();
 
        // US3: Show loading screen FIRST, then load resources with progress
        LoadingWin loadingWin = await UIManager.Instance.OpenWindowAsync<LoadingWin>();
        InitLoadingWinData(loadingWin);
 
        // Phase 1 (0% ~ 50%): Scene loading
        if (AssetSource.isUseAssetBundle)
        {
            var handle = YooAssetService.Instance.BeginLoadScene("Assets/ResourcesOut/Scenes/Login.unity", LoadSceneMode.Single, LocalPhysicsMode.None, true);
            while (!handle.IsDone)
            {
                if (handle.Progress >= 0.9f)
                    handle.UnSuspend();
                loadingWin.SetProgress(handle.Progress * 0.5f);
                await UniTask.Yield();
            }
        }
        else
        {
            var asyncOp = SceneManager.LoadSceneAsync("Login");
            asyncOp.allowSceneActivation = false;
            while (!asyncOp.isDone)
            {
                if (asyncOp.progress >= 0.9f)
                    asyncOp.allowSceneActivation = true;
                loadingWin.SetProgress(asyncOp.progress * 0.5f);
                await UniTask.Yield();
            }
        }
 
        // Phase 2 (50% ~ 100%): Manager initialization
        await WaitForManagerProgress(loadingWin, 0.5f, 1.0f,
            ConfigManager.Instance.GetLoadingProgress, Main.InitManagers);
 
        loadingWin.SetProgress(1f, true);
        await UniTask.Delay(TimeSpan.FromSeconds(0.5f));
        loadingWin.CloseWindow();
 
        Main.OnSwitchToLoginScene();
 
        currentStage = StageName.Login;
 
        UIManager.Instance.OpenWindowAsync<LaunchBackGroundWin>().ContinueWith(s => {
            UIManager.Instance.OpenWindowAsync<LoginWin>().Forget();
            DumpLoginSceneDiagnostics("ToLoginScene open triggered").ContinueWith(() => {
                if (VersionUtility.Instance.NeedDownAsset() && !AssetVersionUtility.hasDownLoadFullAsset)
                {
                    DownloadHotMgr.Instance.ClearDownloadCache();
                    InGameDownLoad.Instance.inGameDownLoadAllow = true;
                    InGameDownLoad.Instance.TryDownLoad(InGameDownLoad.Dominant.None);
                }
            }).Forget();
        }).Forget();
 
 
 
    }
 
    // 返回登录界面 如断线
    public async UniTaskVoid ReturnToLoginScene()
    {
        UIManager.Instance.DestroyAllUI();
 
        if (AssetSource.isUseAssetBundle)
            await YooAssetService.Instance.LoadSceneAsync("Assets/ResourcesOut/Scenes/Login.unity");
        else
            await SceneManager.LoadSceneAsync("Login");
 
        Main.OnSwitchToLoginScene();
        currentStage = StageName.Login;
 
        UIManager.Instance.OpenWindowAsync<LaunchBackGroundWin>().ContinueWith(s => {
            UIManager.Instance.OpenWindowAsync<LoginWin>().Forget();
            DumpLoginSceneDiagnostics("ToLoginScene open triggered").ContinueWith(() => {
                if (ServerForceExitHintWin.reason != 0)
                {
                    UIManager.Instance.OpenWindowAsync<ServerForceExitHintWin>().Forget();
                }
                // SoundPlayer.Instance.StopBackGroundMusic();
                SoundPlayer.Instance.PlayLoginMusic();
            }).Forget();
        }).Forget();
    }
 
    private async UniTask DumpLoginSceneDiagnostics(string context)
    {
        await UniTask.DelayFrame(2);
        UIManager.Instance.DumpUIDiagnostics(context + " after 2 frames");
        await UniTask.DelayFrame(30);
        UIManager.Instance.DumpUIDiagnostics(context + " after 32 frames");
    }
 
    protected float GetManagerRequestDataProgress()
    {
        if (Main.managers.Count == 0)
        {
            return 1f;
        }
 
        int count = 0;
 
        for (int i = 0; i < Main.managers.Count; i++)
        {
            var manager = Main.managers[i];
 
            if (manager.IsNessaryDataReady())
            {
                count++;
            }
        }
 
        return ((float)count) / ((float)Main.managers.Count);
    }
 
    public async UniTaskVoid ToGameScene()
    {
        UIManager.Instance.DestroyAllUI();
        
        BeforeLoadingGameScene?.Invoke();
 
        // US3: Show loading screen FIRST, then load resources with progress
        LoadingWin loadingWin = await UIManager.Instance.OpenWindowAsync<LoadingWin>();
        InitLoadingWinData(loadingWin);
 
        SoundPlayer.Instance.StopBackGroundMusic();
 
        // Phase 1 (0% ~ 50%): Scene loading
        if (AssetSource.isUseAssetBundle)
        {
            var handle = YooAssetService.Instance.BeginLoadScene("Assets/ResourcesOut/Scenes/Game.unity", LoadSceneMode.Single, LocalPhysicsMode.None, true);
            while (!handle.IsDone)
            {
                if (handle.Progress >= 0.9f)
                    handle.UnSuspend();
                loadingWin.SetProgress(handle.Progress * 0.5f);
                await UniTask.Yield();
            }
        }
        else
        {
            var asyncOp = SceneManager.LoadSceneAsync("Game");
            asyncOp.allowSceneActivation = false;
            while (!asyncOp.isDone)
            {
                if (asyncOp.progress >= 0.9f)
                    asyncOp.allowSceneActivation = true;
                loadingWin.SetProgress(asyncOp.progress * 0.5f);
                await UniTask.Yield();
            }
        }
 
        // Phase 2 (50% ~ 100%): Manager data ready
        await WaitForManagerProgress(loadingWin, 0.5f, 1.0f,
            () => (DTC0403_tagPlayerLoginLoadOK.finishedLogin ? .5f : 0f) + GetManagerRequestDataProgress() * .5f);
 
        loadingWin.SetProgress(1f, true);
        await UniTask.Delay(TimeSpan.FromSeconds(0.5f));
        loadingWin.CloseWindow();
 
        //  加载初始化数据完成
        currentStage = StageName.Game;
 
        Main.OnEnterGameScene();
 
        AfterLoadingGameScene?.Invoke();
 
        UIManager.Instance.OpenWindowAsync<MainWin>().Forget();
 
        //游戏内日志关闭
#if !UNITY_EDITOR
        if (File.Exists(Directory.GetParent(Application.persistentDataPath) + "/Debug") ||
        LocalSave.GetString("#@#BrancH") != string.Empty)
        {
            Debug.unityLogger.logEnabled = true;
        }
        else
        { 
            Debug.unityLogger.logEnabled = false;
        }
 
#endif
    }
 
    protected async UniTask OnLoading(AsyncOperation asyncOperation, Func<float> getLoadingProgress, Func<UniTask> anthorTask = null)
    {
        asyncOperation.allowSceneActivation = false;
 
        LoadingWin loadingWin = await UIManager.Instance.OpenWindowAsync<LoadingWin>();
        InitLoadingWinData(loadingWin);
 
        while (!asyncOperation.isDone)
        {
            if (asyncOperation.progress >= 0.9f)
            {
                asyncOperation.allowSceneActivation = true;
            }
 
            loadingWin.SetProgress(asyncOperation.progress * 0.5f + getLoadingProgress() * 0.5f);
 
            await UniTask.Yield();
        }
 
        float managerProgress = getLoadingProgress();
 
        while (managerProgress < 1f)
        {
            loadingWin.SetProgress(asyncOperation.progress * 0.5f + managerProgress * 0.5f);
 
            await UniTask.Yield();
 
            managerProgress = getLoadingProgress();
        }
 
        if (anthorTask != null)
        {
            await anthorTask();
        }
 
        loadingWin.SetProgress(1f, true);
 
        await UniTask.Delay(TimeSpan.FromSeconds(0.5f));
 
        loadingWin.CloseWindow();
    }
 
    /// <summary>
    /// US3: 等待Manager初始化进度并更新LoadingWin。
    /// </summary>
    private async UniTask WaitForManagerProgress(LoadingWin loadingWin, float startPct, float endPct,
        Func<float> getProgress, Func<UniTask> extraTask = null)
    {
        float managerProgress = getProgress();
 
        while (managerProgress < 1f)
        {
            loadingWin.SetProgress(startPct + managerProgress * (endPct - startPct));
            await UniTask.Yield();
            managerProgress = getProgress();
        }
 
        if (extraTask != null)
        {
            await extraTask();
        }
    }
 
    /// <summary>
    /// US3: 初始化 LoadingWin 数据(从 LaunchWin 继承背景等)。
    /// </summary>
    private void InitLoadingWinData(LoadingWin loadingWin)
    {
        LaunchWin launchWin = UIManager.Instance.GetUI<LaunchWin>();
        if (launchWin != null && launchWin.IsActive() && launchWinData == null)
        {
            launchWinData = launchWin.GetData();
        }
 
        if (launchWinData != null)
        {
            loadingWin.SetData(launchWinData);
            launchWinData = null;
        }
    }
 
    private void OnCloseWindow(UIBase closeUI)
    {
        if (closeUI is LaunchWin)
        {
            launchWinData = (closeUI as LaunchWin).GetData();
        }
    }
}