unity loadsceneasync code example

Example 1: unity loadsceneasync callback

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Example : MonoBehaviour
{
    void Update()
    {
        // Press the space key to start coroutine
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Use a coroutine to load the Scene in the background
            StartCoroutine(LoadYourAsyncScene());
        }
    }

    IEnumerator LoadYourAsyncScene()
    {
        // The Application loads the Scene in the background as the current Scene runs.
        // This is particularly good for creating loading screens.
        // You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
        // a sceneBuildIndex of 1 as shown in Build Settings.

        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Scene2");

        // Wait until the asynchronous scene fully loads
        while (!asyncLoad.isDone)
        {
            yield return null;
        }
    }
}

Example 2: unity LoadSceneAsync get scene

Scene reference; bool isLoading;  IEnumerator LoadScene(string sceneName)     {         this.isLoading = false;          AsyncOperation op = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);         while (this.isLoading)         {             if (op.isDone)             {                 this.isLoading = false;                 this.reference = SceneManager.GetSceneByName(sceneName);             }             yield return 0;         }     }

Tags:

Misc Example