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
using System;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public enum StageName
{
    Login,
    Game,
}
 
public class StageManager : Singleton<StageManager>, IManager
{
    public StageName currentStage;
 
    public Action AfterLoadingGameScene;
 
    public Action BeforeLoadingGameScene;
 
    // public Action OnSwitchAccount;
 
    public void Init()
    {
 
    }
 
    public void Release()
    {
        AfterLoadingGameScene = null;
        BeforeLoadingGameScene = null;
    }
 
    public async UniTaskVoid ToLoginScene()
    {
        UIManager.Instance.DestroyAllUI();
 
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Login");
 
        await OnLoading(asyncOperation, ConfigManager.Instance.GetLoadingProgress);
 
        Main.OnSwitchToLoginScene();
 
        currentStage = StageName.Login;
        UIManager.Instance.OpenWindow<LaunchBackGroundWin>();
        UIManager.Instance.OpenWindow<LoginWin>();
    }
 
    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();
 
        // ResManager.Instance.PrewarmResources();
 
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Game");
 
        await OnLoading(asyncOperation, () => (DTC0403_tagPlayerLoginLoadOK.finishedLogin ? .5f : 0f) + GetManagerRequestDataProgress() * .5f);
 
        //  加载初始化数据完成
        currentStage = StageName.Game;
 
        Main.OnEnterGameScene();
 
        AfterLoadingGameScene?.Invoke();
 
        UIManager.Instance.OpenWindow<MainWin>();
    }
 
    protected async UniTask OnLoading(AsyncOperation asyncOperation, Func<float> getLoadingProgress, Func<UniTask> anthorTask = null)
    {
        asyncOperation.allowSceneActivation = false;
 
        LoadingWin loadingWin = UIManager.Instance.OpenWindow<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();
    }
}