Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.IO;
- public class ConsoleManager : MonoBehaviour
- {
- // General Variables
- private static string serverLogPath = System.Environment.CurrentDirectory + "\\serverlog.txt";
- private static bool consoleToggle = false;
- private static KeyCode hotKey = KeyCode.BackQuote;
- // GUIStyles
- private RectOffset zeroOffset;
- private GUIStyle zeroPaddingStyle;
- private GUIStyle consoleStyle;
- private GUIStyle lineStyle;
- private GUIStyle textfieldStyle;
- private GUIStyle scrollbarStyle;
- // Style Configuration
- private static int fontSize = 14;
- private static Color bodyColor = new Color (0.0F, 0.0F, 0.0F, 0.3F);
- private static Color textColor = Color.white;
- private static Color borderColor = Color.black;
- private static int borderThickness = 2;
- private static Color scrollbarThumbColor = Color.white;
- // Console Position
- private static Vector2 consolePosition = new Vector2(10, 10); // Position relative to top left corner heading down and right
- // Console Width
- private static int consoleWidth = 600; // Total console width, scrollbar plus write area
- // Console Height
- private static int consoleLines = 15; // This is what you should manipulate to elongate the console
- private static int consoleHeight = fontSize * consoleLines; // total height in pixels is the Font Size multiplied by how many lines we want to display
- // Scroll Bar
- private static int scrollBarWidth = 20;
- private float scrollbarValue;
- // Text Field
- private int textfieldHeight = 20;
- private string textfieldValue = "";
- private int textfieldMaxCharacters = 50;
- // Calls up the console when consoleKey is being pressed
- void OnGUI()
- {
- // Deal with loading up the GUIStyles, only once
- // GUIStyle for removing padding and margins
- if (zeroPaddingStyle == null) {
- zeroOffset = new RectOffset (1, 0, -1, 0);
- zeroPaddingStyle = new GUIStyle (GUI.skin.label);
- zeroPaddingStyle.padding = zeroOffset;
- zeroPaddingStyle.margin = zeroOffset;
- }
- // Console-Background style creation
- if (consoleStyle == null) {
- consoleStyle = new GUIStyle (GUI.skin.box);
- consoleStyle.normal.background = MakeTextureColor (consoleWidth, consoleHeight, bodyColor, borderThickness, borderColor);
- consoleStyle.padding = zeroOffset;
- consoleStyle.margin = zeroOffset;
- }
- // Console-Line style creation
- if (lineStyle == null) {
- lineStyle = new GUIStyle (GUI.skin.label);
- lineStyle.fontSize = fontSize;
- lineStyle.normal.textColor = textColor;
- lineStyle.padding = zeroOffset;
- lineStyle.margin = zeroOffset;
- }
- // Text-Input Style Creation
- if (textfieldStyle == null) {
- textfieldStyle = new GUIStyle (GUI.skin.textField);
- Texture2D textfieldTexture = MakeTextureColor (consoleWidth, textfieldHeight, bodyColor, borderThickness, borderColor);
- textfieldStyle.normal.background = textfieldTexture;
- textfieldStyle.focused.background = textfieldTexture;
- textfieldStyle.hover.background = textfieldTexture;
- }
- if (scrollbarStyle == null) {
- scrollbarStyle = new GUIStyle (GUI.skin.verticalScrollbar);
- scrollbarStyle.normal.background = MakeTextureColor (scrollBarWidth, consoleHeight, bodyColor, borderThickness - 1, borderColor);
- }
- // See if we need to toggle the console
- // This is done only during the Layout GUI phase, so as to prevent double-checking
- if(Event.current.type == EventType.Layout && Input.GetKeyDown(hotKey)) {
- consoleToggle = !consoleToggle;
- }
- // Display the console if it's open
- if (consoleToggle) {
- Console ();
- }
- }
- // Drawing the console
- void Console()
- {
- // Pull serverlog contents
- string[] serverLogLines = File.ReadAllLines (serverLogPath);
- // Console-Background Creation
- Rect consoleBorder = new Rect(consolePosition.x - borderThickness, consolePosition.y - borderThickness, consoleWidth + scrollBarWidth + borderThickness, consoleHeight + textfieldHeight + borderThickness + fontSize);
- GUI.Box (consoleBorder, "Console", consoleStyle);
- // Console-Output-Area creation
- Rect consoleOutputArea = new Rect(consolePosition.x, consolePosition.y + fontSize, consoleWidth, consoleHeight);
- GUILayout.BeginArea(consoleOutputArea, zeroPaddingStyle);
- // Empty space to force everything downwards
- GUILayout.FlexibleSpace ();
- // Find start point so as to draw only the lines we can see in the console
- int logStartPoint;
- if(serverLogLines.Length <= consoleLines) {
- // Start at the beginning
- logStartPoint = 0;
- } else {
- // Start at the oldest line we can see
- logStartPoint = serverLogLines.Length - consoleLines - (int) scrollbarValue;
- }
- // Line Writing
- for(int i = logStartPoint; i < serverLogLines.Length; i++) {
- // Read the line at this index
- string line = serverLogLines[i];
- // Line writing
- GUILayout.BeginVertical(zeroPaddingStyle, GUILayout.Width(consoleWidth));
- GUILayout.Label (line, lineStyle, GUILayout.Width(consoleWidth));
- GUILayout.EndVertical();
- }
- // End the console-output area
- GUILayout.EndArea();
- // Scroll-Bar Creation
- int paddingBuffer = 4;
- Rect scrollbarRect = new Rect (consolePosition.x + consoleWidth + paddingBuffer, consolePosition.y - 1, scrollBarWidth, consoleHeight + fontSize + 1);
- scrollbarValue = GUI.VerticalScrollbar (scrollbarRect, scrollbarValue, 1.0F, serverLogLines.Length - consoleLines + 1, 0.0F, scrollbarStyle);
- // Text-Input Creation
- Rect textfieldRect = new Rect (consolePosition.x, consolePosition.y + consoleHeight + fontSize, consoleWidth + scrollBarWidth, textfieldHeight);
- textfieldValue = GUI.TextField (textfieldRect, textfieldValue, textfieldMaxCharacters, textfieldStyle);
- // Grab entered input from text field
- if(Event.current.keyCode == KeyCode.Return && textfieldValue != "" && textfieldValue != "\n") {
- string command = textfieldValue;
- if(textfieldValue.Contains("\n")) {
- textfieldValue = textfieldValue.Remove(0, 1);
- }
- ExecuteInput(textfieldValue);
- textfieldValue = "";
- }
- }
- // Executes the console input
- void ExecuteInput(string command)
- {
- // Temporary, System.out echo function
- print (">" + command);
- }
- // Returns of a solid texture of width * height of color 'color'
- private Texture2D MakeTextureColor(int width, int height, Color bodyColor, int borderThickness, Color borderColor)
- {
- // Create an array to contain all of our pixels
- Color[] pixels = new Color[width * height];
- // Set each pixel to our color
- for (int i = 0; i < pixels.Length; i++) {
- // Determine where the pixel is located in 2d space
- int x = (i % width);
- int y = (i / width);
- // See if we need to draw the border, or the body
- if(x < borderThickness || y < borderThickness || x > (width - borderThickness) || y > (height - borderThickness)) {
- pixels[i] = borderColor;
- } else {
- pixels [i] = bodyColor;
- }
- }
- // Apply the pixels to the texture
- Texture2D output = new Texture2D (width, height);
- output.SetPixels (pixels);
- output.Apply ();
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement