Unity3D_UGUI is super simple to use Tap and Shift keys to switch the Input input box up and down

                                    Unity3D_UGUI is super simple to use Tap and Shift keys to switch the Input input box up and down


table of Contents

1. Introduction to the blog post

2. Content

3. Push

4. Conclusion


1. Introduction to the blog post

The content is simple, such as the title, the effect is as follows


2. Content

The implementation is very simple, without too much introduction, put the code directly, the comments are very clear, just hang the script directly into the scene

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

/// <summary>
/// time:2019/8/28  0:08
/// author:Sun
/// des:Tap
///
/// github:https://github.com/KingSun5
/// csdn:https://blog.csdn.net/Mr_Sun88
/// </summary>
public class ChangeInputLine : MonoBehaviour
{
    /// <summary>
    /// 用来获取当前是否处于Input的焦点状态
    /// </summary>
    private EventSystem _system;
    /// <summary>
    /// 当前焦点所处的Input
    /// </summary>
    private Selectable _selecInput;
    /// <summary>
    /// 目标Input
    /// </summary>
    private Selectable _nextInput;

    private void Start()
    {
        _system = EventSystem.current;
    }

    void Update()
    {
        //在Update内监听Tap键的按下
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            //是否聚焦Input
            if (_system.currentSelectedGameObject != null)
            {  
                //获取当前选中的Input
                _selecInput = _system.currentSelectedGameObject.GetComponent<Selectable>();
                //监听Shift
                if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
                {
                    //Shift按下则选择出去上方的Input
                    _nextInput = _selecInput.FindSelectableOnUp();
                    //上边没有找左边的
                    if (_nextInput == null)  _nextInput = _selecInput.FindSelectableOnLeft();
                }
                else
                {
                    //没按shift就找下边的Input
                    _nextInput = _selecInput.FindSelectableOnDown();
                    //或者右边的
                    if (_nextInput == null) _nextInput = _selecInput.FindSelectableOnRight();
                }
            }
       
            //下一个Input不空的话就聚焦
            if (_nextInput != null) _nextInput.Select();
        }
    }

}

3. Push

github:https://github.com/KingSun5

4. Conclusion

       If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

       This article is an original article, please reprint the source of the famous author and stick to the top! ! ! !

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/100110850