yyl
2026-02-11 3f2cd27c5dfb3b450245bf1a37fc1b3414031c7c
Main/Manager/StageManager.cs
@@ -3,6 +3,7 @@
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
using ProjSG.Resource;
public enum StageName
{
@@ -37,14 +38,39 @@
    {
        UIManager.Instance.DestroyAllUI();
        // US3: Show loading screen FIRST, then load resources with progress
        LoadingWin loadingWin = UIManager.Instance.OpenWindow<LoadingWin>();
        InitLoadingWinData(loadingWin);
        // Phase 1 (0% ~ 30%): YooAsset resource preload
        if (AssetSource.isUseAssetBundle)
        {
            AssetBundleUtility.Instance.Sync_LoadAll("maps/Login");
            loadingWin.SetProgress(0.05f);
            await YooAssetService.Instance.LoadAllAssetsAsync<UnityEngine.Object>("Assets/ResourcesOut/maps/Login");
            loadingWin.SetProgress(0.3f);
        }
        // Phase 2 (30% ~ 60%): Scene loading
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Login");
        asyncOperation.allowSceneActivation = false;
        await OnLoading(asyncOperation, ConfigManager.Instance.GetLoadingProgress, Main.InitManagers);
        while (!asyncOperation.isDone)
        {
            if (asyncOperation.progress >= 0.9f)
            {
                asyncOperation.allowSceneActivation = true;
            }
            loadingWin.SetProgress(0.3f + asyncOperation.progress * 0.3f);
            await UniTask.Yield();
        }
        // Phase 3 (60% ~ 100%): Manager initialization
        await WaitForManagerProgress(loadingWin, 0.6f, 1.0f,
            ConfigManager.Instance.GetLoadingProgress, Main.InitManagers);
        loadingWin.SetProgress(1f, true);
        await UniTask.Delay(TimeSpan.FromSeconds(0.5f));
        loadingWin.CloseWindow();
        Main.OnSwitchToLoginScene();
@@ -52,7 +78,6 @@
        UIManager.Instance.OpenWindow<LaunchBackGroundWin>();
        UIManager.Instance.OpenWindow<LoginWin>();
        // SoundPlayer.Instance.StopBackGroundMusic();
        if (VersionUtility.Instance.NeedDownAsset() && !AssetVersionUtility.hasDownLoadFullAsset)
        {
@@ -111,15 +136,41 @@
        
        BeforeLoadingGameScene?.Invoke();
        // ResManager.Instance.PrewarmResources();
        // US3: Show loading screen FIRST, then load resources with progress
        LoadingWin loadingWin = UIManager.Instance.OpenWindow<LoadingWin>();
        InitLoadingWinData(loadingWin);
        // Phase 1 (0% ~ 30%): YooAsset resource preload
        if (AssetSource.isUseAssetBundle)
        {
            AssetBundleUtility.Instance.Sync_LoadAll("maps/Game");
            loadingWin.SetProgress(0.05f);
            await YooAssetService.Instance.LoadAllAssetsAsync<UnityEngine.Object>("Assets/ResourcesOut/maps/Game");
            loadingWin.SetProgress(0.3f);
        }
        SoundPlayer.Instance.StopBackGroundMusic();
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Game");
        await OnLoading(asyncOperation, () => (DTC0403_tagPlayerLoginLoadOK.finishedLogin ? .5f : 0f) + GetManagerRequestDataProgress() * .5f);
        SoundPlayer.Instance.StopBackGroundMusic();
        // Phase 2 (30% ~ 60%): Scene loading
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Game");
        asyncOperation.allowSceneActivation = false;
        while (!asyncOperation.isDone)
        {
            if (asyncOperation.progress >= 0.9f)
            {
                asyncOperation.allowSceneActivation = true;
            }
            loadingWin.SetProgress(0.3f + asyncOperation.progress * 0.3f);
            await UniTask.Yield();
        }
        // Phase 3 (60% ~ 100%): Manager data ready
        await WaitForManagerProgress(loadingWin, 0.6f, 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;
@@ -150,18 +201,7 @@
        asyncOperation.allowSceneActivation = false;
        LoadingWin loadingWin = UIManager.Instance.OpenWindow<LoadingWin>();
        LaunchWin launchWin = UIManager.Instance.GetUI<LaunchWin>();
        if (null != launchWin && launchWin.IsActive() && launchWinData == null)
        {
            launchWinData = launchWin.GetData();
        }
        if (null != launchWinData)
        {
            loadingWin.SetData(launchWinData);
            launchWinData = null;
        }
        InitLoadingWinData(loadingWin);
        while (!asyncOperation.isDone)
        {
@@ -198,6 +238,45 @@
        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)