【问题标题】:How to change scene in unity based on the value from inputfield如何根据输入字段的值统一更改场景
【发布时间】:2019-05-05 06:05:55
【问题描述】:

我想根据输入字段中输入的值统一打开一个场景。

例如:

如果用户输入 4,将加载场景名称编号 如果用户输入 5,将加载场景名称 numbers1

【问题讨论】:

    标签: c# unity3d


    【解决方案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。

    【讨论】:

      【解决方案2】:

      要获取更改的文本事件,您可以使用文本字段 https://docs.unity3d.com/Manual/script-InputField.html 上的 OnValueChange 属性

      要加载场景,请使用 SceneManager.LoadScene("SceneName") 方法 https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

      【讨论】:

        【解决方案3】:

        您可以通过引用 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
        } 
        

        【讨论】:

          猜你喜欢
          • 2012-06-11
          • 2021-07-05
          • 2011-01-23
          • 2011-10-21
          • 2021-02-28
          • 2013-12-07
          • 2016-05-17
          • 2017-12-30
          • 2019-07-10
          相关资源
          最近更新 更多