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; 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(); } 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, GetManagerRequestDataProgress); // 加载初始化数据完成 currentStage = StageName.Game; AfterLoadingGameScene?.Invoke(); UIManager.Instance.OpenWindow(); } protected async UniTask OnLoading(AsyncOperation asyncOperation, Func getLoadingProgress, Func anthorTask = null) { asyncOperation.allowSceneActivation = false; LoadingWin loadingWin = UIManager.Instance.OpenWindow(); 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(); } }