如何自动激活TMP_InputField

huangapple go评论131阅读模式
英文:

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&#39;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 &gt; 0)
                {
                    b2.Select();
                    b1.DeactivateInputField();
                }
    
            }
            Debug.Log(&quot;is b1 focused after : &quot; + b1.isFocused);
    
        }
    
    }



</details>


# 答案1
**得分**: 1

不特别确定你对“has an input”究竟是什么意思。

然而,我认为你可以在`TMP_InputField`上使用```OnValueChanged```事件。可以通过检查器分配回调,或者以编程方式进行覆盖。每当你的输入发生变化时,你的回调将被触发。

<details>
<summary>英文:</summary>

Not particularly sure what exactly you mean with &quot;has an input&quot;.

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>



huangapple
  • 本文由 发表于 2023年3月9日 18:46:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定