UGUI Tab鍵切換InputField

需求

遊戲登錄界面輸入框之間的跳轉用tab去實現

代碼

    using UnityEngine;
    using System.Collections;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    /// <summary>
    /// UGUI Tab鍵切換InputField
    /// </summary>
    public class InputNavigator : MonoBehaviour,ISelectHandler,IDeselectHandler {

        private EventSystem system;
        private bool isSelect = false;
        void Start () {
            system = EventSystem.current;
        }

        void Update () {
            if (Input.GetKeyDown(KeyCode.Tab) && isSelect)
            {
                Selectable next = null;
                 var sec=system.currentSelectedGameObject.GetComponent<Selectable>();
                if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
                {
                    next = sec.FindSelectableOnUp();
                    if (next == null)
                        next = sec;
                }
                else
                {
                    next = sec.FindSelectableOnDown();
                    if (next == null)
                        next = sec;
                }

                if (next != null)
                {
                    var inputField = next.GetComponent<InputField>();
                    if (inputField == null) return;
                    system.SetSelectedGameObject(next.gameObject,new BaseEventData(system));
                }
            }
        }

        public void OnSelect(BaseEventData eventData)
        {
            isSelect = true;
        }

        public void OnDeselect(BaseEventData eventData)
        {
            isSelect = false;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章