DeveloperConsole.OnGUI C# (CSharp) Method

OnGUI() public method

public OnGUI ( ) : void
return void
	void OnGUI ()
	{
		if ( guiSkin != null ) GUI.skin = guiSkin;
		
		bool doKey = false;
		EventType rawEventType = Event.current.type;
		if (showingAutocomplete && previousEvent != Event.current.type && (Event.current.type == EventType.KeyDown || Event.current.type == EventType.KeyUp) && (Event.current.keyCode == KeyCode.DownArrow || Event.current.keyCode == KeyCode.UpArrow))
		{
			Event.current.Use();
			doKey = true;
		}
		
		previousEvent = Event.current.type;
		
		if (displayAmount == 0f && !showing) return;
		
		Camera mainCamera = Camera.main;
		
		float consoleHeight = mainCamera.pixelHeight * ySizePercent;
		float consoleWidth = mainCamera.pixelWidth * xSizePercent;
		float leftMargin = ( mainCamera.pixelWidth - consoleWidth ) * 0.5f;
		
		Rect consoleRect = new Rect( leftMargin, Mathf.Lerp( -consoleHeight, 0f, displayAmount ), consoleWidth, consoleHeight );
		Rect consoleLogRect = new Rect( 0f, 0f, consoleWidth, consoleHeight - textFieldHeight );
		Rect consoleLogViewRect = new Rect( 0f, 0f, consoleWidth - 16f, Mathf.Max( consoleOutput.Count * lineHeight, consoleHeight - textFieldHeight ) );
		Rect consoleTextFieldRect = new Rect( 5f, consoleRect.height - textFieldHeight, consoleWidth - 10f, textFieldHeight );
		
		GUI.BeginGroup( consoleRect );
		
		// Background
		GUI.SetNextControlName( "ConsoleBackground" );
		GUI.Box( new Rect( 0f, 0f, consoleRect.width, consoleRect.height ), "" );
		
		// Log area
		scrollPosition = GUI.BeginScrollView( consoleLogRect, scrollPosition, consoleLogViewRect, false, true );
		for ( int i = 0; i < consoleOutput.Count; i++ )
		{
			string line = consoleOutput[i];
			GUI.Label( new Rect( 8f, i * lineHeight, consoleLogRect.width, lineHeight ), line );
		}
		GUI.EndScrollView();
		
		// Text Field
		string previousText = consoleText;
		GUI.SetNextControlName( "ConsoleTextField" );
		consoleText = GUI.TextField( consoleTextFieldRect, consoleText );
		consoleText = consoleText.Trim(disallowedChars);
		
		if ( previousText != consoleText )
		{
			closeAutocomplete = false;
			if ( previousTextEntries.Count > 0 ) previousEntriesIndex = previousTextEntries.Count;
			else previousEntriesIndex = 0;
		}
		
		if ( focusOnTextField )
		{
			GUI.FocusControl( "ConsoleBackground" );
			GUI.FocusControl( "ConsoleTextField" );
			
			TextEditor te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); if (te != null) { te.MoveCursorToPosition(new Vector2(5555, 5555)); }
		}
		focusOnTextField = false;
		
		GUI.EndGroup();
		
		if ( !closeAutocomplete )
		{
			bool hasMethodNames = foundMethodNames.Count > 0;
			bool hasFieldNames = foundFieldNames.Count > 0;
			bool hasPropertyNames = foundPropertyNames.Count > 0;
			
			if (hasMethodNames || hasFieldNames || hasPropertyNames)
			{
				int numWindows = 0;
				if (hasMethodNames) numWindows++;
				if (hasFieldNames || hasPropertyNames) numWindows++;
				
				autocompleteWindowIndex = Mathf.Clamp(autocompleteWindowIndex, 0, numWindows - 1);
				
				int selectedIndex = -1;
				float xPosition = Input.compositionCursorPos.x + autocompleteBoxMaxSize.x * 0.5f;
				if (hasMethodNames)
				{
					if (autocompleteWindowIndex == 0 )
					{
						autocompleteIndex = Mathf.Clamp(autocompleteIndex, 0, foundMethodNames.Count - 1);
						selectedIndex = autocompleteIndex;
					}
					DrawAutocompleteWindow("Methods", foundMethodNames.ToArray(), new Vector2(xPosition, consoleRect.yMax), selectedIndex, 0);
					xPosition += autocompleteBoxMaxSize.x + 10f;
				}
				if (hasFieldNames || hasPropertyNames)
				{
					selectedIndex = -1;
					string[] fieldAndPropertyNames = new string[foundFieldNames.Count + foundPropertyNames.Count];
					Array.Copy(foundFieldNames.ToArray(), 0, fieldAndPropertyNames, 0, foundFieldNames.Count);
					Array.Copy(foundPropertyNames.ToArray(), 0, fieldAndPropertyNames, foundFieldNames.Count, foundPropertyNames.Count);
					if (autocompleteWindowIndex == 0 + (Convert.ToInt32(hasMethodNames)))
					{
						autocompleteIndex = Mathf.Clamp(autocompleteIndex, 0, fieldAndPropertyNames.Length - 1);
						selectedIndex = autocompleteIndex;
					}
					
					DrawAutocompleteWindow("Fields & Properties", fieldAndPropertyNames, new Vector2(xPosition + autocompleteWindowPadding, consoleRect.yMax), selectedIndex, Convert.ToInt32(hasMethodNames));
				}
			}
			else if ( foundComponentNames.Count > 0 )
			{
				autocompleteIndex = Mathf.Clamp(autocompleteIndex, 0, foundComponentNames.Count - 1);
				autocompleteWindowIndex = 0;
				
				DrawAutocompleteWindow("Components", foundComponentNames.ToArray(), new Vector2(Input.compositionCursorPos.x + autocompleteBoxMaxSize.x * 0.5f, consoleRect.yMax), autocompleteIndex);
			}
			else if ( foundGameObjectNames.Count > 0 )
			{
				autocompleteIndex = Mathf.Clamp(autocompleteIndex, 0, foundGameObjectNames.Count - 1);
				autocompleteWindowIndex = 0;
				
				DrawAutocompleteWindow("GameObjects", foundGameObjectNames.ToArray(), new Vector2(Input.compositionCursorPos.x + autocompleteBoxMaxSize.x * 0.5f, consoleRect.yMax), autocompleteIndex);
			}
		}
		
		if (Event.current.keyCode == KeyCode.LeftArrow && Event.current.type == EventType.Used)
		{
			if (showingAutocomplete && !closeAutocomplete && autocompleteWindowIndex > 0)
			{
				autocompleteWindowIndex--;
				Event.current.Use();
				ScrollToDisplaySelected();
			}
		}
		if (Event.current.keyCode == KeyCode.RightArrow && Event.current.type == EventType.Used)
		{
			bool hasMethodNames = foundMethodNames.Count > 0;
			bool hasFieldNames = foundFieldNames.Count > 0;
			bool hasPropertyNames = foundPropertyNames.Count > 0;
			int numWindows = Convert.ToInt32(hasMethodNames) + Convert.ToInt32(hasFieldNames || hasPropertyNames);
			
			if (showingAutocomplete && !closeAutocomplete && autocompleteWindowIndex < (numWindows - 1))
			{
				autocompleteWindowIndex++;
				Event.current.Use();
				ScrollToDisplaySelected();
			}
		}
		
		if (Event.current.isKey || doKey)
		{
			if ( Event.current.type == EventType.KeyUp && Event.current.keyCode == autocompleteKeyCode )
			{
				if ( !closeAutocomplete )
				{
					ApplyAutocompleteText();
				}
				
				closeAutocomplete = false;
			}
			if ( Event.current.keyCode == KeyCode.DownArrow )
			{
				if (showingAutocomplete && !closeAutocomplete)
				{
					if (timeUntilAutocompleteSelectionChangeAllowed <= 0f)
					{
						autocompleteIndex++;
						timeUntilAutocompleteSelectionChangeAllowed = timeBetweenAutocompleteSelectionChange;
						ScrollToDisplaySelected();
					}
				}
				else if (rawEventType == EventType.KeyUp)
				{
					if ( previousEntriesIndex < previousTextEntries.Count ) previousEntriesIndex++;
					if ( previousTextEntries.Count > previousEntriesIndex ) consoleText = previousTextEntries[previousEntriesIndex];
					else consoleText = "";
				}
			}
			if ( Event.current.keyCode == KeyCode.UpArrow )
			{
				if (showingAutocomplete && !closeAutocomplete)
				{
					if (timeUntilAutocompleteSelectionChangeAllowed <= 0f)
					{
						autocompleteIndex--;
						timeUntilAutocompleteSelectionChangeAllowed = timeBetweenAutocompleteSelectionChange;
						ScrollToDisplaySelected();
					}
				}
				else if (rawEventType == EventType.KeyUp)
				{
					if ( previousEntriesIndex > 0 ) previousEntriesIndex--;
					if (previousTextEntries.Count > 0 && previousTextEntries.Count >= (previousEntriesIndex - 1)) consoleText = previousTextEntries[previousEntriesIndex];
				}
			}
			if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Escape)
			{
				closeAutocomplete = true;
			}
			if (Event.current.type == EventType.KeyUp && Event.current.keyCode == toggleKeyCode)
			{
				if (showing)
				{
					if (showCursorWhenActive)
					{
						Screen.showCursor = cursorShowingBefore;
						Screen.lockCursor = cursorLockedBefore;
					}
				}
				showing = !showing;
				if (showing) focusOnTextField = true;
			}
			if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Return)
			{
				closeAutocomplete = true;
				
				bool printResult = false;
				
				string workWithText = consoleText;
				
				// Replace aliases
				string[] keyArray = new string[dictionaryOfAliases.Count];
				dictionaryOfAliases.Keys.CopyTo(keyArray, 0);
				string[] valArray = new string[dictionaryOfAliases.Count];
				dictionaryOfAliases.Values.CopyTo(valArray, 0);
				
				for (int aI = 0; aI < dictionaryOfAliases.Count; aI++)
				{
					if (workWithText.StartsWith(keyArray[aI]))
					{
						workWithText = workWithText.Replace(keyArray[aI], valArray[aI]);
					}
				}
				
				if (workWithText.ToLower().StartsWith("print "))
				{
					workWithText = workWithText.Substring(6, workWithText.Length - 6);
					printResult = true;
				}
				
				string[] splitSpaces = workWithText.Split(' ');
				
				if (splitSpaces.Length > 0)
				{
					string[] splitDots = workWithText.Split('.');
					
					List<GameObject> gol = new List<GameObject>();
					
					if (splitDots.Length >= 3)
					{
						if (tagList.Any(item => item == splitDots[0]))
							gol = GameObject.FindGameObjectsWithTag(splitDots[0]).ToList();
						
						foreach(GameObject g in gol)
						{
							ApplyCommandsToGameObject(g, workWithText, printResult);
						}
						
						GameObject go = GameObject.Find(splitDots[0]);
						if (go != null)
						{
							ApplyCommandsToGameObject(go, workWithText, printResult);
						}
					}
				}
				
				previousTextEntries.Add(consoleText);
				consoleText = "";
				previousEntriesIndex = previousTextEntries.Count;
			}
		}
		
		GUI.skin = null;
	}