修复UI系统BUG
This commit is contained in:
@@ -11,7 +11,7 @@ namespace GameFramework
|
||||
///
|
||||
/// 搭建:把 SceneLoader 挂到一个空物体上(放第一个场景 MainMenu 里),
|
||||
/// 它会 DontDestroyOnLoad。transitionImage 是它子物体 Canvas 上的 Image(全屏黑),
|
||||
/// Canvas 设为 Screen Space - Overlay、sortingOrder 设很高(如 1000)。
|
||||
/// Canvas 设为 Screen Space - Overlay、sortingOrder 设很高(如 9999)。
|
||||
/// </summary>
|
||||
public class SceneLoader : PersistentSingleton<SceneLoader>
|
||||
{
|
||||
@@ -25,18 +25,28 @@ namespace GameFramework
|
||||
[SerializeField] private string scoringScene = "Scoring";
|
||||
|
||||
private Color color;
|
||||
private bool isLoading = false;
|
||||
|
||||
public void LoadGameplayScene() => Load(gameplayScene);
|
||||
public void LoadMainMenuScene() => Load(mainMenuScene);
|
||||
public void LoadScoringScene() => Load(scoringScene);
|
||||
public void Load(string sceneName)
|
||||
{
|
||||
if (isLoading) return;
|
||||
isLoading = true;
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(LoadingCoroutine(sceneName));
|
||||
}
|
||||
|
||||
private IEnumerator LoadingCoroutine(string sceneName)
|
||||
{
|
||||
if (transitionImage == null)
|
||||
{
|
||||
Debug.LogWarning("[SceneLoader] transitionImage 未赋值! " +
|
||||
"请在 SceneLoader 子物体上创建 Canvas (Overlay, SortOrder=9999) + 全屏黑色 Image," +
|
||||
"拖到 SceneLoader 的 transitionImage 字段。");
|
||||
}
|
||||
|
||||
var loadingOperation = SceneManager.LoadSceneAsync(sceneName);
|
||||
loadingOperation.allowSceneActivation = false;
|
||||
|
||||
@@ -46,20 +56,32 @@ namespace GameFramework
|
||||
color = transitionImage.color;
|
||||
color.a = 0f;
|
||||
|
||||
// 淡入到黑
|
||||
// 淡入到黑(LoadSceneAsync 在后台并行加载)
|
||||
while (color.a < 1f)
|
||||
{
|
||||
color.a = Mathf.Clamp01(color.a + Time.unscaledDeltaTime / fadeTime);
|
||||
transitionImage.color = color;
|
||||
yield return null;
|
||||
}
|
||||
color.a = 1f;
|
||||
transitionImage.color = color;
|
||||
|
||||
// 淡入完成后,确保场景已加载就绪(大多数情况下已经加载好了)
|
||||
yield return new WaitUntil(() => loadingOperation.progress >= 0.9f);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 没有过渡图,直接等加载
|
||||
yield return new WaitUntil(() => loadingOperation.progress >= 0.9f);
|
||||
}
|
||||
|
||||
// 等异步加载到 90%
|
||||
yield return new WaitUntil(() => loadingOperation.progress >= 0.9f);
|
||||
|
||||
loadingOperation.allowSceneActivation = true;
|
||||
|
||||
// 等待场景切换完全完成(旧场景卸载 + 新场景渲染就绪)
|
||||
// 没有这一步会在场景交接时闪一帧旧场景的内容
|
||||
yield return null;
|
||||
yield return null;
|
||||
|
||||
if (transitionImage != null)
|
||||
{
|
||||
// 淡出到透明
|
||||
|
||||
Reference in New Issue
Block a user