unity 按tab键切换下一个inputfild
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class InputNavigator : MonoBehaviour, ISelectHandler, IDeselectHandler { EventSystem _system; private bool _isSelect = false; void Start() { _system = EventSystem.current; } void Update() { if (Input.GetKeyDown(KeyCode.Tab) && _isSelect) { Selectable next = null; if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp(); } else { next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown(); } if (next != null) { InputField inputfield = next.GetComponent<InputField>(); _system.SetSelectedGameObject(next.gameObject, new BaseEventData(_system)); } else { Debug.LogError("找不到下一个控件"); } } } public void OnSelect(BaseEventData eventData) { _isSelect = true; } public void OnDeselect(BaseEventData eventData) { _isSelect = false; } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步