UGUI 中Tab切换 InputField
2016-07-29 16:24
996 查看
UGUI在InputField里即使设置了Navigation也是不可以用键盘切换上下,所以自己写了个组件实现这个功能
前提条件是要在InputField里设置Navigation,也就是必须能找到上一个控件和下一个控件
然后将以下脚本挂在需要切换的InputField上,用Tab和Shift+Tab切换:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InputNavigator : MonoBehaviour, ISelectHandler, IDeselectHandler
{
EventSystem system;
private bool _isSelect = false;
void Start()
{
system = EventSystem.current;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab) && _isSelect)
{
Selectable next = null;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
if (next == null) next = system.lastSelectedGameObject.GetComponent<Selectable>();
}
else
{
next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
if (next == null) next = system.firstSelectedGameObject.GetComponent<Selectable>();
}
if (next != null)
{
InputField inputfield = next.GetComponent<InputField>();
system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
}
else
{
Debug.LogError("找不到下一个控件");
}
}
}
public void OnSelect(BaseEventData eventData)
{
_isSelect = true;
}
public void OnDeselect(BaseEventData eventData)
{
_isSelect = false;
}
}
前提条件是要在InputField里设置Navigation,也就是必须能找到上一个控件和下一个控件
然后将以下脚本挂在需要切换的InputField上,用Tab和Shift+Tab切换:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InputNavigator : MonoBehaviour, ISelectHandler, IDeselectHandler
{
EventSystem system;
private bool _isSelect = false;
void Start()
{
system = EventSystem.current;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab) && _isSelect)
{
Selectable next = null;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
if (next == null) next = system.lastSelectedGameObject.GetComponent<Selectable>();
}
else
{
next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
if (next == null) next = system.firstSelectedGameObject.GetComponent<Selectable>();
}
if (next != null)
{
InputField inputfield = next.GetComponent<InputField>();
system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
}
else
{
Debug.LogError("找不到下一个控件");
}
}
}
public void OnSelect(BaseEventData eventData)
{
_isSelect = true;
}
public void OnDeselect(BaseEventData eventData)
{
_isSelect = false;
}
}
相关文章推荐
- 通过UIImageView的tag为点击事件UITapGestureRecognizer传参
- UITextField 点击事件 --- 不触发键盘弹出,而触发其他事件的实现
- POJ 2533 Longest Ordered Subsequence(最长递增子序列)
- 修改IOS UITableViewCell箭头颜色方法
- tableView分组头部标题处理
- UITableView实现选择框和编辑状态时cell向右移动的自定义方法
- Rescue<hdoj1242>
- MongoChef malformed UTF-8 character sequence at offset 89
- marquee标签实现跑马灯效果--无缝滚动
- poj3080Blue Jeans
- JSP基础之 C标签中的 varStatues属性
- svn 413 Request Entity Too Large 错误的解决方法
- Vue.js 动态绑定class
- configure: error: zlib library and headers are required
- 裸奔单片机GUI
- this-is-how-we-troubleshoot-windows-interoperability-issues-in-the-open-specifications-support-team/
- Your build settings specify a provisioning profile with the UUID, no provisioning profile was found
- UITextField UITextView对内容字数的限制
- cts android.bluetooth.cts.BluetoothLeScanTest --testBasicBleScan fail
- break&continue