GUIConsole.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // => always show exceptions & errors
  55. // => usually a good idea to show warnings too, otherwise it's too
  56. // easy to miss OnDeserialize warnings etc. in builds
  57. bool isImportant = type == LogType.Error || type == LogType.Exception || type == LogType.Warning;
  58. // use stack trace only if important
  59. // (otherwise users would have to find and search the log file.
  60. // seeing it in the console directly is way easier to deal with.)
  61. // => only add \n if stack trace is available (only in debug builds)
  62. if (isImportant && !string.IsNullOrWhiteSpace(stackTrace))
  63. message += $"\n{stackTrace}";
  64. // add to queue
  65. log.Enqueue(new LogEntry(message, type));
  66. // respect max entries
  67. if (log.Count > maxLogCount)
  68. log.Dequeue();
  69. // become visible if it was important
  70. // (no need to become visible for regular log. let the user decide.)
  71. if (isImportant)
  72. visible = true;
  73. // auto scroll
  74. scroll.y = float.MaxValue;
  75. }
  76. void Update()
  77. {
  78. if (Input.GetKeyDown(hotKey))
  79. visible = !visible;
  80. }
  81. void OnGUI()
  82. {
  83. if (!visible) return;
  84. scroll = GUILayout.BeginScrollView(scroll, "Box", GUILayout.Width(Screen.width), GUILayout.Height(height));
  85. foreach (LogEntry entry in log)
  86. {
  87. if (entry.type == LogType.Error || entry.type == LogType.Exception)
  88. GUI.color = Color.red;
  89. else if (entry.type == LogType.Warning)
  90. GUI.color = Color.yellow;
  91. GUILayout.Label(entry.message);
  92. GUI.color = Color.white;
  93. }
  94. GUILayout.EndScrollView();
  95. }
  96. }
  97. }