GUIConsole.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // People should be able to see and report errors to the developer very easily.
  2. //
  3. // Unity's Developer Console only works in development builds and it only shows
  4. // errors. This class provides a console that works in all builds and also shows
  5. // log and warnings in development builds.
  6. //
  7. // Note: we don't include the stack trace, because that can also be grabbed from
  8. // the log files if needed.
  9. //
  10. // Note: there is no 'hide' button because we DO want people to see those errors
  11. // and report them back to us.
  12. //
  13. // Note: normal Debug.Log messages can be shown by building in Debug/Development
  14. // mode.
  15. using UnityEngine;
  16. using System.Collections.Generic;
  17. namespace Mirror
  18. {
  19. struct LogEntry
  20. {
  21. public string message;
  22. public LogType type;
  23. public LogEntry(string message, LogType type)
  24. {
  25. this.message = message;
  26. this.type = type;
  27. }
  28. }
  29. public class GUIConsole : MonoBehaviour
  30. {
  31. public int height = 150;
  32. // only keep the recent 'n' entries. otherwise memory would grow forever
  33. // and drawing would get slower and slower.
  34. public int maxLogCount = 50;
  35. // log as queue so we can remove the first entry easily
  36. Queue<LogEntry> log = new Queue<LogEntry>();
  37. // hotkey to show/hide at runtime for easier debugging
  38. // (sometimes we need to temporarily hide/show it)
  39. // => F12 makes sense. nobody can find ^ in other games.
  40. public KeyCode hotKey = KeyCode.F12;
  41. // GUI
  42. bool visible;
  43. Vector2 scroll = Vector2.zero;
  44. void Awake()
  45. {
  46. Application.logMessageReceived += OnLog;
  47. }
  48. // OnLog logs everything, even Debug.Log messages in release builds
  49. // => this makes a lot of things easier. e.g. addon initialization logs.
  50. // => it's really better to have than not to have those
  51. void OnLog(string message, string stackTrace, LogType type)
  52. {
  53. // is this important?
  54. bool isImportant = type == LogType.Error || type == LogType.Exception;
  55. // use stack trace only if important
  56. // (otherwise users would have to find and search the log file.
  57. // seeing it in the console directly is way easier to deal with.)
  58. // => only add \n if stack trace is available (only in debug builds)
  59. if (isImportant && !string.IsNullOrWhiteSpace(stackTrace))
  60. message += "\n" + stackTrace;
  61. // add to queue
  62. log.Enqueue(new LogEntry(message, type));
  63. // respect max entries
  64. if (log.Count > maxLogCount)
  65. log.Dequeue();
  66. // become visible if it was important
  67. // (no need to become visible for regular log. let the user decide.)
  68. if (isImportant)
  69. visible = true;
  70. // auto scroll
  71. scroll.y = float.MaxValue;
  72. }
  73. void Update()
  74. {
  75. if (Input.GetKeyDown(hotKey))
  76. visible = !visible;
  77. }
  78. void OnGUI()
  79. {
  80. if (!visible) return;
  81. scroll = GUILayout.BeginScrollView(scroll, "Box", GUILayout.Width(Screen.width), GUILayout.Height(height));
  82. foreach (LogEntry entry in log)
  83. {
  84. if (entry.type == LogType.Error || entry.type == LogType.Exception)
  85. GUI.color = Color.red;
  86. else if (entry.type == LogType.Warning)
  87. GUI.color = Color.yellow;
  88. GUILayout.Label(entry.message);
  89. GUI.color = Color.white;
  90. }
  91. GUILayout.EndScrollView();
  92. }
  93. }
  94. }