如何自动激活TMP_InputField

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

How to automatically activate a TMP_InputField

问题

  1. Unity,我有两个TMP_InputField。我尝试在第一个输入内容后自动将焦点转移到另一个并激活它。我尝试了以下方法,但出现了一些问题。
  2. ```csharp
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using UnityEngine.EventSystems;
  7. public class FocusBox : MonoBehaviour
  8. {
  9. public TMP_InputField b1;
  10. public TMP_InputField b2;
  11. // 在每一帧调用一次
  12. private void Update()
  13. {
  14. if (b1.isFocused)
  15. {
  16. if (b1.text.Length > 0)
  17. {
  18. b2.Select();
  19. b1.DeactivateInputField();
  20. }
  21. }
  22. Debug.Log("在b1输入后b1是否聚焦: " + b1.isFocused);
  23. }
  24. }
  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using TMPro;
  7. using UnityEngine.EventSystems;
  8. public class FocusBox : MonoBehaviour
  9. {
  10. public TMP_InputField b1;
  11. public TMP_InputField b2;
  12. // Update is called once per frame
  13. private void Update()
  14. {
  15. if (b1.isFocused)
  16. {
  17. if (b1.text.Length &gt; 0)
  18. {
  19. b2.Select();
  20. b1.DeactivateInputField();
  21. }
  22. }
  23. Debug.Log(&quot;is b1 focused after : &quot; + b1.isFocused);
  24. }
  25. }
  26. </details>
  27. # 答案1
  28. **得分**: 1
  29. 不特别确定你对“has an input”究竟是什么意思。
  30. 然而,我认为你可以在`TMP_InputField`上使用```OnValueChanged```事件。可以通过检查器分配回调,或者以编程方式进行覆盖。每当你的输入发生变化时,你的回调将被触发。
  31. <details>
  32. <summary>英文:</summary>
  33. Not particularly sure what exactly you mean with &quot;has an input&quot;.
  34. 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.
  35. </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:

确定