Updated initialisation logic to not cause an endless loop in the TabNavigationHelper

Resolves: https://github.com/Unity-UI-Extensions/com.unity.uiextensions/issues/208
pull/413/head
Simon Jackson 2023-01-01 12:16:33 +00:00
parent 55489bd617
commit d1b94bf6f1
1 changed files with 28 additions and 12 deletions

View File

@ -6,6 +6,7 @@
/// - autoselect "firstSelectedGameObject" since it doesn't seem to work automatically /// - 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. /// Updated 08-29-15 - On request of Issue #13 on repo, added a manual navigation order.
using System.Collections.Generic;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
@ -16,15 +17,19 @@ namespace UnityEngine.UI.Extensions
public class TabNavigationHelper : MonoBehaviour public class TabNavigationHelper : MonoBehaviour
{ {
private EventSystem _system; private EventSystem _system;
private Selectable StartingObject; private Selectable startingObject;
private Selectable LastObject; private Selectable lastObject;
[Tooltip("The path to take when user is tabbing through ui components.")] [Tooltip("The path to take when user is tabbing through ui components.")]
public Selectable[] NavigationPath; public Selectable[] NavigationPath;
[Tooltip("Use the default Unity navigation system or a manual fixed order using Navigation Path")] [Tooltip("Use the default Unity navigation system or a manual fixed order using Navigation Path")]
public NavigationMode NavigationMode; public NavigationMode NavigationMode;
[Tooltip("If True, this will loop the tab order from last to first automatically")] [Tooltip("If True, this will loop the tab order from last to first automatically")]
public bool CircularNavigation; public bool CircularNavigation;
void Start() void Start()
{ {
_system = GetComponent<EventSystem>(); _system = GetComponent<EventSystem>();
@ -34,24 +39,35 @@ namespace UnityEngine.UI.Extensions
} }
if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0) if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0)
{ {
StartingObject = NavigationPath[0].gameObject.GetComponent<Selectable>(); startingObject = NavigationPath[0].gameObject.GetComponent<Selectable>();
} }
if (StartingObject == null && CircularNavigation) if (startingObject == null && CircularNavigation)
{ {
SelectDefaultObject(out StartingObject); SelectDefaultObject(out startingObject);
} }
} }
public void Update() public void Update()
{ {
Selectable next = null; Selectable next = null;
if (LastObject == null && _system.currentSelectedGameObject != null) if (lastObject == null && _system.currentSelectedGameObject != null)
{ {
var startingPoint = _system.currentSelectedGameObject.GetComponent<Selectable>();
var selectableItems = new Stack<Selectable>();
selectableItems.Push(startingPoint);
//Find the last selectable object //Find the last selectable object
next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown(); next = startingPoint.FindSelectableOnDown();
while (next != null) while (next != null)
{ {
LastObject = next; if (selectableItems.Contains(next))
{
lastObject = selectableItems.Pop();
selectableItems.Clear();
break;
}
lastObject = next;
selectableItems.Push(next);
next = next.FindSelectableOnDown(); next = next.FindSelectableOnDown();
} }
} }
@ -76,7 +92,7 @@ namespace UnityEngine.UI.Extensions
next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp(); next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
if (next == null && CircularNavigation) if (next == null && CircularNavigation)
{ {
next = LastObject; next = lastObject;
} }
} }
else else
@ -105,7 +121,7 @@ namespace UnityEngine.UI.Extensions
next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown(); next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
if (next == null && CircularNavigation) if (next == null && CircularNavigation)
{ {
next = StartingObject; next = startingObject;
} }
} }
else else
@ -119,9 +135,9 @@ namespace UnityEngine.UI.Extensions
SelectDefaultObject(out next); SelectDefaultObject(out next);
} }
if (CircularNavigation && StartingObject == null) if (CircularNavigation && startingObject == null)
{ {
StartingObject = next; startingObject = next;
} }
selectGameObject(next); selectGameObject(next);
} }