66namespace Zigurous . UI
77{
88 /// <summary>
9- /// Keeps track of a stack of selected game objects as to allow for easy
10- /// back navigation.
9+ /// Manages a stack of game objects for menu navigation purposes. This is
10+ /// especially useful to handle backwards navigation by simply popping off
11+ /// the last item in the stack.
1112 /// </summary>
1213 [ RequireComponent ( typeof ( EventSystem ) ) ]
1314 [ AddComponentMenu ( "Zigurous/UI/Navigation/Navigation Stack" ) ]
@@ -29,10 +30,10 @@ public class NavigationStack : MonoBehaviour
2930 public GameObject Top => this . items . Count > 0 ? this . items . Peek ( ) : null ;
3031
3132 /// <summary>
32- /// The input action to handle navigating backwards in the stack by
33- /// popping items off .
33+ /// The input action that handles backwards navigation by popping items
34+ /// off the stack .
3435 /// </summary>
35- [ Tooltip ( "The input action to handle navigating backwards in the stack by popping items off." ) ]
36+ [ Tooltip ( "The input action that handles backwards navigation by popping items off the stack ." ) ]
3637 public InputAction backNavigationInput = new InputAction ( "MenuBackNavigation" , InputActionType . Button ) ;
3738
3839 /// <summary>
@@ -92,6 +93,11 @@ private void OnDisable()
9293 this . backNavigationInput . Disable ( ) ;
9394 }
9495
96+ /// <summary>
97+ /// Pushes a game object onto the stack, effectively navigating
98+ /// forwards.
99+ /// </summary>
100+ /// <param name="selected">The game object to push onto the stack.</param>
95101 public void Push ( GameObject selected )
96102 {
97103 if ( selected != null || this . allowNullSelections )
@@ -105,16 +111,23 @@ public void Push(GameObject selected)
105111 }
106112 }
107113
108- public void Back ( )
114+ /// <summary>
115+ /// Pops the last game object off the stack, effectively navigating
116+ /// backwards.
117+ /// </summary>
118+ /// <returns>The game object that was popped off the stack.</returns>
119+ public GameObject Pop ( )
109120 {
110121 if ( this . items . Count == 0 ) {
111- return ;
122+ return null ;
112123 }
113124
125+ GameObject top = null ;
126+
114127 // Pop off the top of the stack
115128 if ( this . items . Count > 1 || this . allowEmptyStack )
116129 {
117- GameObject top = this . items . Pop ( ) ;
130+ top = this . items . Pop ( ) ;
118131
119132 if ( this . setActiveState && top != null ) {
120133 top . SetActive ( false ) ;
@@ -134,12 +147,14 @@ public void Back()
134147
135148 this . eventSystem . SetSelectedGameObject ( previous ) ;
136149 }
150+
151+ return top ;
137152 }
138153
139154 private void OnBack ( InputAction . CallbackContext context )
140155 {
141156 if ( context . performed ) {
142- Back ( ) ;
157+ Pop ( ) ;
143158 }
144159 }
145160
0 commit comments