2121using System . Collections ;
2222#if INPUT_SYSTEM_INSTALLED
2323using UnityEngine . InputSystem ;
24- using UnityEngine . InputSystem . UI ;
2524#endif
2625
2726using InputKey =
@@ -51,6 +50,9 @@ internal sealed class DevConsoleMono : MonoBehaviour
5150 private const float MaxConsoleHeight = 900 ;
5251 private const int CommandHistoryLength = 10 ;
5352 private const int MaxCachedEnumTypes = 6 ;
53+
54+ #region Input constants
55+
5456 private const InputKey DefaultToggleKey =
5557#if USE_NEW_INPUT_SYSTEM
5658 InputKey . Backquote ;
@@ -66,6 +68,23 @@ internal sealed class DevConsoleMono : MonoBehaviour
6668 "FAB_DevConsole.OldEventSystem" ;
6769#endif
6870
71+ #endregion
72+
73+ #region PlayerPref constants
74+
75+ private const string PrefConsoleToggleKey =
76+ #if USE_NEW_INPUT_SYSTEM
77+ "DevConsole.newConsoleToggleKey" ;
78+ #else
79+ "DevConsole.legacyConsoleToggleKey" ;
80+ #endif
81+ private const string PrefDisplayUnityLogs = "DevConsole.displayUnityLogs" ;
82+ private const string PrefDisplayUnityErrors = "DevConsole.displayUnityErrors" ;
83+ private const string PrefDisplayUnityExceptions = "DevConsole.displayUnityExceptions" ;
84+ private const string PrefDisplayUnityWarnings = "DevConsole.displayUnityWarnings" ;
85+
86+ #endregion
87+
6988 private static readonly Version _version = new Version ( 0 , 1 , 5 ) ;
7089 private static readonly string [ ] _permanentCommands =
7190 {
@@ -432,46 +451,52 @@ internal bool AddParameterType(Type type, Func<string, object> parseFunc)
432451
433452 #region Log methods
434453
454+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
435455 internal void Log ( object message )
436456 {
437457 _logTextStore += $ "\n { message } ";
438458 }
439459
460+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
440461 internal void Log ( object message , string htmlColour )
441462 {
442463 Log ( $ "<color={ htmlColour } >{ message } </color>") ;
443464 }
444465
466+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
445467 internal void LogVariable ( string variableName , object value , string suffix = "" )
446468 {
447469 Log ( $ "{ variableName } : { value } { suffix } .") ;
448470 }
449471
472+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
450473 internal void LogError ( object message )
451474 {
452475 Log ( message , ErrorColour ) ;
453476 }
454477
478+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
455479 internal void LogWarning ( object message )
456480 {
457481 Log ( message , WarningColour ) ;
458482 }
459483
484+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
460485 internal void LogSuccess ( object message )
461486 {
462487 Log ( message , SuccessColour ) ;
463488 }
464489
490+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
465491 internal void LogSeperator ( object message = null )
466492 {
467493 if ( message == null )
468494 {
469495 Log ( "-" ) ;
496+ return ;
470497 }
471- else
472- {
473- Log ( $ "- <b>{ message } </b> -") ;
474- }
498+
499+ Log ( $ "- <b>{ message } </b> -") ;
475500 }
476501
477502 internal void LogCommand ( )
@@ -481,11 +506,12 @@ internal void LogCommand()
481506
482507 internal void LogCommand ( string name )
483508 {
484- Command command = GetCommand ( name ) ;
485- if ( command != null )
509+ if ( ! GetCommand ( name , out Command command ) )
486510 {
487- Log ( $ ">> { command . ToFormattedString ( ) } ." ) ;
511+ return ;
488512 }
513+
514+ Log ( $ ">> { command . ToFormattedString ( ) } .") ;
489515 }
490516
491517 #endregion
@@ -587,7 +613,7 @@ private void Awake()
587613 _resizeButtonColour = _resizeButtonImage . color ;
588614 _logFieldPrefab . SetActive ( false ) ;
589615
590- InitPreferences ( ) ;
616+ LoadPreferences ( ) ;
591617 InitBuiltInCommands ( ) ;
592618 InitAttributeCommands ( ) ;
593619
@@ -658,7 +684,7 @@ private void LateUpdate()
658684 return ;
659685 }
660686
661- // Force the canvas to rebuild layouts, which will display the log correctly
687+ // Process the stored logs, displaying them to the console
662688 if ( _logTextStore != string . Empty )
663689 {
664690 ProcessStoredLogs ( ) ;
@@ -671,6 +697,11 @@ private void LateUpdate()
671697 return ;
672698 }
673699
700+ if ( ! ConsoleIsShowing )
701+ {
702+ return ;
703+ }
704+
674705 if ( _inputField . isFocused )
675706 {
676707 // Allow cycling through command suggestions using the UP and DOWN arrows
@@ -689,7 +720,8 @@ private void LateUpdate()
689720 // Allow cycling through command history using the UP and DOWN arrows
690721 else
691722 {
692- if ( _commandHistoryIndex != - 1 && InputText == string . Empty )
723+ // Reset the command history index if the input text is blank
724+ if ( string . IsNullOrEmpty ( InputText ) && _commandHistoryIndex != - 1 )
693725 {
694726 _commandHistoryIndex = - 1 ;
695727 }
@@ -708,37 +740,13 @@ private void LateUpdate()
708740
709741 private void OnDestroy ( )
710742 {
711- #if USE_NEW_INPUT_SYSTEM
712- // TODO: Save console toggle key in new input system
713- #else
714- PlayerPrefs . SetInt ( "DevConsole.legacyConsoleToggleKey" , ! ConsoleToggleKey . HasValue ? - 1 : ( int ) ConsoleToggleKey . Value ) ;
715- #endif
716- PlayerPrefs . SetInt ( "DevConsole.displayUnityLogs" , _displayUnityLogs ? 1 : 0 ) ;
717- PlayerPrefs . SetInt ( "DevConsole.displayUnityErrors" , _displayUnityErrors ? 1 : 0 ) ;
718- PlayerPrefs . SetInt ( "DevConsole.displayUnityExceptions" , _displayUnityExceptions ? 1 : 0 ) ;
719- PlayerPrefs . SetInt ( "DevConsole.displayUnityWarnings" , _displayUnityWarnings ? 1 : 0 ) ;
720-
721- PlayerPrefs . Save ( ) ;
743+ SavePreferences ( ) ;
722744 }
723745
724746 #endregion
725747
726748 #region Init methods
727749
728- private void InitPreferences ( )
729- {
730- #if USE_NEW_INPUT_SYSTEM
731- // TODO: Load console toggle key in new input system
732- #else
733- int n = PlayerPrefs . GetInt ( "DevConsole.legacyConsoleToggleKey" , ( int ) DefaultToggleKey ) ;
734- ConsoleToggleKey = n < 0 ? ( InputKey ? ) null : ( InputKey ) n ;
735- #endif
736- _displayUnityLogs = PlayerPrefs . GetInt ( "DevConsole.displayUnityLogs" , 1 ) == 1 ;
737- _displayUnityErrors = PlayerPrefs . GetInt ( "DevConsole.displayUnityErrors" , 1 ) == 1 ;
738- _displayUnityExceptions = PlayerPrefs . GetInt ( "DevConsole.displayUnityExceptions" , 1 ) == 1 ;
739- _displayUnityWarnings = PlayerPrefs . GetInt ( "DevConsole.displayUnityWarnings" , 1 ) == 1 ;
740- }
741-
742750 private void InitBuiltInCommands ( )
743751 {
744752 #region Console commands
@@ -1531,6 +1539,11 @@ private Command GetCommand(string name)
15311539 return _commands . TryGetValue ( name . ToLower ( ) , out Command command ) ? command : _commands . Values . FirstOrDefault ( c => c . HasAlias ( name ) ) ;
15321540 }
15331541
1542+ private bool GetCommand ( string name , out Command command )
1543+ {
1544+ return _commands . TryGetValue ( name . ToLower ( ) , out command ) || ( ( command = _commands . Values . FirstOrDefault ( c => c . HasAlias ( name ) ) ) != null ) ;
1545+ }
1546+
15341547 private string [ ] GetInput ( string rawInput )
15351548 {
15361549 string [ ] split = rawInput . Split ( ' ' ) ;
@@ -1752,6 +1765,7 @@ private void ProcessStoredLogs()
17521765 if ( vertexCountStored > MaximumTextVertices )
17531766 {
17541767 // TODO: Split into multiple
1768+ // For now, produce an error
17551769 _logTextStore = $ "<color={ ErrorColour } >Message to log exceeded { MaximumTextVertices } vertices and was ignored.</color>";
17561770 return ;
17571771 }
@@ -1819,6 +1833,7 @@ private void RefreshLogFieldsSize()
18191833 RebuildLayout ( ) ;
18201834 }
18211835
1836+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
18221837 private void RebuildLayout ( )
18231838 {
18241839 // Forcefully rebuild the layout, otherwise transforms are positioned incorrectly
@@ -1841,19 +1856,28 @@ IEnumerator ScrollToBottomCoroutine()
18411856
18421857 #region Physical input methods
18431858
1859+ /// <summary>
1860+ /// Check if the specified key was pressed this frame, using the correct input system.
1861+ /// </summary>
1862+ /// <param name="key"></param>
1863+ /// <returns></returns>
1864+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
18441865 private bool GetKeyDown ( InputKey key )
18451866 {
1846- // Check if the specified key was pressed this frame, using the correct input system
18471867#if USE_NEW_INPUT_SYSTEM
18481868 return Keyboard . current [ key ] . wasPressedThisFrame ;
18491869#else
18501870 return Input . GetKeyDown ( key ) ;
18511871#endif
18521872 }
18531873
1874+ /// <summary>
1875+ /// Get the current mouse position, using the correct input system.
1876+ /// </summary>
1877+ /// <returns></returns>
1878+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
18541879 private Vector2 GetMousePosition ( )
18551880 {
1856- // Get the current mouse position, using the correct input system
18571881#if USE_NEW_INPUT_SYSTEM
18581882 return Mouse . current . position . ReadValue ( ) ;
18591883#else
@@ -1863,6 +1887,32 @@ private Vector2 GetMousePosition()
18631887
18641888 #endregion
18651889
1890+ #region Pref methods
1891+
1892+ private void SavePreferences ( )
1893+ {
1894+ PlayerPrefs . SetInt ( PrefConsoleToggleKey , ! ConsoleToggleKey . HasValue ? - 1 : ( int ) ConsoleToggleKey . Value ) ;
1895+ PlayerPrefs . SetInt ( PrefDisplayUnityLogs , _displayUnityLogs ? 1 : 0 ) ;
1896+ PlayerPrefs . SetInt ( PrefDisplayUnityErrors , _displayUnityErrors ? 1 : 0 ) ;
1897+ PlayerPrefs . SetInt ( PrefDisplayUnityExceptions , _displayUnityExceptions ? 1 : 0 ) ;
1898+ PlayerPrefs . SetInt ( PrefDisplayUnityWarnings , _displayUnityWarnings ? 1 : 0 ) ;
1899+
1900+ PlayerPrefs . Save ( ) ;
1901+ }
1902+
1903+ private void LoadPreferences ( )
1904+ {
1905+ int n = PlayerPrefs . GetInt ( PrefConsoleToggleKey , ( int ) DefaultToggleKey ) ;
1906+ ConsoleToggleKey = n < 0 ? ( InputKey ? ) null : ( InputKey ) n ;
1907+
1908+ _displayUnityLogs = PlayerPrefs . GetInt ( PrefDisplayUnityLogs , 1 ) == 1 ;
1909+ _displayUnityErrors = PlayerPrefs . GetInt ( PrefDisplayUnityErrors , 1 ) == 1 ;
1910+ _displayUnityExceptions = PlayerPrefs . GetInt ( PrefDisplayUnityExceptions , 1 ) == 1 ;
1911+ _displayUnityWarnings = PlayerPrefs . GetInt ( PrefDisplayUnityWarnings , 1 ) == 1 ;
1912+ }
1913+
1914+ #endregion
1915+
18661916 #endregion
18671917 }
18681918}
0 commit comments