123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
-
- using UnityEngine;
- using System.Collections.Generic;
- namespace Mirror
- {
- struct LogEntry
- {
- public string message;
- public LogType type;
- public LogEntry(string message, LogType type)
- {
- this.message = message;
- this.type = type;
- }
- }
- public class GUIConsole : MonoBehaviour
- {
- public int height = 150;
-
-
- public int maxLogCount = 50;
-
- Queue<LogEntry> log = new Queue<LogEntry>();
-
-
-
- public KeyCode hotKey = KeyCode.F12;
-
- bool visible;
- Vector2 scroll = Vector2.zero;
- void Awake()
- {
- Application.logMessageReceived += OnLog;
- }
-
-
-
- void OnLog(string message, string stackTrace, LogType type)
- {
-
- bool isImportant = type == LogType.Error || type == LogType.Exception;
-
-
-
-
- if (isImportant && !string.IsNullOrWhiteSpace(stackTrace))
- message += "\n" + stackTrace;
-
- log.Enqueue(new LogEntry(message, type));
-
- if (log.Count > maxLogCount)
- log.Dequeue();
-
-
- if (isImportant)
- visible = true;
-
- scroll.y = float.MaxValue;
- }
- void Update()
- {
- if (Input.GetKeyDown(hotKey))
- visible = !visible;
- }
- void OnGUI()
- {
- if (!visible) return;
- scroll = GUILayout.BeginScrollView(scroll, "Box", GUILayout.Width(Screen.width), GUILayout.Height(height));
- foreach (LogEntry entry in log)
- {
- if (entry.type == LogType.Error || entry.type == LogType.Exception)
- GUI.color = Color.red;
- else if (entry.type == LogType.Warning)
- GUI.color = Color.yellow;
- GUILayout.Label(entry.message);
- GUI.color = Color.white;
- }
- GUILayout.EndScrollView();
- }
- }
- }
|