【问题标题】:How to make a scene loading Pause for a second in unity如何使场景加载暂停一秒钟
【发布时间】:2021-08-27 11:51:27
【问题描述】:

下面是我在 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);
    }
}

【问题讨论】:

  • yield return new WaitForSeconds(transitionTime);移动上方transition.SetTrigger("Start");?

标签: unity3d asynchronous loading wait scene


【解决方案1】:

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

需要注意的是,场景负载的progress 将介于[0f-0.9f] 之间,其中0.9f 表示完全加载但处于非活动状态的场景。在您再次将AsyncOperation.allowSceneActivation 设置为true 并允许场景激活之前,它不会到达1.0f

因此在等待场景加载时,您必须检查gameLevel.progress &lt; 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
}

它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 2016-05-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多