Unity 框架

Console.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using TFramework;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System;
  7. namespace TFramework
  8. {
  9. public class Console : BaseComponent
  10. {
  11. public Toggle m_infoTog;
  12. public Text m_infoCountText;
  13. public Toggle m_warningTog;
  14. public Text m_warningCountText;
  15. public Toggle m_errorTog;
  16. public Text m_errorCountText;
  17. public Button m_clearBtn;
  18. public Toggle m_logItem;
  19. private List<Toggle> m_logItems = new List<Toggle>();
  20. public InputField m_logInfoText;
  21. public Text m_logInfoContent;
  22. private List<ConsoleLog> _infoLogs = new List<ConsoleLog>();
  23. private List<ConsoleLog> _errorLogs = new List<ConsoleLog>();
  24. private List<ConsoleLog> _warningLogs = new List<ConsoleLog>();
  25. private List<ConsoleLog> _logBuffers = new List<ConsoleLog>();
  26. string msg;
  27. bool _isDestroy = false;
  28. // Start is called before the first frame update
  29. void Start()
  30. {
  31. //m_logInfoText.onValueChanged.AddListener(st =>
  32. //{
  33. // if (st != msg)
  34. // m_logInfoText.text = msg;
  35. //});
  36. }
  37. public override void Init()
  38. {
  39. base.Init();
  40. Clear();
  41. Application.logMessageReceived += OnLogMessageReceived;
  42. m_clearBtn.onClick.AddListener(Clear);
  43. m_infoTog.onValueChanged.AddListener(isOn => Refresh());
  44. m_warningTog.onValueChanged.AddListener(isOn => Refresh());
  45. m_errorTog.onValueChanged.AddListener(isOn => Refresh());
  46. Main.Instance.Delay(0.2f, () =>
  47. {
  48. m_infoTog.isOn = true;
  49. m_warningTog.isOn = true;
  50. m_errorTog.isOn = true;
  51. });
  52. }
  53. private void Refresh()
  54. {
  55. m_logItems.ForEach(i => i.SetActive(false));
  56. msg = string.Empty;
  57. m_logInfoText.text = string.Empty;
  58. _logBuffers.Clear();
  59. if(m_infoTog.isOn)
  60. _logBuffers.AddRange(_infoLogs);
  61. if(m_warningTog.isOn)
  62. _logBuffers.AddRange(_warningLogs);
  63. if( m_errorTog.isOn)
  64. _logBuffers.AddRange(_errorLogs);
  65. _logBuffers.Sort((a,b)=>a.Time.CompareTo(b.Time));
  66. for (int i = 0; i < _logBuffers.Count; i++)
  67. {
  68. if (m_logItems.Count <= i)
  69. {
  70. m_logItems.Add(m_logItem.Clone(m_logItem.transform.parent));
  71. }
  72. int index = i;
  73. m_logItems[i].gameObject.SetActive(true);
  74. m_logItems[i].GetChildComponent<Text>("Label").text = $"<color={(_logBuffers[i].Type == LogType.Log ? "white" : (_logBuffers[i].Type==LogType.Warning?"yellow":"red"))}>{_logBuffers[i].Name}</color>";
  75. m_logItems[i].onValueChanged.RemoveAllListeners();
  76. m_logItems[i].onValueChanged.AddListener (isOn =>
  77. {
  78. if (isOn)
  79. {
  80. msg = $"<color={(_logBuffers[index].Type == LogType.Log ? "white" : (_logBuffers[index].Type == LogType.Warning ? "yellow" : "red"))}>{_logBuffers[index].Message}</color>\n{_logBuffers[index].StackTrace}";
  81. m_logInfoContent.text = $"{_logBuffers[index].Message}\n{_logBuffers[index].StackTrace}";
  82. m_logInfoText.text = msg;
  83. }
  84. });
  85. }
  86. }
  87. private void OnLogMessageReceived(string condition, string stackTrace, LogType type)
  88. {
  89. ConsoleLog log = new ConsoleLog();
  90. log.Time = DateTime.Now;
  91. log.Message = condition;
  92. log.StackTrace = stackTrace;
  93. if (type == LogType.Assert)
  94. {
  95. log.Type = LogType.Error;
  96. _errorLogs.Add(log);
  97. }
  98. else if (type == LogType.Exception || type == LogType.Error)
  99. {
  100. log.Type = LogType.Error;
  101. _errorLogs.Add(log);
  102. }
  103. else if (type == LogType.Warning)
  104. {
  105. log.Type = LogType.Warning;
  106. _warningLogs.Add(log);
  107. }
  108. else if (type == LogType.Log)
  109. {
  110. log.Type = LogType.Log;
  111. _infoLogs.Add(log);
  112. }
  113. log.Name = string.Format("[{0}] [{1}] {2}", log.Type, log.Time.ToString("HH:mm:ss"), log.Message);
  114. if (_isDestroy||m_errorCountText==null||m_infoCountText==null|m_warningCountText==null) return;
  115. m_errorCountText.text = _errorLogs.Count < 99 ? _errorLogs.Count.ToString() : "99+";
  116. m_infoCountText.text = _infoLogs.Count < 99 ? _infoLogs.Count.ToString() : "99+";
  117. m_warningCountText.text = _warningLogs.Count < 99 ? _warningLogs.Count.ToString() : "99+";
  118. Refresh();
  119. }
  120. private void Clear()
  121. {
  122. _infoLogs.Clear();
  123. _warningLogs.Clear();
  124. _errorLogs.Clear();
  125. m_errorCountText.text = string.Empty;
  126. m_infoCountText.text = string.Empty;
  127. m_warningCountText.text = string.Empty;
  128. m_logItems.ForEach(i=>i.SetActive(false));
  129. m_logInfoText.text = string.Empty;
  130. m_infoCountText.text = string.Empty;
  131. }
  132. private void OnDestroy()
  133. {
  134. _isDestroy = true;
  135. Application.logMessageReceived -= OnLogMessageReceived;
  136. }
  137. }
  138. }