【问题标题】:Having Issue with Setting Up multiple Input Field in Unity3d在 Unity3d 中设置多个输入字段时遇到问题
【发布时间】:2016-01-12 13:29:13
【问题描述】:

我目前正在尝试在 Unity 中设置多个输入字段并遇到问题。我只能让一个输入工作(intPressure),但不知道如何添加第二个(钻管)。在我看来,统一一次只允许一个输入字段。我认为只需将其更改为 InputField2 等就可以了,但这似乎不起作用。其他人提到创建一个空的游戏对象并将输入字段插入每个游戏对象。

说实话,我仍然对如何设置第二个输入字段感到困惑。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

    public class UserInput : MonoBehaviour {

        InputField input;

        int intPressure = 0;
        int drillpipe = 0;

        public DataManager data;

        void Start ()    
        {
            var input = gameObject.GetComponent<InputField>();
            var se= new InputField.SubmitEvent();
            se.AddListener(SubmitName);
            input.onEndEdit = se;
        }

        private void SubmitName(string pressure)    
        {

            var pressureScript = 
              GameObject.FindObjectOfType(typeof(DataManager)) as DataManager;

            if (int.TryParse(pressure, out intPressure))
            {
                pressureScript.pressure = intPressure;
            }
            else
            {
                // Parse fail, show an error or something
            }
        }
   }

【问题讨论】:

    标签: c# unity3d user-input


    【解决方案1】:

    我不确定你想要实现这个的方式(使用单个游戏对象),但是如果你有几个游戏对象(每个控件一个对象)嵌套在另一个游戏对象中,你肯定能够做到这一点(让我们称它为 UserInputRoot) 像这样:

      UserInputRoot (has UserInputController script)
      |
      +--- InputPressure (has InputField component)
      |
      +--- InputDrillpipe (has InputField component)
    

    然后,控制脚本将有几个公共 InputField 或几个私有 InputField,在 Start() 或 Awake() 方法中初始化:

    class UserInputController : MonoBehaviour {
        //These can be set from the inspector and controlled from within the script
        public InputField PressureInput;  
        public InputField DrillpipeInput; 
    
        // It would be better to pass this reference through
        // the inspector instead of searching for it every time
        // an input changes
        public DataManager dataManager;
    
        private void Start() {
            // There is no actual need in creating those SubmitEvent objects.
            // You can attach event handlers to existing events without a problem.
            PressureInput.onEndEdit.AddListener(SubmitPressureInput);
            DrillpipeInput.onEndEdit.AddListener(SubmitDrillpipeInput);
        }
    
        private void SubmitDrillpipeInput(string input) {
            int result;
            if (int.TryParse(input, out result)) {
                dataManager.drillpipe = result;
            }
        }
    
        private void SubmitPressureInput(string input) {
            int result;
            if (int.TryParse(input, out result)) {
                dataManager.pressure = result;
            }
        }
    }
    

    顺便说一句,您的代码格式绝对是糟糕的。你必须修复它。

    【讨论】:

    • 说实话,我其实是一个动画师,想学编程。所以我花了一段时间才真正理解它。
    • 使用像 mono-develop 这样的 IDE 来格式化您的代码,尝试使用空格而不是制表符,这样当您在某处复制粘贴时您的代码看起来没问题(将您的 IDE 配置为使用 4 个空格而不是制表符)。由于您使用 C#,因此最好遵循 Microsoft 的编码标准:msdn.microsoft.com/en-us/library/ff926074.aspx 或至少或多或少一致。
    • 我知道这很草率,但我的主要重点是先让代码正常工作,然后再进行格式化。
    • 到目前为止,我实际上有两个游戏对象。每个游戏对象都有一个字段输入。
    • 现在您必须编写一个脚本,该脚本将位于根游戏对象(参见树形图)并在这些游戏对象上使用 InputFields。
    【解决方案2】:

    看起来您显示的脚本已附加到输入字段游戏对象,对吗?您的对象中有一个名为 input 的成员变量,但随后您在 Start 方法中创建了一个新变量 input。与其将脚本附加到您的第一个 InputField,不如在编辑器中创建一个空的游戏对象并将脚本附加到该对象。在脚本中添加两个公共成员:

     public class UserInput : MonoBehaviour {
    
      public InputField PressureInput;
      public InputField DrillPipeInput;
    

    现在弹回编辑器,当您选择空的游戏对象时,您应该会看到两个输入字段。将两个输入字段拖放到每个输入字段的插槽中。现在,当场景开始时,您的 UserInput 脚本将设置输入字段,您可以同时使用它们。

    【讨论】:

    • 好的,让我看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多