Files
Cielonos/Assets/Scripts/Core/SceneManagement/Loading/LoadingSceneManager.cs
SoulliesOfficial 50ee502684 完善
2026-02-13 09:22:11 -05:00

73 lines
2.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Cielonos.Core.SceneManagement
{
public class LoadingSceneManager : MonoBehaviour
{
[Header("UI References")]
[SerializeField] private TMP_Text progressText;
[Header("Settings")]
[SerializeField] private float minimumLoadTime = 1.0f; // 防止加载太快闪屏强制显示至少1秒
private void Start()
{
// 获取目标场景
string targetScene = SceneBus.TargetSceneName;
if (string.IsNullOrEmpty(targetScene))
{
Debug.LogError("LoadingController: Target scene is null! Returning to Menu.");
// 这里可以做一个回滚逻辑,比如回 Menu
return;
}
StartCoroutine(LoadSceneAsync(targetScene));
}
private IEnumerator LoadSceneAsync(string sceneName)
{
// 开始异步加载目标场景
// 注意:这里是 LoadSceneMode.Single因为是从 Menu 切到 MainGame需要彻底销毁 Menu
AsyncOperation op = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
// 阻止自动跳转,为了我们可以控制进度条到 100% 再跳转
op.allowSceneActivation = false;
float timer = 0f;
while (!op.isDone)
{
timer += Time.deltaTime;
// op.progress 在 allowSceneActivation = false 时最大只能到 0.9
float progress = Mathf.Clamp01(op.progress / 0.9f);
// 只有当加载进度到了 0.9 且 满足了最小等待时间,才允许完成
if (progress >= 1f && timer >= minimumLoadTime)
{
// 可以在这里做一个 "Press Any Key to Continue" 的逻辑,或者直接跳转
UpdateUI(1f);
op.allowSceneActivation = true;
}
else
{
// 为了视觉平滑,我们可以取 真实进度 和 模拟时间进度 的较小值
// 或者简单粗暴直接显示 progress
UpdateUI(progress);
}
yield return null;
}
}
private void UpdateUI(float progress)
{
//if (_progressBar != null) _progressBar.value = progress;
if (progressText != null) progressText.text = $"Loading... {(progress * 100):F0}%";
}
}
}