Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace EpicHax
- {
- public class Console : MonoBehaviour
- {
- public void OnEnable()
- {
- Application.RegisterLogCallback(new Application.LogCallback(this.HandleLog));
- }
- public void OnDisable()
- {
- Application.RegisterLogCallback(null);
- }
- public void Update()
- {
- if (Input.GetKeyDown(KeyCode.KeypadMinus))
- {
- Statics.consoleEnabled = !Statics.consoleEnabled;
- }
- this.show = Statics.consoleEnabled;
- }
- public void OnGUI()
- {
- if (!this.show)
- {
- return;
- }
- this.windowRect = GUILayout.Window(123456, this.windowRect, new GUI.WindowFunction(this.ConsoleWindow), "Console", new GUILayoutOption[0]);
- }
- public void ConsoleWindow(int windowID)
- {
- this.scrollPosition = GUILayout.BeginScrollView(this.scrollPosition, new GUILayoutOption[0]);
- for (int i = 0; i < this.logs.Count; i++)
- {
- Console.Log log = this.logs[i];
- if (!this.collapse || i <= 0 || !(log.message == this.logs[i - 1].message))
- {
- GUI.contentColor = Console.logTypeColors[log.type];
- GUILayout.Label(log.message, new GUILayoutOption[0]);
- }
- }
- GUILayout.EndScrollView();
- GUI.contentColor = Color.white;
- GUILayout.BeginHorizontal(new GUILayoutOption[0]);
- if (GUILayout.Button(this.clearLabel, new GUILayoutOption[0]))
- {
- this.logs.Clear();
- }
- this.collapse = GUILayout.Toggle(this.collapse, this.collapseLabel, new GUILayoutOption[]
- {
- GUILayout.ExpandWidth(false)
- });
- GUILayout.EndHorizontal();
- GUI.DragWindow(this.titleBarRect);
- }
- public void HandleLog(string message, string stackTrace, LogType type)
- {
- this.logs.Add(new Console.Log
- {
- message = message,
- stackTrace = stackTrace,
- type = type
- });
- }
- public Console()
- {
- }
- static Console()
- {
- }
- public KeyCode toggleKey = KeyCode.BackQuote;
- public List<Console.Log> logs = new List<Console.Log>();
- public Vector2 scrollPosition;
- public bool show;
- public bool collapse;
- public static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color>
- {
- {
- LogType.Assert,
- Color.green
- },
- {
- LogType.Error,
- Color.magenta
- },
- {
- LogType.Exception,
- Color.red
- },
- {
- LogType.Log,
- Color.white
- },
- {
- LogType.Warning,
- Color.yellow
- }
- };
- public const int margin = 20;
- public Rect windowRect = new Rect(20f, 20f, (float)(Screen.width - 80), (float)(Screen.height - 80));
- public Rect titleBarRect = new Rect(0f, 0f, 10000f, 20f);
- public GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
- public GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
- public struct Log
- {
- public string message;
- public string stackTrace;
- public LogType type;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement