Tanoda
TabNavigationHelper.cs
Go to the documentation of this file.
1
8
10
12{
13 public enum NavigationMode { Auto = 0, Manual = 1};
14 [RequireComponent(typeof(EventSystem))]
15 [AddComponentMenu("Event/Extensions/Tab Navigation Helper")]
16 public class TabNavigationHelper : MonoBehaviour
17 {
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.")]
22 public Selectable[] NavigationPath;
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")]
26 public bool CircularNavigation;
27
28 void Start()
29 {
30 _system = GetComponent<EventSystem>();
31 if (_system == null)
32 {
33 Debug.LogError("Needs to be attached to the Event System component in the scene");
34 }
35 if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0)
36 {
37 StartingObject = NavigationPath[0].gameObject.GetComponent<Selectable>();
38 }
39 if (StartingObject == null && CircularNavigation)
40 {
41 SelectDefaultObject(out StartingObject);
42 }
43 }
44
45 public void Update()
46 {
47 Selectable next = null;
48 if (LastObject == null && _system.currentSelectedGameObject != null)
49 {
50 //Find the last selectable object
51 next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
52 while (next != null)
53 {
54 LastObject = next;
55 next = next.FindSelectableOnDown();
56 }
57 }
58
59 if (Input.GetKeyDown(KeyCode.Tab) && Input.GetKey(KeyCode.LeftShift))
60 {
61 if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0)
62 {
63 for (var i = NavigationPath.Length - 1; i >= 0; i--)
64 {
65 if (_system.currentSelectedGameObject != NavigationPath[i].gameObject) continue;
66
67 next = i == 0 ? NavigationPath[NavigationPath.Length - 1] : NavigationPath[i - 1];
68
69 break;
70 }
71 }
72 else
73 {
74 if (_system.currentSelectedGameObject != null)
75 {
76 next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
77 if (next == null && CircularNavigation)
78 {
79 next = LastObject;
80 }
81 }
82 else
83 {
84 SelectDefaultObject(out next);
85 }
86 }
87 }
88 else if (Input.GetKeyDown(KeyCode.Tab))
89 {
90 if (NavigationMode == NavigationMode.Manual && NavigationPath.Length > 0)
91 {
92 for (var i = 0; i < NavigationPath.Length; i++)
93 {
94 if (_system.currentSelectedGameObject != NavigationPath[i].gameObject) continue;
95
96 next = i == (NavigationPath.Length - 1) ? NavigationPath[0] : NavigationPath[i + 1];
97
98 break;
99 }
100 }
101 else
102 {
103 if (_system.currentSelectedGameObject != null)
104 {
105 next = _system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
106 if (next == null && CircularNavigation)
107 {
108 next = StartingObject;
109 }
110 }
111 else
112 {
113 SelectDefaultObject(out next);
114 }
115 }
116 }
117 else if (_system.currentSelectedGameObject == null)
118 {
119 SelectDefaultObject(out next);
120 }
121
122 if (CircularNavigation && StartingObject == null)
123 {
124 StartingObject = next;
125 }
126 selectGameObject(next);
127 }
128
129 private void SelectDefaultObject(out Selectable next)
130 {
131 if (_system.firstSelectedGameObject)
132 {
133 next = _system.firstSelectedGameObject.GetComponent<Selectable>();
134 }
135 else
136 {
137 next = null;
138 }
139 }
140
141 private void selectGameObject(Selectable selectable)
142 {
143 if (selectable != null)
144 {
145 InputField inputfield = selectable.GetComponent<InputField>();
146 if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(_system)); //if it's an input field, also set the text caret
147
148 _system.SetSelectedGameObject(selectable.gameObject, new BaseEventData(_system));
149 }
150 }
151 }
152}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Credit Erdener Gonenc - @PixelEnvision.