Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading new scene in background

Tags:

c#

unity3d

I'm creating a Unity application targeting the Samsung Gear VR. I currently have two scenes:

  1. The initial scene
  2. Second scene, with big quantity of data (it takes too much time to load the scene).

From the first scene, I want to load the second scene in background, and switch to it once it has been loaded. While the new scene is loading in background, the user should keep the ability to move their head to see any part of the VR environment.

I'm using SceneManager.LoadSceneAsync but it's not working:

// ...

StartCoroutiune(loadScene());

// ...

IEnumerator loadScene(){
        AsyncOperation async = SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Single);
        async.allowSceneActivation = false;
        while(async.progress < 0.9f){
              progressText.text = async.progress+"";
        }
       while(!async.isDone){
              yield return null;
        }
        async.allowSceneActivation = true;
}

With that code, the scene never changes.

I've tried this the typical SceneManager.LoadScene("name") in which case the scene changes correctly after 30 seconds.

like image 290
adlagar Avatar asked Feb 05 '23 22:02

adlagar


1 Answers

This should work

    while(async.progress < 0.9f){
          progressText.text = async.progress.ToString();
          yield return null;
    }

Secondly, I've seen cases where isDone is never set to true, unless the scene has activated. Remove these lines:

    while(!async.isDone){
          yield return null;
    }

On top of that, you are locking your code in that first while loop. Add a yield so the application can continue loading your code.

So your entire code looks like this:

IEnumerator loadScene(){
    AsyncOperation async = SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Single);
    async.allowSceneActivation = false;
    while(async.progress <= 0.89f){
          progressText.text = async.progress.ToString();
          yield return null;
    }
    async.allowSceneActivation = true;
}

The biggest culprit to your problem is the locking in the first while loop, though.

like image 132
Riaan Walters Avatar answered Feb 08 '23 12:02

Riaan Walters