public string name ;

Descripción

Returns the name of the Scene.

//This script changes the Scene depending on Button clicks and the Scene’s name

//To make this work, attach this script to a GameObject //Click on the GameObject and go to the Inspector. Go to the "My First Scene" field and "My Second Scene" field and type in the name of your Scenes you would like to switch between. //Create a Button (Create>UI>Button) and attach this in the "My Button" field of the Inspector. //Place a Object.DontDestroyOnLoad script on your Canvas to ensure the Button isn’t deleted. You may want to add to this to remove copies on a second load. //Make sure both of your Scenes have an EventSystem as a child of your Canvas (Create>UI>Event System).

using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;

public class SceneManagementSceneNameExample : MonoBehaviour { //These are your Scene names. Make sure to set them in the Inspector window public string m_MyFirstScene, m_MySecondScene; Scene m_Scene; public Button m_MyButton;

void Awake() { //Ensure the script is not deleted while loading DontDestroyOnLoad(this);

//Make sure there are copies are not made of the GameObject when it isn't destroyed if (FindObjectsOfType(GetType()).Length > 1) //Destroy any copies Destroy(gameObject);

//Call the MyButton function when you click the Button m_MyButton.onClick.AddListener(MyButton); }

void Update() { //Return the current Active Scene in order to get the current Scene's name m_Scene = SceneManager.GetActiveScene();

//Check if the current Active Scene's name is your first Scene if (m_Scene.name == m_MyFirstScene) //Change the Text of the Button to say "Load Next Scene" m_MyButton.GetComponentInChildren<Text>().text = "Load Next Scene"; //Otherwise change the Text to "Load Previous Scene" else m_MyButton.GetComponentInChildren<Text>().text = "Load Previous Scene"; }

void MyButton() { //Check if the current Active Scene's name is your first Scene if (m_Scene.name == m_MyFirstScene) { //Load your second Scene SceneManager.LoadScene(m_MySecondScene); }

//Check if the current Active Scene's name is your second Scene's name if (m_Scene.name == m_MySecondScene) { //Load your first Scene SceneManager.LoadScene(m_MyFirstScene); } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961