场景加载
场景加载
普通加载
SceneManager.LoadScene("Scenes/SampleScene1");
同时加载
SceneManager.LoadScene("Scenes/SampleScene1",LoadSceneMode.Additive);
异步加载
using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; public class Test : MonoBehaviour { void Start() { StartCoroutine(Load()); } private IEnumerator Load() { AsyncOperation asyncOperation=SceneManager.LoadSceneAsync("Scene/Cut"); asyncOperation.allowSceneActivation = false; while (asyncOperation.progress < 0.9f) { Debug.Log("Current progress is " + asyncOperation.progress); yield return null; } asyncOperation.allowSceneActivation = true; if (asyncOperation.isDone) { Debug.Log("Finished load and skip."); } else { Debug.Log("Not finished"); } } }
加载场景时保留物体
using UnityEngine; using UnityEngine.SceneManagement; public class Test : MonoBehaviour { void Start() { GameObject capsule = GameObject.Find("Capsule"); DontDestroyOnLoad(capsule); SceneManager.LoadScene("Scenes/SampleScene1"); } }