Free Console System

Decided today having a console system might be useful, so whipped this up.

Includes a few special example commands (send, message, clear), supports color coding for each line of text, ConsoleSystem.Log is static so can be called anywhere.

Its basic, uses built-in GUI, but its also free, so if it doesn’t work for you, too bad (no offence) - It works perfectly for me.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ConsoleSystem : MonoBehaviour {
	
	bool showConsole;
	string command = "";
	List<HistoryItem> consoleHistory;
	Vector2 scrollPosition = Vector2.zero;
	
	//main console window style
	public GUIStyle consoleBackground = new GUIStyle();
	
	//text styles for items displayed in console
	public GUIStyle consoleTextNone = new GUIStyle();
	public GUIStyle consoleTextSpecial = new GUIStyle();
	public GUIStyle consoleTextResult = new GUIStyle();
	public GUIStyle consoleTextLog = new GUIStyle();
	
		
	public int ItemHeight = 20;		//line height for each item
	public int MaxItems = 2000;  	//maximum history records
	public int MaxDisplayed = 10; 	//default console size - will show 10 historic items
	

	internal enum CommandType
	{
		None,
		Result,
		Log,
		Special
	};
	
	internal class HistoryItem
	{
		internal string command;
		internal CommandType type;
	};
	
	static ConsoleSystem instance;
	public static void Log(string message)
	{
		if(instance != null)
			instance.AddHistory(message, CommandType.Log);
	}

	void Start () {
		//setup static instance for static calls
		instance = this;
		consoleHistory = new List<HistoryItem>();
		
		//setup console styles
		consoleTextNone.normal.textColor = Color.white;
		consoleTextSpecial.normal.textColor = Color.blue;
		consoleTextResult.normal.textColor = Color.green;
		consoleTextLog.normal.textColor = Color.yellow;
		
		//create a default texture to use for background of console
		Texture2D tex = new Texture2D(1, 1);
		tex.SetPixel(0,0, Color.gray);
		tex.Apply();
		consoleBackground.normal.background = tex;
		
		ConsoleSystem.Log("Console System Initialised");
	}
	
	void Update () {
		if(Input.GetKeyDown(KeyCode.BackQuote))
			showConsole = !showConsole;
	}
	
	void AddHistory(string command, CommandType type)
	{
		consoleHistory.Add(new HistoryItem() { command = command, type = type });
	
		if(consoleHistory.Count > MaxItems)
			consoleHistory.RemoveAt(0);
	}
	
	GUIStyle GetStyle(CommandType type)
	{
		switch(type)
		{
			case CommandType.None:
				return consoleTextNone;
			case CommandType.Result:
				return consoleTextResult;
			case CommandType.Special:
				return consoleTextSpecial;
			case CommandType.Log:
				return consoleTextLog;
		}
		
		return consoleTextNone;
	}
	
	void OnGUI()
	{
		if(showConsole)
		{
			//catch certain key presses
			Event e = Event.current;
        	if (e != null)
			{
				if(e.type == EventType.KeyDown)
				{
					if(e.keyCode == KeyCode.Return) 
						ExecuteCommand();
					if(e.keyCode == KeyCode.BackQuote)
					{
						showConsole = false;
						e.Use();
					}
				}
			}
			
			GUI.BeginGroup(new Rect(0,0, Screen.width, (MaxDisplayed+1) * ItemHeight), consoleBackground);	
			
			scrollPosition = GUI.BeginScrollView(new Rect(0,0, Screen.width, MaxDisplayed * ItemHeight), scrollPosition, new Rect(0, 0, Screen.width-20, consoleHistory.Count*ItemHeight),false, true);
			
			int index = 0;
			foreach(HistoryItem history in consoleHistory)
			{
				GUI.Label(new Rect(0,index*ItemHeight,Screen.width,ItemHeight), history.command, GetStyle(history.type));
				index++;
			}
			 GUI.EndScrollView();
			
			command = command.Replace("`","");
			
			GUI.SetNextControlName("CommandField");
			command = GUI.TextField(new Rect(0,MaxDisplayed * ItemHeight,Screen.width, ItemHeight),command);
			GUI.EndGroup();
			
			GUI.FocusControl("CommandField");
			
			if(scrollPosition.y < 0)
				scrollPosition.y = 0;
		}
	}

	void ExecuteCommand()
	{
		if(command.Trim().Length > 0)
		{
			string c = command.ToLower().Trim();
			if(c.StartsWith("clear"))
				consoleHistory.Clear();
			else 
			{
				//check for special console commands				
				if(c.StartsWith("send"))
				{
					AddHistory(command, CommandType.Special);
					SendCommand(command.Trim());
				}
				else if(c.StartsWith("message"))
				{
					AddHistory(command, CommandType.Special);
					MessageCommand(command.Trim());
				}
				else AddHistory(command, CommandType.None);
			}
			scrollPosition.y = Mathf.Clamp((consoleHistory.Count * ItemHeight) - (MaxDisplayed * ItemHeight), 0 , MaxItems * ItemHeight);
		}
		command = "";	
	}
	
	//calls to do somethign with an interpretted console command
	void SendCommand(string command)
	{
		AddHistory("\""+command.TrimStart().Remove(0, 4).Trim()+"\"", CommandType.Result);
	}
	
	void MessageCommand(string command)
	{
		AddHistory("\""+command.TrimStart().Remove(0, 7).Trim()+"\"", CommandType.Result);	
	}
	

}

I don’t think that can find enough nice things to say about you. This is nice script. Thank you, sir.

Exactly what I was looking for!
Super-Easy to implement/use/adapt
A big thank you !
(strange that only one person posted here in 2 years)