【问题标题】:Unity InputField box always in focusUnity InputField 框始终处于焦点
【发布时间】:2021-07-17 08:22:18
【问题描述】:

我正在尝试统一创建一个简单的数学游戏,玩家可以在其中输入数学问题的答案。基本上用户所能做的就是输入答案,然后输入。

目前,我有用户可以进入输入字段,输入后,当出现下一个问题时,他们必须再次物理单击输入字段框才能输入答案。

有没有办法让您可以连续输入输入字段而无需重新单击输入字段?

在下面编辑:

我输入了下面的代码。如果出现错误,如何获取输入字段无法将类型输入字段隐式转换为游戏对象。

GameObject inputField;
void Start()
{
    GameObject inputField = gameObject.GetComponent<InputField>();
    inputField.Select();
    inputField.ActivateInputField();

【问题讨论】:

    标签: unity3d input textbox enter


    【解决方案1】:

    您可以在加载新的“问题”后调用ActivateInputField 来自动聚焦InputField。不确定,但也许你还需要先Select

    // Not sure if this is needed
    theInputField.Select();
    
    theInputField.ActivateInputField();
    

    或者,您也可以收听提交并执行例如

    private void Start ()
    {
        // Make your InputField accept multiple lines
        // See https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.InputField.LineType.MultiLineNewline.html
        theInputField.lineType = InputField.LineType.MultiLineNewline;
        // Instead of waiting for submissions use the return key
        theInputField.onValidateInput += MyValidate;
    }
    
    private char MyValidate(string currentText, int currentIndex, char addedCharToValidate)
    {
        // Checks if a new line is entered
        if (addedCharToValidate == '\n')
        {
            // if so treat it as submission
            // -> clear the input and evaluate
            EvaluateInput(theInputField.text.Trim('\0'));
            theInputField.text = "";
            return '\0';
        }
        return addedCharToValidate;
    } 
    

    所以实际上用户根本不会离开InputField

    【讨论】:

    • 为什么你的 inputFieldGameObject 而不仅仅是 InputField inputField; ?在Start 中这样做可能还不够......你会在每次提交后这样做;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    相关资源
    最近更新 更多