UGUI中按Tab切换InputField

来源:互联网 发布:mac适合做java开发吗 编辑:程序博客网 时间:2024/05/29 19:05

目前的UGUI在InputField里即使设置了Navigation也是不可以用键盘切换上下,所以自己写了个组件实现这个功能

前提条件是要在InputField里设置Navigation,也就是必须能找到上一个控件和下一个控件

然后将以下脚本挂在需要切换的InputField上,用Tab和Shift+Tab切换:

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();                if (next == null) next = system.lastSelectedGameObject.GetComponent<Selectable>();            }            else            {                next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();                if (next == null) next = system.firstSelectedGameObject.GetComponent<Selectable>();            }            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;    }}


原创粉丝点击