@@ -19,14 +19,14 @@ public class NavigationStack : MonoBehaviour
1919 public EventSystem eventSystem { get ; private set ; }
2020
2121 /// <summary>
22- /// The stack of game objects that have been selected .
22+ /// The game objects added to the stack .
2323 /// </summary>
24- public Stack < GameObject > navigationStack { get ; private set ; }
24+ public Stack < GameObject > items { get ; private set ; }
2525
2626 /// <summary>
2727 /// The current selected game object at the top of the stack.
2828 /// </summary>
29- public GameObject CurrentSelectedGameObject => this . navigationStack . Peek ( ) ;
29+ public GameObject Top => this . items . Count > 0 ? this . items . Peek ( ) : null ;
3030
3131 /// <summary>
3232 /// The input action to handle navigating backwards in the stack by
@@ -35,6 +35,19 @@ public class NavigationStack : MonoBehaviour
3535 [ Tooltip ( "The input action to handle navigating backwards in the stack by popping items off." ) ]
3636 public InputAction backNavigationInput = new InputAction ( "MenuBackNavigation" , InputActionType . Button ) ;
3737
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+
3851 /// <summary>
3952 /// Allows for all items to be popped off the stack. Often times you
4053 /// want to maintain at least the root game object.
@@ -59,11 +72,16 @@ private void Reset()
5972
6073 private void Awake ( )
6174 {
75+ this . items = new Stack < GameObject > ( 8 ) ;
6276 this . eventSystem = GetComponent < EventSystem > ( ) ;
63- this . navigationStack = new Stack < GameObject > ( 8 ) ;
6477 this . backNavigationInput . performed += OnBack ;
6578 }
6679
80+ private void Start ( )
81+ {
82+ Push ( this . rootGameObject ) ;
83+ }
84+
6785 private void OnEnable ( )
6886 {
6987 this . backNavigationInput . Enable ( ) ;
@@ -74,30 +92,47 @@ private void OnDisable()
7492 this . backNavigationInput . Disable ( ) ;
7593 }
7694
77- private void Update ( )
95+ public void Push ( GameObject selected )
7896 {
79- if ( this . eventSystem . currentSelectedGameObject != CurrentSelectedGameObject )
97+ if ( selected != null || this . allowNullSelections )
8098 {
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 ) ;
83101 }
102+
103+ this . eventSystem . SetSelectedGameObject ( selected ) ;
104+ this . items . Push ( selected ) ;
84105 }
85106 }
86107
87108 public void Back ( )
88109 {
89- if ( this . navigationStack . Count == 0 ) {
110+ if ( this . items . Count == 0 ) {
90111 return ;
91112 }
92113
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 ) ;
95124 }
96125
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+ }
98134
99- if ( this . navigationStack . Count > 0 ) {
100- this . eventSystem . SetSelectedGameObject ( this . navigationStack . Peek ( ) ) ;
135+ this . eventSystem . SetSelectedGameObject ( previous ) ;
101136 }
102137 }
103138
0 commit comments