using System; using Cysharp.Threading.Tasks; using UnityEngine; using UnityEngine.SceneManagement; public enum StageName { Login, Game, } public class StageManager : Singleton, IManager { public StageName currentStage; public Action AfterLoadingGameScene; public Action BeforeLoadingGameScene; // public Action OnSwitchAccount; private LaunchWinData launchWinData = null; public void Init() { UIManager.Instance.OnCloseWindow += OnCloseWindow; } public void Release() { AfterLoadingGameScene = null; BeforeLoadingGameScene = null; UIManager.Instance.OnCloseWindow -= OnCloseWindow; } public async UniTaskVoid ToLoginScene() { UIManager.Instance.DestroyAllUI(); AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Login"); await OnLoading(asyncOperation, ConfigManager.Instance.GetLoadingProgress, Main.InitManagers); Main.OnSwitchToLoginScene(); currentStage = StageName.Login; UIManager.Instance.OpenWindow(); UIManager.Instance.OpenWindow(); } 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(); } protected async UniTask OnLoading(AsyncOperation asyncOperation, Func getLoadingProgress, Func anthorTask = null) { asyncOperation.allowSceneActivation = false; LoadingWin loadingWin = UIManager.Instance.OpenWindow(); LaunchWin launchWin = UIManager.Instance.GetUI(); if (null != launchWin && launchWin.IsActive() && launchWinData == null) { launchWinData = launchWin.GetData(); } if (null != launchWinData) { loadingWin.SetData(launchWinData); launchWinData = null; } 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(); } private void OnCloseWindow(UIBase closeUI) { if (closeUI is LaunchWin) { launchWinData = (closeUI as LaunchWin).GetData(); } } }