1

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

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

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

下面编辑:

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

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

在此处输入图像描述

4

1 回答 1

0

您可以在加载新的“问题”后InputField通过调用来自动对焦。ActivateInputField不确定,但也许你也需要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

于 2021-04-23T06:17:14.670 回答