首先创建保存两个Scene,分别是Scene001、Scene002:
Scene001,放一个脚本,以及一个Cube:
Scene002,放若干个Cube,以便区分第一个场景:
然后打开菜单栏上的 File -> Build Settings... :
打开各个场景并且按下上图的 “Add Open Scenes” 按钮,就可以将场景加入到设置中。如下图:
脚本代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Test001 : MonoBehaviour {
void OnGUI(){
GUILayout.Label ("当前场景: " + Application.loadedLevelName);
if (GUILayout.Button("点击进入场景")) {
SceneManager.LoadScene ("Scene002");
}
}
}
解说:
SceneManager.LoadScene
public static void LoadScene(int sceneBuildIndex, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
public static void LoadScene(string sceneName, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
Parameters
sceneName
Name or path of the scene to load.
sceneBuildIndex
Index of the scene in the Build Settings to load.
mode
Allows you to specify whether or not to load the scene additively. See LoadSceneMode for more information about the options.
LoadSceneMode
Single -> Closes all current loaded scenes and loads a scene.(关闭所有当前加载的场景并加载场景。)
Additive -> Adds the scene to the current loaded scenes.(将场景添加到当前加载的场景中。)