首先创建保存两个Scene,分别是Scene001、Scene002:
33CF388C-BA85-4B3D-A3C5-981444EF7F89.pngScene001,放一个脚本,以及一个Cube:
EF065768-8BFF-445A-8AF0-1E582895885E.png
Scene002,放若干个Cube,以便区分第一个场景:
FA7D7FF2-8764-4A67-8F26-07FC7F7B06BF.png然后打开菜单栏上的 File -> Build Settings... :
image.png打开各个场景并且按下上图的 “Add Open Scenes” 按钮,就可以将场景加入到设置中。如下图:
image.png脚本代码:
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.(将场景添加到当前加载的场景中。)
网友评论