返回游戏或应用程序中当前处于活动状态的场景的名称。
Scene.name 返回一个运行时只读字符串值。name 限制为 244 个字符。场景的默认名称为 /scene/。用户在游戏创建期间更改 name。
以下脚本示例将根据 GUI.Button 点击情况和场景名称来更改场景。要使此示例运行,请执行以下操作:
创建一个包含场景 scene1
和 scene2
的项目。
将以下脚本附加到已添加到 scene1
的 GameObject。
将同一个脚本附加到已添加到 scene2
的 GameObject。
单击 GameObject 并转到 Inspector。
在 My First Scene
字段和 My Second Scene
字段中,输入您要在 scene1
和 scene2
之间进行切换的场景的名称。
在项目中双击 scene1
以将其选中,然后按 scene1
。此时将显示 scene1
场景。
单击 Load Next Scene
按钮,然后将加载 /scene2/。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Example : MonoBehaviour { // These are the Scene names. Make sure to set them in the Inspector window. public string myFirstScene, mySecondScene;
private string nextButton = "Load Next Scene"; private string nextScene; private static bool created = false;
private Rect buttonRect; private int width, height;
void Awake() { Debug.Log("Awake:" + SceneManager.GetActiveScene().name);
// Ensure the script is not deleted while loading. if (!created) { DontDestroyOnLoad(this.gameObject); created = true; } else { Destroy(this.gameObject); }
// Specify the items for each scene. Camera.main.clearFlags = CameraClearFlags.SolidColor; width = Screen.width; height = Screen.height; buttonRect = new Rect(width / 8, height / 3, 3 * width / 4, height / 3); }
void OnGUI() { // Return the current Active Scene in order to get the current Scene name. Scene scene = SceneManager.GetActiveScene();
// Check if the name of the current Active Scene is your first Scene. if (scene.name == myFirstScene) { nextButton = "Load Next Scene"; nextScene = mySecondScene; } else { nextButton = "Load Previous Scene"; nextScene = myFirstScene; }
// Display the button used to swap scenes. GUIStyle buttonStyle = new GUIStyle(GUI.skin.GetStyle("button")); buttonStyle.alignment = TextAnchor.MiddleCenter; buttonStyle.fontSize = 12 * (width / 200);
if (GUI.Button(buttonRect, nextButton, buttonStyle)) { SceneManager.LoadScene(nextScene); } } }
以下示例使用了两个场景,其中一个场景具有长 Scene 名称,包含 244 位数字。另一个场景名为 /testScene/。要使此示例运行,请执行以下操作:
1. 创建一个新的项目。
2. 将默认场景的名称更改为 /testScene/,方法是选中默认场景,然后选择 Assets > Rename。
3. 接下来,创建第二个场景,再次选中该场景并选择 Asset > Rename。使用如下所示的名称。(这是包含 244 个字符的名称“0123456789...0123”)。
4. 创建一个 C# 脚本,将其命名为 /Example.cs/。
5. 将以下脚本文本添加到 /Example.cs/。
6. 接下来,在这两个场景中都添加一个名为 GameObject
的空游戏对象。
7. 最后,将 Example.cs
复制到这两个游戏对象。\
使用 Game
按钮来启动 testScene
场景。此时将显示用于切换场景的 GUI 按钮。
using UnityEngine; using UnityEngine.SceneManagement;
// SceneManagement.SceneManager-name example
public class Example : MonoBehaviour { private Scene scene;
void Start() { scene = SceneManager.GetActiveScene(); Debug.Log("Name: " + scene.name); }
void OnGUI() { if (GUI.Button(new Rect(10, 10, 150, 100), "Change Scene")) { if (scene.name == "testScene") { // The scene to load has a 244 characters name. SceneManager.LoadScene("0123456789012345678901234567890123456789" + "012345678901234567890123456789012345678901234567890123456789" + "012345678901234567890123456789012345678901234567890123456789" + "012345678901234567890123456789012345678901234567890123456789" + "012345678901234567890123"); } else { SceneManager.LoadScene("testScene"); } } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.