UGUI用Tab键来控制InputField切换

    这里我介绍两种方法,第一种很简单,但是更加试用与两个InputFile之间的切换,下面就让三藏来说说原理:

还是直接上代码吧微笑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    //这个是输入账号的InputField
    public InputField accountInputField;
    这个是输入密码的InputField
    public InputField passwordInputField;
    // Use this for initialization
    void Start()
    {

    }
    /// <summary>
    /// 按下Tab的时候执行的方法,在Update()中调用就可以了
    /// </summary>
    private void GetTabDown()
    {

        if (Input.GetKeyDown(KeyCode.Tab))
        {
            //accountInputField当前是否有焦点并且能够处理事件。
            if (accountInputField.isFocused)
            {
                //让passwordInputField能够处理事件
                passwordInputField.Select();
            }
            //和上面反过来
            else if (passwordInputField.isFocused)
            {
                accountInputField.Select();
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        GetTabDown();
    }
}

这个脚本就挂在场景中的空物体上就好了,把accountInputField和passwordInputField拖上去就好了。

这就是简单的第一种方法。


下面第二种方法,网上也有很多,我也再发一次,他适合多个InputField之间切换,还有个前提是,所有要切换的InputField或者Button啥的,都要设置一个东西,这个东西就是Nauigation,也就是下图

都设置成默认的,记住,如果你不想切换到Button,例如我上面的截图上的LoginButton,我不想在切到最后一个InputField的时候再切换切到LoginButton,那么这个时候我们就要把你不想切换到的那一个的Nauigation调成None,如下图

就是这个东东,然后上代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class InputFieldController : MonoBehaviour,ISelectHandler ,IDeselectHandler 
{
    private bool isSelect = false;
    private EventSystem system;
	// Use this for initialization
	void Start () {
        system = EventSystem.current;
	}
    private void GetTabDown()
    {
        if (Input.GetKeyDown (KeyCode.Tab )&&isSelect )
        {
            //下一个要切换到的
            Selectable next = null;
            //现在正处在能够处理事件的
            Selectable now = system.currentSelectedGameObject.GetComponent<Selectable>();
            //找到现在的下一个
            next = now.FindSelectableOnDown();
            if (next ==null )
            {
                print("没有下一个了");
            }
            //让下一个能够处理事件
            system.SetSelectedGameObject(next.gameObject);
        }
    }
	// Update is called once per frame
	void Update () {
        GetTabDown();
	}
   /// <summary>
    /// 实现ISelectHandler接口
   /// </summary>
   /// <param name="eventData"></param>
    public void OnSelect(BaseEventData eventData)
    {
        isSelect = true;
    }
    /// <summary>
    /// 实现IDeselectHandler接口
    /// </summary>
    /// <param name="eventData"></param>
    public void OnDeselect(BaseEventData eventData)
    {
        isSelect = false;
    }
}

    然后把这个脚本挂在你要切换的InputField上就可以了。

    这个时候要注意,切到最后一个的时候,它是不会回头的,要自己设置,怎么设置呢,当然,还是在Nauigation里面,找到你切换到的最后一个InputField,我这里是VerificationCodeInputField即验证码输入框,怎么设置呢,看截图

就是把它的Navigation类型设置成Explicit,然后把Select On Down设置成你的第一个InputField,我这里是AccountInputField。保险起见,把Select On Up

设置成它上面一个,我这里是PasswordInputField,然后就好啦。

    第一次写博客,有写错了的地方希望大家多多指教,大家互相学习。

下面是项目文件,需要的小伙伴可以下载一下点击打开链接


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值