如何使场景加载统一暂停一秒钟

How to make a scene loading Pause for a second in unity

下面是我在 Unity (Asyn) 中加载下一个场景的代码。它工作正常。但是我需要它在加载后等待一秒钟才能显示场景。怎么做

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadingScreenScript : MonoBehaviour
{
    [SerializeField]
    private Image _progressBar;
    // Start is called before the first frame update
    void Start()
    {
     StartCoroutine(LoadAsyncOperatiom());   
    }


    IEnumerator LoadAsyncOperatiom()
    {
        AsyncOperation gameLevel = SceneManager.LoadSceneAsync(2);

        while (gameLevel.progress < 1)
        {
            _progressBar.fillAmount = gameLevel.progress;
            yield return new WaitForEndOfFrame();
        } 
        transition.SetTrigger("Start");
        
        //yield return new WaitForSeconds(transitionTime);
    }
}

所以默认情况下,Unity 在加载场景后立即进入场景(并离开旧场景)。但是,您可以使用 AsyncOperation.allowSceneActivation.

更改此行为

需要注意的一点是,场景加载的 进度 将在 [0f-0.9f] 之间,其中 0.9f 表示完全加载但不活动的场景.它不会到达 1.0f,直到您再次将 AsyncOperation.allowSceneActivation 设置为 true,并允许场景激活。

因此在等待场景加载时,您必须检查gameLevel.progress < 0.9f,而不是1

您可以将代码更改为:

IEnumerator LoadAsyncOperatiom()
{
    AsyncOperation gameLevel = SceneManager.LoadSceneAsync(2);
    gameLevel.allowSceneActivation = false; // stop the level from activating

    while (gameLevel.progress < 0.9f)
    {
        _progressBar.fillAmount = gameLevel.progress;
        yield return new WaitForEndOfFrame();
    } 
    transition.SetTrigger("Start");
    
    yield return new WaitForSeconds(transitionTime);

    gameLevel.allowSceneActivation = true; // this will enter the level now
}

它应该可以工作。