unity Tab键实现切换输入框功能

时间:2021-08-27 02:34:09
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
/// <summary>
/// ************************************本脚本挂在输入框上即可*********************************************
/// </summary>
public class Tabcoll : MonoBehaviour,ISelectHandler,IDeselectHandler {
public void OnDeselect(BaseEventData eventData)
{
_instacnet = false;
} public void OnSelect(BaseEventData eventData)
{
_instacnet = true;
}
EventSystem system;
private bool _instacnet = false;
// Use this for initialization
void Start () {
system = EventSystem.current;
} // Update is called once per frame
void Update () {
if (Input .GetKeyDown (KeyCode.Tab )&&_instacnet )
{
Selectable _Nect = null;
if (Input .GetKey (KeyCode.LeftShift )||Input.GetKey (KeyCode.RightShift ))
{
_Nect = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
if (_Nect == null) _Nect = system.lastSelectedGameObject.GetComponent<Selectable>(); }
else
{
_Nect = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
if (_Nect == null) _Nect = system.firstSelectedGameObject.GetComponent<Selectable>();
}
if (_Nect!=null)
{
InputField inputField = _Nect.GetComponent<InputField>();
system.SetSelectedGameObject(_Nect.gameObject, new BaseEventData(system));
}
else
{
Debug.LogError("没有下一个组件");
}
}
} }