Skip to content

Instantly share code, notes, and snippets.

@mminer
Created April 11, 2018 21:44
Show Gist options
  • Select an option

  • Save mminer/3f900908c19a5ce383997d1bf0ae6bda to your computer and use it in GitHub Desktop.

Select an option

Save mminer/3f900908c19a5ce383997d1bf0ae6bda to your computer and use it in GitHub Desktop.
Unity component to allow navigating between UI components using tab.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
/// <summary>
/// Allows moving between UI components using tab and shift-tab.
/// </summary>
public class TabNavigator : MonoBehaviour
{
EventSystem system;
bool isShiftHeld
{
get { return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift); }
}
void Start()
{
system = EventSystem.current;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
FocusNextSelectable();
}
}
void FocusNextSelectable()
{
var nextSelectable = GetNextSelectable();
if (!nextSelectable)
{
return;
}
SimulateClick(nextSelectable);
system.SetSelectedGameObject(nextSelectable.gameObject);
}
Selectable GetNextSelectable()
{
if (!system.currentSelectedGameObject)
{
return null;
}
var currentSelectable = system.currentSelectedGameObject.GetComponent<Selectable>();
if (!currentSelectable)
{
return null;
}
return isShiftHeld ?
currentSelectable.FindSelectableOnUp() :
currentSelectable.FindSelectableOnDown();
}
void SimulateClick(Selectable selectable)
{
var clickableElement = selectable.GetComponent<IPointerClickHandler>();
if (clickableElement == null)
{
return;
}
var eventData = new PointerEventData(system);
clickableElement.OnPointerClick(eventData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment