我正在尝试创建一个简单的数学游戏,玩家输入一个数学问题的答案。基本上,用户所能做的就是输入答案,然后输入。
目前,我有它的用户可以进入输入栏,当他们进入后,当下一个问题出现时,他们必须再次物理点击输入栏框来输入答案。
有没有一种方法可以让你连续地输入输入字段,而不需要重新单击输入字段?
在下面编辑:
我输入了下面的代码。如果我得到的错误不能隐式地将类型inputfield转换为gameobject,我该如何获取inputfield?
GameObject inputField;
void Start()
{
GameObject inputField = gameObject.GetComponent<InputField>();
inputField.Select();
inputField.ActivateInputField();
发布于 2021-04-23 14:17:15
在加载了一个新的“问题”之后,你可以通过调用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
。
https://stackoverflow.com/questions/67224653
复制相似问题