Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- public class InGameConsole : MonoBehaviour {
- private class Log {
- public Log(string log, string sender, LogType type) {
- this.log = log;
- this.sender = sender;
- this.type = type;
- }
- public string log;
- public string sender;
- public LogType type;
- }
- private static List<Log> logs;
- private static Vector2 scroll;
- private static InGameConsole instance;
- private static Log selected;
- private static bool open;
- [RuntimeInitializeOnLoadMethod]
- private static void Init() {
- logs = new List<Log>();
- instance = new GameObject("Console").AddComponent<InGameConsole>();
- DontDestroyOnLoad(instance.gameObject);
- open = false;
- Application.logMessageReceivedThreaded += (log, sender, type) => {
- logs.Add(new Log(log, sender, type));
- scroll.y = 4096f;
- open = true;
- };
- }
- //private void Update() {
- // if(Input.GetKeyDown(KeyCode.Escape)) {
- // Debug.Log("Simple LOG");
- // Debug.LogWarning("Simple Warning LOG");
- // Debug.LogError("Simple Error LOG");
- // }
- // if(Input.GetKeyDown(KeyCode.A))
- // Debug.Log("Simple LOG");
- // if(Input.GetKeyDown(KeyCode.S))
- // Debug.LogWarning("Simple Warning LOG");
- // if(Input.GetKeyDown(KeyCode.D))
- // Debug.LogError("Simple Error LOG");
- //}
- private void OnGUI() {
- if(!open)
- return;
- var windowStyle = new GUIStyle(GUI.skin.window);
- var boxStyle = new GUIStyle(GUI.skin.box);
- boxStyle.alignment = TextAnchor.UpperLeft;
- boxStyle.wordWrap = true;
- GUILayout.BeginArea(new Rect(0f, 0f, Screen.width * 0.5f, Screen.height * 0.5f), "Console", windowStyle);
- scroll = GUILayout.BeginScrollView(scroll, false, false, GUILayout.ExpandHeight(true));
- foreach(var log in logs)
- if(log != null) {
- var color = string.Empty;
- switch(log.type) {
- case LogType.Assert:
- case LogType.Error:
- case LogType.Exception:
- color = "red";
- break;
- case LogType.Warning:
- color = "orange";
- break;
- }
- if(selected == log)
- GUILayout.Box(string.Format("<color={0}>{1}: {2}</color>\n\n{3}", color, log.type, log.log, log.sender), boxStyle);
- else if(GUILayout.Button(string.Format("<color={0}>{1}: {2}</color>", color, log.type, log.log), boxStyle))
- selected = log;
- }
- GUILayout.EndScrollView();
- GUILayout.FlexibleSpace();
- GUILayout.BeginHorizontal();
- if(GUILayout.Button("Clear"))
- logs.Clear();
- if(GUILayout.Button("Close"))
- open = false;
- GUILayout.EndHorizontal();
- GUILayout.EndArea();
- }
- }
Add Comment
Please, Sign In to add comment