Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5.  
  6. public class Terminal : MonoBehaviour
  7. {
  8. List<string> commands = new List<string>();
  9. List<string> found= new List<string>();
  10.  
  11. public Text autocompleteText;
  12. public bool terminalActive;
  13. string terminalInput;
  14. public InputField terminalField;
  15. string[] strArray;
  16. public GameObject terminalFieldGO;
  17.  
  18. void Start()
  19. {
  20.  
  21. terminalActive = false;
  22. terminalFieldGO.SetActive(false);
  23.  
  24. //add commands to the list
  25. commands.Add("load");
  26. commands.Add("reload");
  27. commands.Add("fast");
  28. commands.Add("slow");
  29.  
  30. }
  31.  
  32. public void OnType()
  33. {
  34. found = commands.FindAll( w => w.StartsWith(terminalField.text));
  35. if (found.Count > 0)
  36. {
  37. autocompleteText.text = found[0];
  38. }
  39.  
  40. else
  41. {
  42. autocompleteText.text="";
  43.  
  44. }
  45.  
  46. if(terminalField.text.Equals(""))
  47. {
  48. autocompleteText.text="";
  49. }
  50. }
  51.  
  52. void Update()
  53. {
  54.  
  55. InputProcessor();
  56. }
  57.  
  58. void InputProcessor()
  59. {
  60.  
  61. if(Input.anyKeyDown)
  62. {
  63. if(terminalActive)
  64. {
  65. List<string> found = commands.FindAll( w => w.StartsWith(terminalField.text));
  66. if (found.Count > 0 && Input.GetKeyDown(KeyCode.RightArrow))
  67. {
  68. terminalField.text = found[0];
  69. terminalField.MoveTextEnd(false);
  70.  
  71. }
  72. }
  73. }
  74.  
  75. if(Input.GetKeyDown(KeyCode.BackQuote))
  76. {
  77. terminalActive = !terminalActive;
  78. StartTerminal(terminalActive);
  79. }
  80.  
  81. if(Input.GetKeyDown(KeyCode.Return))
  82. {
  83. //check if a suggestion is available
  84. //if yes, make it the command
  85.  
  86. if (found.Count > 0)
  87. {
  88. terminalField.text = found[0];
  89. }
  90.  
  91. ProcessInput();
  92. }
  93.  
  94. if(terminalActive && Input.GetKeyDown(KeyCode.UpArrow))
  95. {
  96.  
  97. terminalField.text = PlayerPrefs.GetString("lastCommand", "");
  98. terminalField.MoveTextEnd(false);
  99. }
  100.  
  101. if(Input.GetKeyDown(KeyCode.Escape) && terminalActive)
  102. {
  103. DeactivateTerminal();
  104. }
  105. }
  106.  
  107. void ClearConsole()
  108. {
  109. // This simply does "LogEntries.Clear()" the long way:
  110. var logEntries = System.Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll");
  111. var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  112. clearMethod.Invoke(null,null);
  113. }
  114.  
  115. void StartTerminal(bool _terminalActiveStatus)
  116. {
  117. if(_terminalActiveStatus = true)
  118. {
  119. ClearConsole();
  120. terminalFieldGO.SetActive(true);
  121. UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(terminalField.gameObject, null);
  122. terminalField.OnPointerClick(new UnityEngine.EventSystems.PointerEventData(UnityEngine.EventSystems.EventSystem.current));
  123. Cursor.visible = true;
  124.  
  125. }
  126.  
  127. else
  128. {
  129. Cursor.visible = false;
  130. }
  131.  
  132. terminalActive = _terminalActiveStatus;
  133. }
  134.  
  135. void ProcessInput()
  136. {
  137. //store input
  138. string input = terminalField.text;
  139.  
  140. //remember this command
  141. PlayerPrefs.SetString("lastCommand", input);
  142. //break the input into two pieces separating at the space
  143. strArray = terminalField.text.Split(" "[0]);
  144.  
  145. //store the first part as the command
  146. string command = strArray[0];
  147.  
  148. //store the second part (if it exists) as a value that
  149. string value = "";
  150. if(strArray.Length > 1)
  151. {
  152. value = strArray[1];
  153. }
  154.  
  155. //clean the input field
  156. terminalField.text = "";
  157.  
  158. Execute(command, value);
  159.  
  160. DeactivateTerminal();
  161.  
  162.  
  163. }
  164.  
  165. void DeactivateTerminal()
  166. {
  167. terminalFieldGO.SetActive(false);
  168. terminalActive = false;
  169. }
  170.  
  171. void Execute(string _command, string _value)
  172. {
  173. _command = _command.ToLower();
  174. //Add whatever commands you want to in after the case.
  175. switch(_command)
  176. {
  177.  
  178. //>>>>>>>>>
  179. case "load":
  180.  
  181. Application.LoadLevel(int.Parse(_value));
  182. Debug.Log("Loading scene "+_value);
  183. break;
  184.  
  185. //>>>>>>>>>
  186. case "reload":
  187.  
  188. Application.LoadLevel(Application.loadedLevel);
  189. print("Reloading");
  190. break;
  191.  
  192. //>>>>>>>>>
  193. case "fast":
  194. Time.timeScale = 2*Time.timeScale;
  195. print(Time.timeScale+"x speed");
  196. break;
  197.  
  198. //>>>>>>>>>
  199. case "slow":
  200. Time.timeScale = Time.timeScale/2;
  201. print(Time.timeScale+"x speed");
  202. break;
  203.  
  204. //>>>>>>>>>
  205. case "fuck":
  206. if(_value.Equals("you"))
  207. {
  208. print("Fuck you too");
  209. }
  210.  
  211. else
  212. {
  213. print("Invalid command");
  214. }
  215.  
  216. break;
  217.  
  218. default:
  219. print("Invalid command");
  220. break;
  221. }
  222. }
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement