@@ -37,6 +37,12 @@ protected override void OnEnable()
3737 {
3838 ParentTransform = transform ;
3939 }
40+
41+ for ( int i = 0 ; i < ParentTransform . childCount ; i ++ )
42+ {
43+ ParentTransform . GetChild ( i ) . gameObject . SetActive ( false ) ;
44+ }
45+
4046 base . OnEnable ( ) ;
4147 }
4248
@@ -48,23 +54,42 @@ protected override void OnEnable()
4854 public override void OnCursorStateChange ( CursorStateEnum state )
4955 {
5056 base . OnCursorStateChange ( state ) ;
51-
5257 if ( state != CursorStateEnum . Contextual )
5358 {
54- // Hide all children first
55- for ( int i = 0 ; i < ParentTransform . childCount ; i ++ )
59+
60+ // First, try to find a cursor for the current state
61+ var newActive = new ObjectCursorDatum ( ) ;
62+ for ( int cursorIndex = 0 ; cursorIndex < CursorStateData . Length ; cursorIndex ++ )
5663 {
57- ParentTransform . GetChild ( i ) . gameObject . SetActive ( false ) ;
64+ ObjectCursorDatum cursor = CursorStateData [ cursorIndex ] ;
65+ if ( cursor . CursorState == state )
66+ {
67+ newActive = cursor ;
68+ break ;
69+ }
70+ }
71+
72+ // If no cursor for current state is found, let the last active cursor be
73+ // (any cursor is better than an invisible cursor)
74+ if ( newActive . Name == null )
75+ {
76+ return ;
5877 }
5978
60- // Set active any that match the current state
61- for ( int i = 0 ; i < CursorStateData . Length ; i ++ )
79+ // If we come here, there is a cursor for the new state,
80+ // so de-activate a possible earlier active cursor
81+ for ( int cursorIndex = 0 ; cursorIndex < CursorStateData . Length ; cursorIndex ++ )
6282 {
63- if ( CursorStateData [ i ] . CursorState == state )
83+ ObjectCursorDatum cursor = CursorStateData [ cursorIndex ] ;
84+ if ( cursor . CursorObject . activeSelf )
6485 {
65- CursorStateData [ i ] . CursorObject . SetActive ( true ) ;
86+ cursor . CursorObject . SetActive ( false ) ;
87+ break ;
6688 }
6789 }
90+
91+ // ... and set the cursor for the new state active.
92+ newActive . CursorObject . SetActive ( true ) ;
6893 }
6994 }
7095 }
0 commit comments