LogGUI.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. //#define USE_TESTCONSOLE
  4. /// <summary>
  5. /// A console to display Unity's debug logs in-game.
  6. /// </summary>
  7. class LogGUI : MonoBehaviour
  8. {
  9. struct Log
  10. {
  11. public string message;
  12. public string stackTrace;
  13. public LogType type;
  14. }
  15. #region Inspector Settings
  16. /// <summary>
  17. /// The hotkey to show and hide the console window.
  18. /// </summary>
  19. public KeyCode toggleKey = KeyCode.BackQuote;
  20. /// <summary>
  21. /// Whether to open the window by shaking the device (mobile-only).
  22. /// </summary>
  23. public bool shakeToOpen = true;
  24. /// <summary>
  25. /// The (squared) acceleration above which the window should open.
  26. /// </summary>
  27. public float shakeAcceleration = 3f;
  28. /// <summary>
  29. /// Whether to only keep a certain number of logs.
  30. ///
  31. /// Setting this can be helpful if memory usage is a concern.
  32. /// </summary>
  33. public bool restrictLogCount = false;
  34. /// <summary>
  35. /// Number of logs to keep before removing old ones.
  36. /// </summary>
  37. public int maxLogs = 1000;
  38. #endregion
  39. readonly List<Log> logs = new List<Log>();
  40. Vector2 scrollPosition;
  41. bool visible;
  42. bool collapse;
  43. // Visual elements:
  44. static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color>
  45. {
  46. { LogType.Assert, Color.white },
  47. { LogType.Error, Color.red },
  48. { LogType.Exception, Color.red },
  49. { LogType.Log, Color.white },
  50. { LogType.Warning, Color.yellow },
  51. };
  52. const string windowTitle = "Console";
  53. const int margin = 20;
  54. static readonly GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
  55. static readonly GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
  56. readonly Rect titleBarRect = new Rect(0, 0, 10000, 20);
  57. Rect windowRect = new Rect(margin, margin, Screen.width - (margin * 2), Screen.height/3 - (margin * 2));
  58. void OnEnable()
  59. {
  60. #if UNITY_5
  61. Application.logMessageReceived += HandleLog;
  62. #else
  63. Application.RegisterLogCallback(HandleLog);
  64. #endif
  65. }
  66. void OnDisable()
  67. {
  68. #if UNITY_5
  69. Application.logMessageReceived -= HandleLog;
  70. #else
  71. Application.RegisterLogCallback(null);
  72. #endif
  73. }
  74. void Update()
  75. {
  76. if (Input.GetKeyDown(toggleKey))
  77. {
  78. visible = !visible;
  79. }
  80. if (shakeToOpen && Input.acceleration.sqrMagnitude > shakeAcceleration)
  81. {
  82. visible = true;
  83. }
  84. }
  85. void OnGUI()
  86. {
  87. //i(!visible)
  88. //{ // return;
  89. //}
  90. windowRect = GUILayout.Window(123456, windowRect, DrawConsoleWindow, windowTitle);
  91. }
  92. /// <summary>
  93. /// Displays a window that lists the recorded logs.
  94. /// </summary>
  95. /// <param name="windowID">Window ID.</param>
  96. void DrawConsoleWindow(int windowID)
  97. {
  98. DrawLogsList();
  99. DrawToolbar();
  100. // Allow the window to be dragged by its title bar.
  101. GUI.DragWindow(titleBarRect);
  102. }
  103. /// <summary>
  104. /// Displays a scrollable list of logs.
  105. /// </summary>
  106. void DrawLogsList()
  107. {
  108. scrollPosition = GUILayout.BeginScrollView(scrollPosition);
  109. // Iterate through the recorded logs.
  110. for (var i = 0; i < logs.Count; i++)
  111. {
  112. var log = logs[i];
  113. // Combine identical messages if collapse option is chosen.
  114. if (collapse && i > 0)
  115. {
  116. var previousMessage = logs[i - 1].message;
  117. if (log.message == previousMessage)
  118. {
  119. continue;
  120. }
  121. }
  122. GUI.contentColor = logTypeColors[log.type];
  123. GUILayout.Label(log.message);
  124. }
  125. GUILayout.EndScrollView();
  126. // Ensure GUI colour is reset before drawing other components.
  127. GUI.contentColor = Color.white;
  128. }
  129. /// <summary>
  130. /// Displays options for filtering and changing the logs list.
  131. /// </summary>
  132. void DrawToolbar()
  133. {
  134. GUILayout.BeginHorizontal();
  135. if (GUILayout.Button(clearLabel))
  136. {
  137. logs.Clear();
  138. }
  139. collapse = GUILayout.Toggle(collapse, collapseLabel, GUILayout.ExpandWidth(false));
  140. GUILayout.EndHorizontal();
  141. }
  142. /// <summary>
  143. /// Records a log from the log callback.
  144. /// </summary>
  145. /// <param name="message">Message.</param>
  146. /// <param name="stackTrace">Trace of where the message came from.</param>
  147. /// <param name="type">Type of message (error, exception, warning, assert).</param>
  148. void HandleLog(string message, string stackTrace, LogType type)
  149. {
  150. logs.Add(new Log
  151. {
  152. message = message,
  153. stackTrace = stackTrace,
  154. type = type,
  155. });
  156. TrimExcessLogs();
  157. }
  158. /// <summary>
  159. /// Removes old logs that exceed the maximum number allowed.
  160. /// </summary>
  161. void TrimExcessLogs()
  162. {
  163. if (!restrictLogCount)
  164. {
  165. return;
  166. }
  167. var amountToRemove = Mathf.Max(logs.Count - maxLogs, 0);
  168. if (amountToRemove == 0)
  169. {
  170. return;
  171. }
  172. logs.RemoveRange(0, amountToRemove);
  173. }
  174. }