【Unity】InputField间的快速切换

可选基类(Selectable)导航选项

该功能主要基于可选基类(Selectable)导航选项(Navigation)完成实现

Navigation选项功能:

导航选项表示如何控制 UI 元素在播放模式中的导航。

属性功能
None无导航。
Horizontal水平导航。
Vertical垂直导航。
Automatic自动导航。
Explicit在此模式下,可显式指定将要导航到的位置。
Visualize选择 Visualize 可以显示当前已经设置的导航位置。

功能实现:

public class QuicklySwitchInputField : MonoBehaviour
    {
        private EventSystem system;
        [SerializeField]
        private Transform inputFieldRootTrans;
        private Selectable[] selectables;
        private Selectable current;
        private Selectable next;
        void Start()
        {
            system = EventSystem.current;
            inputFieldRootTrans = transform;
            selectables = transform.GetComponentsInChildren<Selectable>();
        }

        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Tab))
            {
                //下一个ui控件
                next = null;
                //当前的ui控件
                current = null;
                                       
                if (JudgeHasInputFielSelected())
                {   //判断当前选中的物体是否可用
                    if (system.currentSelectedGameObject.activeInHierarchy)
                    {
                        current = system.currentSelectedGameObject.GetComponent<Selectable>();
                    }
                }
                if (current != null)
                {
                    //当按住shift,根据当前Select On Up或Select On Left 移动
                    if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
                    {
                        next = current.FindSelectableOnUp();
                        if (next == null)
                        {
                            next = current.FindSelectableOnLeft();
                        }
                    }
                    //根据当前Select On Down或Select On Right 移动
                    else
                    {
                        next = current.FindSelectableOnDown();
                        if (next == null)
                        {
                            next = current.FindSelectableOnRight();
                        }
                    }
                }
                else
                {
                    if (selectables.Length > 0)
                    {
                        next = selectables[0];
                    }
                }

                if (next != null)
                {
                    next.Select();
                }
            }
        }
        /// <summary>
        /// 判断当前是否有正在选中的指定的物体   
        /// </summary>
        private bool JudgeHasInputFielSelected()
        {
            foreach (var item in selectables)
            {
                if (system.currentSelectedGameObject.Equals(item.gameObject))
                {
                    return true;
                }
            }
            return false;
        }
	}

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值