Muk99

InGameConsole

Jan 26th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class InGameConsole : MonoBehaviour {
  5.  
  6.     private class Log {
  7.         public Log(string log, string sender, LogType type) {
  8.             this.log = log;
  9.             this.sender = sender;
  10.             this.type = type;
  11.         }
  12.  
  13.         public string log;
  14.         public string sender;
  15.         public LogType type;
  16.     }
  17.  
  18.     private static List<Log> logs;
  19.     private static Vector2 scroll;
  20.     private static InGameConsole instance;
  21.     private static Log selected;
  22.     private static bool open;
  23.  
  24.     [RuntimeInitializeOnLoadMethod]
  25.     private static void Init() {
  26.         logs = new List<Log>();
  27.  
  28.         instance = new GameObject("Console").AddComponent<InGameConsole>();
  29.         DontDestroyOnLoad(instance.gameObject);
  30.         open = false;
  31.  
  32.         Application.logMessageReceivedThreaded += (log, sender, type) => {
  33.             logs.Add(new Log(log, sender, type));
  34.             scroll.y = 4096f;
  35.             open = true;
  36.         };
  37.     }
  38.  
  39.     //private void Update() {
  40.     //    if(Input.GetKeyDown(KeyCode.Escape)) {
  41.     //        Debug.Log("Simple LOG");
  42.     //        Debug.LogWarning("Simple Warning LOG");
  43.     //        Debug.LogError("Simple Error LOG");
  44.     //    }
  45.  
  46.     //    if(Input.GetKeyDown(KeyCode.A))
  47.     //        Debug.Log("Simple LOG");
  48.     //    if(Input.GetKeyDown(KeyCode.S))
  49.     //        Debug.LogWarning("Simple Warning LOG");
  50.     //    if(Input.GetKeyDown(KeyCode.D))
  51.     //        Debug.LogError("Simple Error LOG");
  52.     //}
  53.  
  54.     private void OnGUI() {
  55.         if(!open)
  56.             return;
  57.  
  58.         var windowStyle = new GUIStyle(GUI.skin.window);
  59.         var boxStyle = new GUIStyle(GUI.skin.box);
  60.  
  61.         boxStyle.alignment = TextAnchor.UpperLeft;
  62.         boxStyle.wordWrap = true;
  63.  
  64.         GUILayout.BeginArea(new Rect(0f, 0f, Screen.width * 0.5f, Screen.height * 0.5f), "Console", windowStyle);
  65.         scroll = GUILayout.BeginScrollView(scroll, false, false, GUILayout.ExpandHeight(true));
  66.  
  67.  
  68.         foreach(var log in logs)
  69.             if(log != null) {
  70.                 var color = string.Empty;
  71.  
  72.                 switch(log.type) {
  73.                     case LogType.Assert:
  74.                     case LogType.Error:
  75.                     case LogType.Exception:
  76.                         color = "red";
  77.                         break;
  78.                     case LogType.Warning:
  79.                         color = "orange";
  80.                         break;
  81.                 }
  82.  
  83.                 if(selected == log)
  84.                     GUILayout.Box(string.Format("<color={0}>{1}: {2}</color>\n\n{3}", color, log.type, log.log, log.sender), boxStyle);
  85.                 else if(GUILayout.Button(string.Format("<color={0}>{1}: {2}</color>", color, log.type, log.log), boxStyle))
  86.                     selected = log;
  87.             }
  88.  
  89.         GUILayout.EndScrollView();
  90.         GUILayout.FlexibleSpace();
  91.         GUILayout.BeginHorizontal();
  92.         if(GUILayout.Button("Clear"))
  93.             logs.Clear();
  94.         if(GUILayout.Button("Close"))
  95.             open = false;
  96.         GUILayout.EndHorizontal();
  97.         GUILayout.EndArea();
  98.     }
  99. }
Add Comment
Please, Sign In to add comment