sceneName | 要加载的场景的名称或路径。 |
sceneBuildIndex | Build Settings 中要加载场景的索引。 |
mode | 如果为 LoadSceneMode.Single,则系统将在加载场景之前卸载所有当前场景。 |
AsyncOperation 使用 AsyncOperation 确定操作是否已完成。
在后台异步加载场景。
提供的场景名称可以是完成的场景路径(即 Build Settings 窗口中显示的路径),也可以只是场景名称。如果仅提供场景名称,此方法将加载匹配的场景列表中的第一个场景。如果您有多个名称相同但路径不同的场景,应该使用 Build Settings 中的完整场景路径。
Examples of supported formats:
"Scene1"
"Scene2"
"Scenes/Scene3"
"Scenes/Others/Scene3"
"Assets/scenes/others/scene3.unity"
注意:要加载场景的名称不区分大小写。
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; } } }