14 [RequireComponent(typeof(EventSystem))]
15 [AddComponentMenu(
"Event/Extensions/Tab Navigation Helper")]
18 private EventSystem _system;
19 private Selectable StartingObject;
20 private Selectable LastObject;
21 [Tooltip(
"The path to take when user is tabbing through ui components.")]
23 [Tooltip(
"Use the default Unity navigation system or a manual fixed order using Navigation Path")]
25 [Tooltip(
"If True, this will loop the tab order from last to first automatically")]
30 _system = GetComponent<EventSystem>();
33 Debug.LogError(
"Needs to be attached to the Event System component in the scene");
37 StartingObject =
NavigationPath[0].gameObject.GetComponent<Selectable>();
41 SelectDefaultObject(out StartingObject);
47 Selectable next =
null;
48 if (LastObject ==
null && _system.currentSelectedGameObject !=
null)
51 next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
55 next = next.FindSelectableOnDown();
59 if (Input.GetKeyDown(KeyCode.Tab) && Input.GetKey(KeyCode.LeftShift))
65 if (_system.currentSelectedGameObject !=
NavigationPath[i].gameObject)
continue;
74 if (_system.currentSelectedGameObject !=
null)
76 next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
84 SelectDefaultObject(out next);
88 else if (Input.GetKeyDown(KeyCode.Tab))
94 if (_system.currentSelectedGameObject !=
NavigationPath[i].gameObject)
continue;
103 if (_system.currentSelectedGameObject !=
null)
105 next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
108 next = StartingObject;
113 SelectDefaultObject(out next);
117 else if (_system.currentSelectedGameObject ==
null)
119 SelectDefaultObject(out next);
124 StartingObject = next;
126 selectGameObject(next);
129 private void SelectDefaultObject(out Selectable next)
131 if (_system.firstSelectedGameObject)
133 next = _system.firstSelectedGameObject.GetComponent<Selectable>();
141 private void selectGameObject(Selectable selectable)
143 if (selectable !=
null)
145 InputField inputfield = selectable.GetComponent<InputField>();
146 if (inputfield !=
null) inputfield.OnPointerClick(
new PointerEventData(_system));
148 _system.SetSelectedGameObject(selectable.gameObject,
new BaseEventData(_system));
NavigationMode NavigationMode
Selectable[] NavigationPath
Credit Erdener Gonenc - @PixelEnvision.