@@ -19,14 +19,14 @@ public class NavigationStack : MonoBehaviour
19
19
public EventSystem eventSystem { get ; private set ; }
20
20
21
21
/// <summary>
22
- /// The stack of game objects that have been selected .
22
+ /// The game objects added to the stack .
23
23
/// </summary>
24
- public Stack < GameObject > navigationStack { get ; private set ; }
24
+ public Stack < GameObject > items { get ; private set ; }
25
25
26
26
/// <summary>
27
27
/// The current selected game object at the top of the stack.
28
28
/// </summary>
29
- public GameObject CurrentSelectedGameObject => this . navigationStack . Peek ( ) ;
29
+ public GameObject Top => this . items . Count > 0 ? this . items . Peek ( ) : null ;
30
30
31
31
/// <summary>
32
32
/// The input action to handle navigating backwards in the stack by
@@ -35,6 +35,19 @@ public class NavigationStack : MonoBehaviour
35
35
[ Tooltip ( "The input action to handle navigating backwards in the stack by popping items off." ) ]
36
36
public InputAction backNavigationInput = new InputAction ( "MenuBackNavigation" , InputActionType . Button ) ;
37
37
38
+ /// <summary>
39
+ /// The root game object added to the bottom of the stack.
40
+ /// </summary>
41
+ [ Tooltip ( "The root game object added to the bottom of the stack." ) ]
42
+ public GameObject rootGameObject ;
43
+
44
+ /// <summary>
45
+ /// Automatically sets the active state of game objects as they are
46
+ /// pushed on and off the stack.
47
+ /// </summary>
48
+ [ Tooltip ( "Automatically sets the active state of game objects as they are pushed on and off the stack." ) ]
49
+ public bool setActiveState = true ;
50
+
38
51
/// <summary>
39
52
/// Allows for all items to be popped off the stack. Often times you
40
53
/// want to maintain at least the root game object.
@@ -59,11 +72,16 @@ private void Reset()
59
72
60
73
private void Awake ( )
61
74
{
75
+ this . items = new Stack < GameObject > ( 8 ) ;
62
76
this . eventSystem = GetComponent < EventSystem > ( ) ;
63
- this . navigationStack = new Stack < GameObject > ( 8 ) ;
64
77
this . backNavigationInput . performed += OnBack ;
65
78
}
66
79
80
+ private void Start ( )
81
+ {
82
+ Push ( this . rootGameObject ) ;
83
+ }
84
+
67
85
private void OnEnable ( )
68
86
{
69
87
this . backNavigationInput . Enable ( ) ;
@@ -74,30 +92,47 @@ private void OnDisable()
74
92
this . backNavigationInput . Disable ( ) ;
75
93
}
76
94
77
- private void Update ( )
95
+ public void Push ( GameObject selected )
78
96
{
79
- if ( this . eventSystem . currentSelectedGameObject != CurrentSelectedGameObject )
97
+ if ( selected != null || this . allowNullSelections )
80
98
{
81
- if ( this . eventSystem . currentSelectedGameObject != null || this . allowNullSelections ) {
82
- this . navigationStack . Push ( this . eventSystem . currentSelectedGameObject ) ;
99
+ if ( this . setActiveState && selected != null ) {
100
+ selected . SetActive ( true ) ;
83
101
}
102
+
103
+ this . eventSystem . SetSelectedGameObject ( selected ) ;
104
+ this . items . Push ( selected ) ;
84
105
}
85
106
}
86
107
87
108
public void Back ( )
88
109
{
89
- if ( this . navigationStack . Count == 0 ) {
110
+ if ( this . items . Count == 0 ) {
90
111
return ;
91
112
}
92
113
93
- if ( this . navigationStack . Count > 1 || this . allowEmptyStack ) {
94
- this . navigationStack . Pop ( ) ;
114
+ // Pop off the top of the stack
115
+ if ( this . items . Count > 1 || this . allowEmptyStack )
116
+ {
117
+ GameObject top = this . items . Pop ( ) ;
118
+
119
+ if ( this . setActiveState && top != null ) {
120
+ top . SetActive ( false ) ;
121
+ }
122
+
123
+ this . eventSystem . SetSelectedGameObject ( null ) ;
95
124
}
96
125
97
- this . eventSystem . SetSelectedGameObject ( null ) ;
126
+ // Set the previous item to be active
127
+ if ( this . items . Count > 0 )
128
+ {
129
+ GameObject previous = this . items . Peek ( ) ;
130
+
131
+ if ( this . setActiveState && previous != null ) {
132
+ previous . SetActive ( true ) ;
133
+ }
98
134
99
- if ( this . navigationStack . Count > 0 ) {
100
- this . eventSystem . SetSelectedGameObject ( this . navigationStack . Peek ( ) ) ;
135
+ this . eventSystem . SetSelectedGameObject ( previous ) ;
101
136
}
102
137
}
103
138
0 commit comments