Unity3D_UGUI超简单利用Tap键和Shift键实现Input输入框的上下切换
目录
1、博文介绍
内容简单,如题,效果如下
2、内容
实现很简单,不作太多介绍,直接放代码,注释都很清楚,直接把脚本向场景里一挂就好了
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、推送
github:https://github.com/KingSun5
4、结语
若是觉得博主的文章写的不错,不妨关注一下博主,点赞一下博文,另博主能力有限,若文中有出现什么错误的地方,欢迎各位评论指摘。
QQ交流群:806091680(Chinar)
该群为CSDN博主Chinar所创,推荐一下!我也在群里!
本文属于原创文章,转载请著名作者出处并置顶!!!!