Savad
Savad

Reputation: 1306

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

Below is my code to load the next scene in Unity (Asyn). It's working fine. But I need it to wait for a second after loading before showing the scene. How to do it

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);
    }
}

Upvotes: 1

Views: 2359

Answers (2)

Denis Sivtsov
Denis Sivtsov

Reputation: 164

Exist the famous BUG which demands to put the

yield return null;

in the beginning the Corutine before any others code lines. I found this mandatory in UNITY 2020.3.34f1. The same order is used in UNITY DOC also. The Coroutine should look something like this:

{
 yield return null;

 AsyncOperation gameLevel = SceneManager.LoadSceneAsync(2);
 gameLevel.allowSceneActivation = false; // stop the level from activating
 while (gameLevel.progress < 0.9f)
 //...
}

And @Jay wrote right in this case the gameLevel.progress can achieve only 0.9f

P.S> Important to remember if you make two and more "async operation" (two async loads or one async load and other async unload) and put on hold ("allowSceneActivation = false") the finishing of the first async operation (see link on UnityDoc upper):

the AsyncOperation queue is stalled.

Upvotes: 1

Jay
Jay

Reputation: 2946

So by default, Unity enters the scene (and leaves the old one) as soon as the scene is loaded. However, you can change this behaviour using AsyncOperation.allowSceneActivation.

Something to note about this, progress of the scene load will be between [0f-0.9f], where 0.9f represents a fully loaded, but inactive, scene. It will not reach 1.0f until you set AsyncOperation.allowSceneActivation to true again, and allow the scene to activate.

Therefore when waiting for the scene to load, you must check against gameLevel.progress < 0.9f, rather than 1.

You can change your code to this:

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
}

And it should work.

Upvotes: 1

Related Questions