11using System ;
22using System . Collections ;
33using System . Collections . Generic ;
4+ using Unity . Collections ;
45using UnityEngine ;
6+ using UnityEngine . InputSystem ;
57
68public class Console : IGameSystem
79{
@@ -27,6 +29,7 @@ public class Console : IGameSystem
2729 string [ ] m_History = new string [ k_HistorySize ] ;
2830 int m_HistoryDisplayIndex = 0 ;
2931 int m_HistoryNextIndex = 0 ;
32+ List < char > m_InputChars = new List < char > ( ) ;
3033
3134 Color m_BackgroundColor = new Color ( 0 , 0 , 0 , 0.9f ) ;
3235 Vector4 m_TextColor = new Vector4 ( 0.7f , 1.0f , 0.7f , 1.0f ) ;
@@ -39,6 +42,40 @@ public Console()
3942 m_ConsoleBuffer = new System . UInt32 [ k_BufferSize ] ;
4043 m_InputFieldBuffer = new char [ k_InputBufferSize ] ;
4144 AddCommand ( "help" , CmdHelp , "Show available commands" ) ;
45+ AddCommand ( "dump" , CmdDumpScene , "Dump scene hierarchy in active scene" ) ;
46+ Keyboard . current . onTextInput += OnTextInput ;
47+ }
48+
49+ private void CmdDumpScene ( string [ ] args )
50+ {
51+ var go = new List < GameObject > ( ) ;
52+ UnityEngine . SceneManagement . SceneManager . GetActiveScene ( ) . GetRootGameObjects ( go ) ;
53+ foreach ( var g in go )
54+ {
55+ RecurDump ( g , 0 ) ;
56+ }
57+ }
58+
59+ private void RecurDump ( GameObject go , int depth )
60+ {
61+ Write ( "{0}{1}\n " , new string ( ' ' , depth ) , go . name ) ;
62+ for ( var i = 0 ; i < go . transform . childCount ; i ++ )
63+ {
64+ RecurDump ( go . transform . GetChild ( i ) . gameObject , depth + 1 ) ;
65+ }
66+ }
67+
68+ private void OnTextInput ( char c )
69+ {
70+ if ( ! m_ConsoleOpen )
71+ return ;
72+
73+ if ( Char . IsControl ( c ) && c != '\n ' && c != '\r ' && c != '\b ' )
74+ {
75+ return ;
76+ }
77+
78+ m_InputChars . Add ( c ) ;
4279 }
4380
4481 void CmdHelp ( string [ ] args )
@@ -107,7 +144,7 @@ public void Show(float shown)
107144
108145 public void TickUpdate ( )
109146 {
110- if ( Input . GetKeyDown ( KeyCode . F12 ) )
147+ if ( Keyboard . current . f12Key . wasPressedThisFrame )
111148 {
112149 m_ConsoleOpen = ! m_ConsoleOpen ;
113150 m_ConsoleFoldoutDest = m_ConsoleOpen ? 1.0f : 0.0f ;
@@ -117,44 +154,41 @@ public void TickUpdate()
117154 if ( ! m_ConsoleOpen )
118155 return ;
119156
120- Scroll ( ( int ) Input . mouseScrollDelta . y ) ;
121- if ( Input . anyKey )
157+ Scroll ( ( int ) Mouse . current . scroll . ReadValue ( ) . y ) ;
158+
159+ if ( Keyboard . current . leftArrowKey . wasPressedThisFrame && m_CursorPos > 0 )
160+ m_CursorPos -- ;
161+ else if ( Keyboard . current . rightArrowKey . wasPressedThisFrame && m_CursorPos < m_InputFieldLength )
162+ m_CursorPos ++ ;
163+ else if ( Keyboard . current . homeKey . wasPressedThisFrame || ( Keyboard . current . aKey . wasPressedThisFrame && ( Keyboard . current . leftCtrlKey . isPressed || Keyboard . current . rightCtrlKey . isPressed ) ) )
164+ m_CursorPos = 0 ;
165+ else if ( Keyboard . current . endKey . wasPressedThisFrame || ( Keyboard . current . eKey . wasPressedThisFrame && ( Keyboard . current . leftCtrlKey . isPressed || Keyboard . current . rightCtrlKey . isPressed ) ) )
166+ m_CursorPos = m_InputFieldLength ;
167+ else if ( Keyboard . current . tabKey . wasPressedThisFrame )
168+ TabComplete ( ) ;
169+ else if ( Keyboard . current . upArrowKey . wasPressedThisFrame )
170+ HistoryPrev ( ) ;
171+ else if ( Keyboard . current . downArrowKey . wasPressedThisFrame )
172+ HistoryNext ( ) ;
173+ else
122174 {
123- if ( Input . GetKeyDown ( KeyCode . LeftArrow ) && m_CursorPos > 0 )
124- m_CursorPos -- ;
125- else if ( Input . GetKeyDown ( KeyCode . RightArrow ) && m_CursorPos < m_InputFieldLength )
126- m_CursorPos ++ ;
127- else if ( Input . GetKeyDown ( KeyCode . Home ) || ( Input . GetKeyDown ( KeyCode . A ) && ( Input . GetKey ( KeyCode . LeftControl ) || Input . GetKey ( KeyCode . RightControl ) ) ) )
128- m_CursorPos = 0 ;
129- else if ( Input . GetKeyDown ( KeyCode . End ) || ( Input . GetKeyDown ( KeyCode . E ) && ( Input . GetKey ( KeyCode . LeftControl ) || Input . GetKey ( KeyCode . RightControl ) ) ) )
130- m_CursorPos = m_InputFieldLength ;
131- else if ( Input . GetKeyDown ( KeyCode . Tab ) )
132- TabComplete ( ) ;
133- else if ( Input . GetKeyDown ( KeyCode . UpArrow ) )
134- HistoryPrev ( ) ;
135- else if ( Input . GetKeyDown ( KeyCode . DownArrow ) )
136- HistoryNext ( ) ;
137- else
175+ for ( var i = 0 ; i < m_InputChars . Count ; i ++ )
138176 {
139- // TODO replace with garbage free alternative (perhaps impossible until new input system?)
140- var inputString = Input . inputString ;
141- for ( var i = 0 ; i < inputString . Length ; i ++ )
177+ var ch = m_InputChars [ i ] ;
178+ if ( ch == '\b ' )
179+ Backspace ( ) ;
180+ else if ( ch == '\n ' || ch == '\r ' )
142181 {
143- var ch = inputString [ i ] ;
144- if ( ch == '\b ' )
145- Backspace ( ) ;
146- else if ( ch == '\n ' || ch == '\r ' )
147- {
148- var s = new string ( m_InputFieldBuffer , 0 , m_InputFieldLength ) ;
149- HistoryStore ( s ) ;
150- ExecuteCommand ( s ) ;
151- m_InputFieldLength = 0 ;
152- m_CursorPos = 0 ;
153- }
154- else
155- Type ( ch ) ;
182+ var s = new string ( m_InputFieldBuffer , 0 , m_InputFieldLength ) ;
183+ HistoryStore ( s ) ;
184+ ExecuteCommand ( s ) ;
185+ m_InputFieldLength = 0 ;
186+ m_CursorPos = 0 ;
156187 }
188+ else
189+ Type ( ch ) ;
157190 }
191+ m_InputChars . Clear ( ) ;
158192 }
159193 }
160194
0 commit comments