Skip to content

Instantly share code, notes, and snippets.

@ProtoJazz
Created November 25, 2015 15:09
Show Gist options
  • Select an option

  • Save ProtoJazz/8f87d3a9eff53326b5a4 to your computer and use it in GitHub Desktop.

Select an option

Save ProtoJazz/8f87d3a9eff53326b5a4 to your computer and use it in GitHub Desktop.
Unity Terminal
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class Terminal : MonoBehaviour
{
List<string> commands = new List<string>();
List<string> found= new List<string>();
public Text autocompleteText;
public bool terminalActive;
string terminalInput;
public InputField terminalField;
string[] strArray;
public GameObject terminalFieldGO;
void Start()
{
terminalActive = false;
terminalFieldGO.SetActive(false);
//add commands to the list
commands.Add("load");
commands.Add("reload");
commands.Add("fast");
commands.Add("slow");
}
public void OnType()
{
found = commands.FindAll( w => w.StartsWith(terminalField.text));
if (found.Count > 0)
{
autocompleteText.text = found[0];
}
else
{
autocompleteText.text="";
}
if(terminalField.text.Equals(""))
{
autocompleteText.text="";
}
}
void Update()
{
InputProcessor();
}
void InputProcessor()
{
if(Input.anyKeyDown)
{
if(terminalActive)
{
List<string> found = commands.FindAll( w => w.StartsWith(terminalField.text));
if (found.Count > 0 && Input.GetKeyDown(KeyCode.RightArrow))
{
terminalField.text = found[0];
terminalField.MoveTextEnd(false);
}
}
}
if(Input.GetKeyDown(KeyCode.BackQuote))
{
terminalActive = !terminalActive;
StartTerminal(terminalActive);
}
if(Input.GetKeyDown(KeyCode.Return))
{
//check if a suggestion is available
//if yes, make it the command
if (found.Count > 0)
{
terminalField.text = found[0];
}
ProcessInput();
}
if(terminalActive && Input.GetKeyDown(KeyCode.UpArrow))
{
terminalField.text = PlayerPrefs.GetString("lastCommand", "");
terminalField.MoveTextEnd(false);
}
if(Input.GetKeyDown(KeyCode.Escape) && terminalActive)
{
DeactivateTerminal();
}
}
void ClearConsole()
{
// This simply does "LogEntries.Clear()" the long way:
var logEntries = System.Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll");
var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
clearMethod.Invoke(null,null);
}
void StartTerminal(bool _terminalActiveStatus)
{
if(_terminalActiveStatus = true)
{
ClearConsole();
terminalFieldGO.SetActive(true);
UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(terminalField.gameObject, null);
terminalField.OnPointerClick(new UnityEngine.EventSystems.PointerEventData(UnityEngine.EventSystems.EventSystem.current));
Cursor.visible = true;
}
else
{
Cursor.visible = false;
}
terminalActive = _terminalActiveStatus;
}
void ProcessInput()
{
//store input
string input = terminalField.text;
//remember this command
PlayerPrefs.SetString("lastCommand", input);
//break the input into two pieces separating at the space
strArray = terminalField.text.Split(" "[0]);
//store the first part as the command
string command = strArray[0];
//store the second part (if it exists) as a value that
string value = "";
if(strArray.Length > 1)
{
value = strArray[1];
}
//clean the input field
terminalField.text = "";
Execute(command, value);
DeactivateTerminal();
}
void DeactivateTerminal()
{
terminalFieldGO.SetActive(false);
terminalActive = false;
}
void Execute(string _command, string _value)
{
_command = _command.ToLower();
//Add whatever commands you want to in after the case.
switch(_command)
{
//>>>>>>>>>
case "load":
Application.LoadLevel(int.Parse(_value));
Debug.Log("Loading scene "+_value);
break;
//>>>>>>>>>
case "reload":
Application.LoadLevel(Application.loadedLevel);
print("Reloading");
break;
//>>>>>>>>>
case "fast":
Time.timeScale = 2*Time.timeScale;
print(Time.timeScale+"x speed");
break;
//>>>>>>>>>
case "slow":
Time.timeScale = Time.timeScale/2;
print(Time.timeScale+"x speed");
break;
//>>>>>>>>>
case "fuck":
if(_value.Equals("you"))
{
print("Fuck you too");
}
else
{
print("Invalid command");
}
break;
default:
print("Invalid command");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment