Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4.  
  5. public class ConsoleManager : MonoBehaviour
  6. {
  7. // General Variables
  8. private static string serverLogPath = System.Environment.CurrentDirectory + "\\serverlog.txt";
  9. private static bool consoleToggle = false;
  10. private static KeyCode hotKey = KeyCode.BackQuote;
  11.  
  12. // GUIStyles
  13. private RectOffset zeroOffset;
  14. private GUIStyle zeroPaddingStyle;
  15. private GUIStyle consoleStyle;
  16. private GUIStyle lineStyle;
  17. private GUIStyle textfieldStyle;
  18. private GUIStyle scrollbarStyle;
  19.  
  20. // Style Configuration
  21. private static int fontSize = 14;
  22. private static Color bodyColor = new Color (0.0F, 0.0F, 0.0F, 0.3F);
  23. private static Color textColor = Color.white;
  24. private static Color borderColor = Color.black;
  25. private static int borderThickness = 2;
  26. private static Color scrollbarThumbColor = Color.white;
  27.  
  28. // Console Position
  29. private static Vector2 consolePosition = new Vector2(10, 10); // Position relative to top left corner heading down and right
  30.  
  31. // Console Width
  32. private static int consoleWidth = 600; // Total console width, scrollbar plus write area
  33.  
  34. // Console Height
  35. private static int consoleLines = 15; // This is what you should manipulate to elongate the console
  36. private static int consoleHeight = fontSize * consoleLines; // total height in pixels is the Font Size multiplied by how many lines we want to display
  37.  
  38. // Scroll Bar
  39. private static int scrollBarWidth = 20;
  40. private float scrollbarValue;
  41.  
  42. // Text Field
  43. private int textfieldHeight = 20;
  44. private string textfieldValue = "";
  45. private int textfieldMaxCharacters = 50;
  46.  
  47.  
  48. // Calls up the console when consoleKey is being pressed
  49. void OnGUI()
  50. {
  51. // Deal with loading up the GUIStyles, only once
  52.  
  53. // GUIStyle for removing padding and margins
  54. if (zeroPaddingStyle == null) {
  55. zeroOffset = new RectOffset (1, 0, -1, 0);
  56. zeroPaddingStyle = new GUIStyle (GUI.skin.label);
  57. zeroPaddingStyle.padding = zeroOffset;
  58. zeroPaddingStyle.margin = zeroOffset;
  59. }
  60.  
  61. // Console-Background style creation
  62. if (consoleStyle == null) {
  63. consoleStyle = new GUIStyle (GUI.skin.box);
  64. consoleStyle.normal.background = MakeTextureColor (consoleWidth, consoleHeight, bodyColor, borderThickness, borderColor);
  65. consoleStyle.padding = zeroOffset;
  66. consoleStyle.margin = zeroOffset;
  67. }
  68. // Console-Line style creation
  69. if (lineStyle == null) {
  70. lineStyle = new GUIStyle (GUI.skin.label);
  71. lineStyle.fontSize = fontSize;
  72. lineStyle.normal.textColor = textColor;
  73. lineStyle.padding = zeroOffset;
  74. lineStyle.margin = zeroOffset;
  75. }
  76. // Text-Input Style Creation
  77. if (textfieldStyle == null) {
  78. textfieldStyle = new GUIStyle (GUI.skin.textField);
  79. Texture2D textfieldTexture = MakeTextureColor (consoleWidth, textfieldHeight, bodyColor, borderThickness, borderColor);
  80. textfieldStyle.normal.background = textfieldTexture;
  81. textfieldStyle.focused.background = textfieldTexture;
  82. textfieldStyle.hover.background = textfieldTexture;
  83. }
  84.  
  85. if (scrollbarStyle == null) {
  86. scrollbarStyle = new GUIStyle (GUI.skin.verticalScrollbar);
  87. scrollbarStyle.normal.background = MakeTextureColor (scrollBarWidth, consoleHeight, bodyColor, borderThickness - 1, borderColor);
  88. }
  89.  
  90. // See if we need to toggle the console
  91. // This is done only during the Layout GUI phase, so as to prevent double-checking
  92. if(Event.current.type == EventType.Layout && Input.GetKeyDown(hotKey)) {
  93. consoleToggle = !consoleToggle;
  94. }
  95.  
  96. // Display the console if it's open
  97. if (consoleToggle) {
  98. Console ();
  99. }
  100. }
  101.  
  102. // Drawing the console
  103. void Console()
  104. {
  105. // Pull serverlog contents
  106. string[] serverLogLines = File.ReadAllLines (serverLogPath);
  107.  
  108. // Console-Background Creation
  109. Rect consoleBorder = new Rect(consolePosition.x - borderThickness, consolePosition.y - borderThickness, consoleWidth + scrollBarWidth + borderThickness, consoleHeight + textfieldHeight + borderThickness + fontSize);
  110. GUI.Box (consoleBorder, "Console", consoleStyle);
  111.  
  112. // Console-Output-Area creation
  113. Rect consoleOutputArea = new Rect(consolePosition.x, consolePosition.y + fontSize, consoleWidth, consoleHeight);
  114. GUILayout.BeginArea(consoleOutputArea, zeroPaddingStyle);
  115.  
  116. // Empty space to force everything downwards
  117. GUILayout.FlexibleSpace ();
  118.  
  119. // Find start point so as to draw only the lines we can see in the console
  120. int logStartPoint;
  121. if(serverLogLines.Length <= consoleLines) {
  122. // Start at the beginning
  123. logStartPoint = 0;
  124. } else {
  125. // Start at the oldest line we can see
  126. logStartPoint = serverLogLines.Length - consoleLines - (int) scrollbarValue;
  127. }
  128.  
  129. // Line Writing
  130. for(int i = logStartPoint; i < serverLogLines.Length; i++) {
  131. // Read the line at this index
  132. string line = serverLogLines[i];
  133.  
  134. // Line writing
  135. GUILayout.BeginVertical(zeroPaddingStyle, GUILayout.Width(consoleWidth));
  136.  
  137. GUILayout.Label (line, lineStyle, GUILayout.Width(consoleWidth));
  138.  
  139. GUILayout.EndVertical();
  140. }
  141.  
  142. // End the console-output area
  143. GUILayout.EndArea();
  144.  
  145. // Scroll-Bar Creation
  146. int paddingBuffer = 4;
  147. Rect scrollbarRect = new Rect (consolePosition.x + consoleWidth + paddingBuffer, consolePosition.y - 1, scrollBarWidth, consoleHeight + fontSize + 1);
  148. scrollbarValue = GUI.VerticalScrollbar (scrollbarRect, scrollbarValue, 1.0F, serverLogLines.Length - consoleLines + 1, 0.0F, scrollbarStyle);
  149.  
  150. // Text-Input Creation
  151. Rect textfieldRect = new Rect (consolePosition.x, consolePosition.y + consoleHeight + fontSize, consoleWidth + scrollBarWidth, textfieldHeight);
  152. textfieldValue = GUI.TextField (textfieldRect, textfieldValue, textfieldMaxCharacters, textfieldStyle);
  153.  
  154. // Grab entered input from text field
  155. if(Event.current.keyCode == KeyCode.Return && textfieldValue != "" && textfieldValue != "\n") {
  156. string command = textfieldValue;
  157.  
  158. if(textfieldValue.Contains("\n")) {
  159. textfieldValue = textfieldValue.Remove(0, 1);
  160. }
  161.  
  162. ExecuteInput(textfieldValue);
  163. textfieldValue = "";
  164. }
  165. }
  166.  
  167. // Executes the console input
  168. void ExecuteInput(string command)
  169. {
  170. // Temporary, System.out echo function
  171. print (">" + command);
  172. }
  173.  
  174. // Returns of a solid texture of width * height of color 'color'
  175. private Texture2D MakeTextureColor(int width, int height, Color bodyColor, int borderThickness, Color borderColor)
  176. {
  177. // Create an array to contain all of our pixels
  178. Color[] pixels = new Color[width * height];
  179.  
  180. // Set each pixel to our color
  181. for (int i = 0; i < pixels.Length; i++) {
  182. // Determine where the pixel is located in 2d space
  183. int x = (i % width);
  184. int y = (i / width);
  185.  
  186. // See if we need to draw the border, or the body
  187. if(x < borderThickness || y < borderThickness || x > (width - borderThickness) || y > (height - borderThickness)) {
  188. pixels[i] = borderColor;
  189. } else {
  190. pixels [i] = bodyColor;
  191. }
  192. }
  193.  
  194. // Apply the pixels to the texture
  195. Texture2D output = new Texture2D (width, height);
  196. output.SetPixels (pixels);
  197. output.Apply ();
  198.  
  199. return output;
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement