2015-02-03 03:25:41 +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
|
2015-08-30 07:31:59 +08:00
|
|
|
/// Updated 08-29-15 - On request of Issue #13 on repo, added a manual navigation order.
|
2015-02-03 03:25:41 +08:00
|
|
|
|
2014-12-31 05:43:06 +08:00
|
|
|
using UnityEngine.EventSystems;
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
2014-12-31 05:43:06 +08:00
|
|
|
{
|
2015-08-30 07:31:59 +08:00
|
|
|
public enum NavigationMode { Auto = 0, Manual = 1};
|
2015-02-03 07:07:31 +08:00
|
|
|
[RequireComponent(typeof(EventSystem))]
|
|
|
|
[AddComponentMenu("Event/Extensions/Tab Navigation Helper")]
|
|
|
|
public class TabNavigationHelper : MonoBehaviour
|
2014-12-31 05:43:06 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
private EventSystem _system;
|
2015-08-30 07:31:59 +08:00
|
|
|
[Tooltip("The path to take when user is tabbing through ui components.")]
|
|
|
|
public Selectable[] NavigationPath;
|
|
|
|
[Tooltip("Use the default Unity navigation system or a manual fixed order using Navigation Path")]
|
|
|
|
public NavigationMode NavigationMode;
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
void Start()
|
2014-12-31 05:43:06 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
_system = GetComponent<EventSystem>();
|
|
|
|
if (_system == null)
|
2014-12-31 05:43:06 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
Debug.LogError("Needs to be attached to the Event System component in the scene");
|
2014-12-31 05:43:06 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
public void Update()
|
2014-12-31 05:43:06 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
Selectable next = null;
|
|
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.Tab) && Input.GetKey(KeyCode.LeftShift))
|
|
|
|
{
|
|
|
|
if (_system.currentSelectedGameObject != null)
|
|
|
|
{
|
|
|
|
next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next = _system.firstSelectedGameObject.GetComponent<Selectable>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Input.GetKeyDown(KeyCode.Tab))
|
2014-12-31 05:43:06 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
if (_system.currentSelectedGameObject != null)
|
|
|
|
{
|
|
|
|
next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next = _system.firstSelectedGameObject.GetComponent<Selectable>();
|
|
|
|
}
|
2014-12-31 05:43:06 +08:00
|
|
|
}
|
2015-08-30 07:31:59 +08:00
|
|
|
else if(NavigationMode == NavigationMode.Manual)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 07:07:31 +08:00
|
|
|
else if (_system.currentSelectedGameObject == null)
|
2014-12-31 05:43:06 +08:00
|
|
|
{
|
|
|
|
next = _system.firstSelectedGameObject.GetComponent<Selectable>();
|
|
|
|
}
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
selectGameObject(next);
|
2014-12-31 05:43:06 +08:00
|
|
|
}
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
private void selectGameObject(Selectable selectable)
|
2014-12-31 05:43:06 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
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));
|
|
|
|
}
|
2014-12-31 05:43:06 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|