【发布时间】:2019-05-05 06:05:55
【问题描述】:
我想根据输入字段中输入的值统一打开一个场景。
例如:
如果用户输入 4,将加载场景名称编号 如果用户输入 5,将加载场景名称 numbers1
【问题讨论】:
我想根据输入字段中输入的值统一打开一个场景。
例如:
如果用户输入 4,将加载场景名称编号 如果用户输入 5,将加载场景名称 numbers1
【问题讨论】:
你必须调用 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
}
【讨论】: