英文:
How to automatically activate a TMP_InputField
问题
在Unity中,我有两个TMP_InputField。我尝试在第一个输入内容后自动将焦点转移到另一个并激活它。我尝试了以下方法,但出现了一些问题。
```csharp
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;
public class FocusBox : MonoBehaviour
{
public TMP_InputField b1;
public TMP_InputField b2;
// 在每一帧调用一次
private void Update()
{
if (b1.isFocused)
{
if (b1.text.Length > 0)
{
b2.Select();
b1.DeactivateInputField();
}
}
Debug.Log("在b1输入后b1是否聚焦: " + b1.isFocused);
}
}
<details>
<summary>英文:</summary>
I have two TMP_InputField in unity. I am trying to automatically focus on and activate the other once the first one has an input. I have tried the following but for some reasons, it's not working for me.
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;
public class FocusBox : MonoBehaviour
{
public TMP_InputField b1;
public TMP_InputField b2;
// Update is called once per frame
private void Update()
{
if (b1.isFocused)
{
if (b1.text.Length > 0)
{
b2.Select();
b1.DeactivateInputField();
}
}
Debug.Log("is b1 focused after : " + b1.isFocused);
}
}
</details>
# 答案1
**得分**: 1
不特别确定你对“has an input”究竟是什么意思。
然而,我认为你可以在`TMP_InputField`上使用```OnValueChanged```事件。可以通过检查器分配回调,或者以编程方式进行覆盖。每当你的输入发生变化时,你的回调将被触发。
<details>
<summary>英文:</summary>
Not particularly sure what exactly you mean with "has an input".
However, I think you could use the ```OnValueChanged``` event on your `TMP_InputField`. Either assign a Callback via the Inspector or overwrite it programatically. Everytime your input changes, your Callback will be triggered.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论