1 using System.Collections.Generic;
10 this._orderedSelectables =
new List<Selectable>();
20 if (Input.GetKeyDown(KeyCode.Tab))
24 this.HandleHotkeySelect(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift),
true);
33 List<Selectable> originalSelectables = Selectable.allSelectables;
34 int totalSelectables = originalSelectables.Count;
35 this._orderedSelectables =
new List<Selectable>(totalSelectables);
36 for (
int index = 0; index < totalSelectables; ++index)
38 Selectable selectable = originalSelectables[index];
39 this._orderedSelectables.Insert(
40 this.FindSortedIndexForSelectable(index, selectable), selectable);
44 private void HandleHotkeySelect(
bool isNavigateBackward,
bool isWrapAround)
46 GameObject selectedObject = EventSystem.current.currentSelectedGameObject;
47 if (selectedObject !=
null && selectedObject.activeInHierarchy)
49 Selectable currentSelection = selectedObject.GetComponent<Selectable>();
50 if (currentSelection !=
null)
52 Selectable nextSelection = this.FindNextSelectable(
53 this._orderedSelectables.IndexOf(currentSelection), isNavigateBackward, isWrapAround);
54 if (nextSelection !=
null)
56 nextSelection.Select();
61 this.SelectFirstSelectable();
66 this.SelectFirstSelectable();
70 private void SelectFirstSelectable()
72 if (this._orderedSelectables !=
null && this._orderedSelectables.Count > 0)
74 Selectable firstSelectable = this._orderedSelectables[0];
75 firstSelectable.Select();
82 private Selectable FindNextSelectable(
int currentSelectableIndex,
bool isNavigateBackward,
bool isWrapAround)
84 Selectable nextSelection =
null;
86 int totalSelectables = this._orderedSelectables.Count;
87 if (totalSelectables > 1)
89 if (isNavigateBackward)
91 if (currentSelectableIndex == 0)
93 nextSelection = (isWrapAround) ? this._orderedSelectables[totalSelectables - 1] :
null;
97 nextSelection = this._orderedSelectables[currentSelectableIndex - 1];
102 if (currentSelectableIndex == (totalSelectables - 1))
104 nextSelection = (isWrapAround) ? this._orderedSelectables[0] :
null;
108 nextSelection = this._orderedSelectables[currentSelectableIndex + 1];
113 return nextSelection;
119 private int FindSortedIndexForSelectable(
int selectableIndex, Selectable selectableToSort)
121 int sortedIndex = selectableIndex;
122 if (selectableIndex > 0)
124 int previousIndex = selectableIndex - 1;
125 Vector3 previousSelectablePosition = this._orderedSelectables[previousIndex].transform.position;
126 Vector3 selectablePositionToSort = selectableToSort.transform.position;
128 if ((previousSelectablePosition.y < selectablePositionToSort.y)
129 || (previousSelectablePosition.y == selectablePositionToSort.y
130 && previousSelectablePosition.x > selectablePositionToSort.x))
133 sortedIndex = this.FindSortedIndexForSelectable(previousIndex, selectableToSort);
140 private List<Selectable> _orderedSelectables =
null;
void SortSelectables()
Iterates through all selectables in scene and orders them based on their position.