unity loadsceneasync .completed code example
Example 1: unity loadsceneasync callback
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Example : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(LoadYourAsyncScene());
}
}
IEnumerator LoadYourAsyncScene()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Scene2");
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; } }