console.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. public class Console : MonoBehaviour
  5. {
  6. public static readonly Version version = new Version(1, 0);
  7. struct ConsoleMessage
  8. {
  9. public readonly string message;
  10. public readonly string stackTrace;
  11. public readonly LogType type;
  12. public ConsoleMessage(string message, string stackTrace, LogType type)
  13. {
  14. this.message = message;
  15. this.stackTrace = stackTrace;
  16. this.type = type;
  17. }
  18. }
  19. public KeyCode toggleKey = KeyCode.BackQuote;
  20. List<ConsoleMessage> entries = new List<ConsoleMessage>();
  21. Vector2 scrollPos;
  22. bool show;
  23. bool collapse;
  24. // Visual elements:
  25. const int margin = 20;
  26. Rect windowRect = new Rect(margin, margin, Screen.width - (2 * margin), Screen.height - (2 * margin));
  27. GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
  28. GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
  29. void OnEnable() { Application.RegisterLogCallback(HandleLog); }
  30. void OnDisable() { Application.RegisterLogCallback(null); }
  31. void Update()
  32. {
  33. /* if (Input.GetKeyDown(toggleKey))
  34. {*/
  35. show = true/*!show*/;
  36. // }
  37. }
  38. void OnGUI()
  39. {
  40. if (!show)
  41. {
  42. return;
  43. }
  44. windowRect = GUILayout.Window(123456, windowRect, ConsoleWindow, "Console");
  45. }
  46. /// <summary>
  47. /// A window displaying the logged messages.
  48. /// </summary>
  49. /// <param name="windowID">The window's ID.</param>
  50. void ConsoleWindow(int windowID)
  51. {
  52. scrollPos = GUILayout.BeginScrollView(scrollPos);
  53. // Go through each logged entry
  54. for (int i = 0; i < entries.Count; i++)
  55. {
  56. ConsoleMessage entry = entries[i];
  57. // If this message is the same as the last one and the collapse feature is chosen, skip it
  58. if (collapse && i > 0 && entry.message == entries[i - 1].message)
  59. {
  60. continue;
  61. }
  62. // Change the text colour according to the log type
  63. switch (entry.type)
  64. {
  65. case LogType.Error:
  66. case LogType.Exception:
  67. GUI.contentColor = Color.red;
  68. break;
  69. case LogType.Warning:
  70. GUI.contentColor = Color.yellow;
  71. break;
  72. default:
  73. GUI.contentColor = Color.white;
  74. break;
  75. }
  76. GUILayout.Label(entry.message);
  77. }
  78. GUI.contentColor = Color.white;
  79. GUILayout.EndScrollView();
  80. GUILayout.BeginHorizontal();
  81. // Clear button
  82. if (GUILayout.Button(clearLabel))
  83. {
  84. entries.Clear();
  85. }
  86. // Collapse toggle
  87. collapse = GUILayout.Toggle(collapse, collapseLabel, GUILayout.ExpandWidth(false));
  88. GUILayout.EndHorizontal();
  89. // Set the window to be draggable by the top title bar
  90. GUI.DragWindow(new Rect(0, 0, 10000, 20));
  91. }
  92. /// <summary>
  93. /// Logged messages are sent through this callback function.
  94. /// </summary>
  95. /// <param name="message">The message itself.</param>
  96. /// <param name="stackTrace">A trace of where the message came from.</param>
  97. /// <param name="type">The type of message: error/exception, warning, or assert.</param>
  98. void HandleLog(string message, string stackTrace, LogType type)
  99. {
  100. ConsoleMessage entry = new ConsoleMessage(message, stackTrace, type);
  101. entries.Add(entry);
  102. }
  103. }