DebugConsole.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. using TMPro;
  12. using UnityEngine;
  13. public class DebugConsole : MonoBehaviour
  14. {
  15. [SerializeField] bool m_ShowOnAppStart;
  16. [SerializeField] private TextMeshProUGUI m_DebugLogText;
  17. [SerializeField] private GameObject m_ContentParent;
  18. [SerializeField] private int m_DebugLogLineMaxCount = 100;
  19. private List<string> m_DebugLogLines = new List<string>();
  20. private StringBuilder m_stringBuilder = new StringBuilder();
  21. private static DebugConsole m_instance = null;
  22. public static DebugConsole Instance
  23. {
  24. get
  25. {
  26. #if UNITY_EDITOR
  27. if (m_instance == null && !Application.isPlaying)
  28. {
  29. m_instance = FindObjectOfType<DebugConsole>();
  30. }
  31. #endif
  32. if (m_instance == null)
  33. {
  34. Debug.LogError("No DebugConsole instance found. Ensure one exists in the scene.");
  35. }
  36. return m_instance;
  37. }
  38. }
  39. void Awake()
  40. {
  41. if (m_instance == null)
  42. {
  43. m_instance = this;
  44. }
  45. if (m_instance != this)
  46. {
  47. Debug.LogError("There must be only one DebugConsole object in a scene.");
  48. DestroyImmediate(this);
  49. }
  50. }
  51. void Start()
  52. {
  53. m_ContentParent.SetActive(m_ShowOnAppStart);
  54. }
  55. void OnEnable()
  56. {
  57. Application.logMessageReceived += HandleLog;
  58. }
  59. void OnDisable()
  60. {
  61. Application.logMessageReceived -= HandleLog;
  62. }
  63. public bool isShown => m_ContentParent.activeSelf;
  64. public void Show(bool show)
  65. {
  66. if (m_ContentParent.activeSelf == show) return;
  67. m_ContentParent.SetActive(show);
  68. Debug.Log("Debug console "+(show?"enabled":"disabled"));
  69. }
  70. public void ToggleShow()
  71. {
  72. Show(!m_ContentParent.activeSelf);
  73. }
  74. void HandleLog(string logString, string stackTrace, LogType type)
  75. {
  76. switch (type)
  77. {
  78. case LogType.Warning:
  79. logString = "<color=orange>" + logString + "</color>";
  80. break;
  81. case LogType.Error:
  82. case LogType.Exception:
  83. logString = "<color=red>" + logString + Environment.NewLine + "STACK TRACE: " + stackTrace + "</color>";
  84. break;
  85. }
  86. AddText(logString);
  87. }
  88. private void AddText(string text)
  89. {
  90. if (m_DebugLogLines.Count >= m_DebugLogLineMaxCount)
  91. {
  92. m_DebugLogLines.RemoveAt(0);
  93. }
  94. m_DebugLogLines.Add("[" + Time.realtimeSinceStartup.ToString("0.000") + "] " + text);
  95. m_DebugLogText.text = ConstructLogText();
  96. }
  97. private string ConstructLogText()
  98. {
  99. m_stringBuilder.Clear();
  100. for (int i = m_DebugLogLines.Count - 1; i >= 0; i--)
  101. {
  102. m_stringBuilder.AppendLine(m_DebugLogLines[i]);
  103. }
  104. return m_stringBuilder.ToString();
  105. }
  106. }