2017-07-17 02:29:47 +08:00
|
|
|
/// Credit Melang
|
|
|
|
/// Sourced from - http://forum.unity3d.com/members/melang.593409/
|
|
|
|
/// Updated omatase 10-18-14 - support for Shift + Tab as well
|
|
|
|
/// - bug fix to prevent crash if no control selected
|
|
|
|
/// - updated to support new semantics for EventSystem in later 4.6 builds
|
|
|
|
/// - autoselect "firstSelectedGameObject" since it doesn't seem to work automatically
|
|
|
|
/// Updated 08-29-15 - On request of Issue #13 on repo, added a manual navigation order.
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
using System.Collections.Generic;
|
2017-07-17 02:29:47 +08:00
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
{
|
|
|
|
public enum NavigationMode { Auto = 0, Manual = 1};
|
|
|
|
[RequireComponent(typeof(EventSystem))]
|
|
|
|
[AddComponentMenu("Event/Extensions/Tab Navigation Helper")]
|
|
|
|
public class TabNavigationHelper : MonoBehaviour
|
|
|
|
{
|
|
|
|
private EventSystem _system;
|
2023-02-07 22:35:43 +08:00
|
|
|
private Selectable startingObject;
|
|
|
|
private Selectable lastObject;
|
|
|
|
|
2017-07-17 02:29:47 +08:00
|
|
|
[Tooltip("The path to take when user is tabbing through ui components.")]
|
|
|
|
public Selectable[] NavigationPath;
|
2023-02-07 22:35:43 +08:00
|
|
|
|
2017-07-17 02:29:47 +08:00
|
|
|
[Tooltip("Use the default Unity navigation system or a manual fixed order using Navigation Path")]
|
|
|
|
public NavigationMode NavigationMode;
|
2023-02-07 22:35:43 +08:00
|
|
|
|
2017-07-17 02:29:47 +08:00
|
|
|
[Tooltip("If True, this will loop the tab order from last to first automatically")]
|
|
|
|
public bool CircularNavigation;
|
2023-02-07 22:35:43 +08:00
|
|
|
|
2017-07-17 02:29:47 +08:00
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
_system = GetComponent<EventSystem>();
|
|
|
|
if (_system == null)
|
|
|
|
{
|
|
|
|
Debug.LogError("Needs to be attached to the Event System component in the scene");
|
|
|
|
}
|
|
|
|
if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0)
|
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
startingObject = NavigationPath[0].gameObject.GetComponent<Selectable>();
|
2017-07-17 02:29:47 +08:00
|
|
|
}
|
2023-02-07 22:35:43 +08:00
|
|
|
if (startingObject == null && CircularNavigation)
|
2017-07-17 02:29:47 +08:00
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
SelectDefaultObject(out startingObject);
|
2017-07-17 02:29:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
{
|
|
|
|
Selectable next = null;
|
2023-02-07 22:35:43 +08:00
|
|
|
if (lastObject == null && _system.currentSelectedGameObject != null)
|
2017-07-17 02:29:47 +08:00
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
var startingPoint = _system.currentSelectedGameObject.GetComponent<Selectable>();
|
|
|
|
var selectableItems = new Stack<Selectable>();
|
|
|
|
selectableItems.Push(startingPoint);
|
|
|
|
|
2017-07-17 02:29:47 +08:00
|
|
|
//Find the last selectable object
|
2023-02-07 22:35:43 +08:00
|
|
|
next = startingPoint.FindSelectableOnDown();
|
2017-07-17 02:29:47 +08:00
|
|
|
while (next != null)
|
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
if (selectableItems.Contains(next))
|
|
|
|
{
|
|
|
|
lastObject = selectableItems.Pop();
|
|
|
|
selectableItems.Clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastObject = next;
|
|
|
|
selectableItems.Push(next);
|
2017-07-17 02:29:47 +08:00
|
|
|
next = next.FindSelectableOnDown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 00:07:39 +08:00
|
|
|
if (UIExtensionsInputManager.GetKeyDown(KeyCode.Tab) && UIExtensionsInputManager.GetKey(KeyCode.LeftShift))
|
2017-07-17 02:29:47 +08:00
|
|
|
{
|
|
|
|
if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0)
|
|
|
|
{
|
|
|
|
for (var i = NavigationPath.Length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (_system.currentSelectedGameObject != NavigationPath[i].gameObject) continue;
|
|
|
|
|
|
|
|
next = i == 0 ? NavigationPath[NavigationPath.Length - 1] : NavigationPath[i - 1];
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_system.currentSelectedGameObject != null)
|
|
|
|
{
|
|
|
|
next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
|
|
|
|
if (next == null && CircularNavigation)
|
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
next = lastObject;
|
2017-07-17 02:29:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SelectDefaultObject(out next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 00:07:39 +08:00
|
|
|
else if (UIExtensionsInputManager.GetKeyDown(KeyCode.Tab))
|
2017-07-17 02:29:47 +08:00
|
|
|
{
|
|
|
|
if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < NavigationPath.Length; i++)
|
|
|
|
{
|
|
|
|
if (_system.currentSelectedGameObject != NavigationPath[i].gameObject) continue;
|
|
|
|
|
|
|
|
next = i == (NavigationPath.Length - 1) ? NavigationPath[0] : NavigationPath[i + 1];
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_system.currentSelectedGameObject != null)
|
|
|
|
{
|
|
|
|
next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
|
|
|
|
if (next == null && CircularNavigation)
|
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
next = startingObject;
|
2017-07-17 02:29:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SelectDefaultObject(out next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_system.currentSelectedGameObject == null)
|
|
|
|
{
|
|
|
|
SelectDefaultObject(out next);
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
if (CircularNavigation && startingObject == null)
|
2017-07-17 02:29:47 +08:00
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
startingObject = next;
|
2017-07-17 02:29:47 +08:00
|
|
|
}
|
|
|
|
selectGameObject(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SelectDefaultObject(out Selectable next)
|
|
|
|
{
|
|
|
|
if (_system.firstSelectedGameObject)
|
|
|
|
{
|
|
|
|
next = _system.firstSelectedGameObject.GetComponent<Selectable>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void selectGameObject(Selectable selectable)
|
|
|
|
{
|
|
|
|
if (selectable != null)
|
|
|
|
{
|
|
|
|
InputField inputfield = selectable.GetComponent<InputField>();
|
|
|
|
if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(_system)); //if it's an input field, also set the text caret
|
|
|
|
|
|
|
|
_system.SetSelectedGameObject(selectable.gameObject, new BaseEventData(_system));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|