Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.6 KiB

  1. // Console Viewer Canvas|Prefabs|0020
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Collections.Generic;
  7. using System.Text.RegularExpressions;
  8. using System.Linq;
  9. /// <summary>
  10. /// Adds an in-scene representation of the Unity console on a world space canvas.
  11. /// </summary>
  12. /// <remarks>
  13. /// **Prefab Usage:**
  14. /// * Place the `VRTK/Prefabs/ConsoleViewerCanvas/ConsoleViewerCanvas` prefab into the scene hierarchy.
  15. ///
  16. /// > It is also possible to interact with the `ConsoleViewerCanvas` with a `VRTK_UIPointer`.
  17. /// </remarks>
  18. public class VRTK_ConsoleViewer : MonoBehaviour
  19. {
  20. [Tooltip("The size of the font the log text is displayed in.")]
  21. public int fontSize = 14;
  22. [Tooltip("The colour of the text for an info log message.")]
  23. public Color infoMessage = Color.black;
  24. [Tooltip("The colour of the text for an assertion log message.")]
  25. public Color assertMessage = Color.black;
  26. [Tooltip("The colour of the text for a warning log message.")]
  27. public Color warningMessage = Color.yellow;
  28. [Tooltip("The colour of the text for an error log message.")]
  29. public Color errorMessage = Color.red;
  30. [Tooltip("The colour of the text for an exception log message.")]
  31. public Color exceptionMessage = Color.red;
  32. protected Dictionary<LogType, Color> logTypeColors;
  33. protected ScrollRect scrollWindow;
  34. protected RectTransform consoleRect;
  35. protected Text consoleOutput;
  36. protected const string NEWLINE = "\n";
  37. protected int lineBuffer = 50;
  38. protected int currentBuffer;
  39. protected string lastMessage;
  40. protected bool collapseLog = false;
  41. /// <summary>
  42. /// The SetCollapse method determines whether the console will collapse same message output into the same line. A state of `true` will collapse messages and `false` will print the same message for each line.
  43. /// </summary>
  44. /// <param name="state">The state of whether to collapse the output messages, true will collapse and false will not collapse.</param>
  45. public virtual void SetCollapse(bool state)
  46. {
  47. collapseLog = state;
  48. }
  49. /// <summary>
  50. /// The ClearLog method clears the current log view of all messages
  51. /// </summary>
  52. public virtual void ClearLog()
  53. {
  54. consoleOutput.text = "";
  55. currentBuffer = 0;
  56. lastMessage = "";
  57. }
  58. protected virtual void Awake()
  59. {
  60. logTypeColors = new Dictionary<LogType, Color>()
  61. {
  62. { LogType.Assert, assertMessage },
  63. { LogType.Error, errorMessage },
  64. { LogType.Exception, exceptionMessage },
  65. { LogType.Log, infoMessage },
  66. { LogType.Warning, warningMessage }
  67. };
  68. scrollWindow = transform.Find("Panel/Scroll View").GetComponent<ScrollRect>();
  69. consoleRect = transform.Find("Panel/Scroll View/Viewport/Content").GetComponent<RectTransform>();
  70. consoleOutput = transform.Find("Panel/Scroll View/Viewport/Content/ConsoleOutput").GetComponent<Text>();
  71. consoleOutput.fontSize = fontSize;
  72. ClearLog();
  73. }
  74. protected virtual void OnEnable()
  75. {
  76. Application.logMessageReceived += HandleLog;
  77. }
  78. protected virtual void OnDisable()
  79. {
  80. Application.logMessageReceived -= HandleLog;
  81. consoleRect.sizeDelta = Vector2.zero;
  82. }
  83. protected virtual string GetMessage(string message, LogType type)
  84. {
  85. string color = ColorUtility.ToHtmlStringRGBA(logTypeColors[type]);
  86. return "<color=#" + color + ">" + message + "</color>" + NEWLINE;
  87. }
  88. protected virtual void HandleLog(string message, string stackTrace, LogType type)
  89. {
  90. string logOutput = GetMessage(message, type);
  91. if (!collapseLog || lastMessage != logOutput)
  92. {
  93. consoleOutput.text += logOutput;
  94. lastMessage = logOutput;
  95. }
  96. consoleRect.sizeDelta = new Vector2(consoleOutput.preferredWidth, consoleOutput.preferredHeight);
  97. scrollWindow.verticalNormalizedPosition = 0;
  98. currentBuffer++;
  99. if (currentBuffer >= lineBuffer)
  100. {
  101. IEnumerable<string> lines = Regex.Split(consoleOutput.text, NEWLINE).Skip(lineBuffer / 2);
  102. consoleOutput.text = string.Join(NEWLINE, lines.ToArray());
  103. currentBuffer = lineBuffer / 2;
  104. }
  105. }
  106. }
  107. }