[英]How to change scene in unity based on the value from inputfield
我想根据输入字段中输入的值打开一个统一的场景。
例如:
如果用户输入4,如果用户输入5,将加载场景名称编号,将加载场景名称编号1
你必须调用SceneManager.LoadScene,这里是参考: https : //docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
为了使这样的东西工作,你需要阅读输入。 这可以通过输入字段完成,如下所示: https : //docs.unity3d.com/ScriptReference/UI.InputField-text.html
或者可能是下拉列表: https : //docs.unity3d.com/Manual/script-Dropdown.html
然后设置一个按钮,它将解析输入并调用函数SceneManager.LoadScene。
要获取更改的文本事件,您可以在文本字段https://docs.unity3d.com/Manual/script-InputField.html上使用OnValueChange属性。
要加载场景,请使用SceneManager.LoadScene(“SceneName”)方法https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
您可以通过引用InputField并从按钮事件调用ChangeSceneBasedOnInputField()
来这样做
public UnityEngine.UI.InputField sceneInputField; //Refence to your inputField
public void ChangeSceneBasedOnInputField() //Code that needs to be called
{
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneInputField.text);
//This way you can type either the index of the scene or it's name
//Will throw an error if it can`t find the scene
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.