LogController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using FairyGUI;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UI.Common;
  6. using System;
  7. namespace GFGGame
  8. {
  9. public class LogController : SingletonBase<LogController>
  10. {
  11. public const string _all = "All";
  12. public const string _error = "Error";
  13. public const string _log = "Log";
  14. public const int _maxCount = 100;
  15. public Dictionary<string, List<string>> msgDic = new Dictionary<string, List<string>>();
  16. private UI_BtnLog _btnLog;
  17. public void Init()
  18. {
  19. msgDic.Add(_all, new List<string>());
  20. msgDic.Add(_error, new List<string>());
  21. msgDic.Add(_log, new List<string>());
  22. this.CreatBtnLog();
  23. this.AddMsgListener();
  24. }
  25. public void AddMsgListener()
  26. {
  27. Application.logMessageReceived += (condition, stackTrace, type) =>
  28. {
  29. if (type == LogType.Error || type == LogType.Exception || type == LogType.Log)
  30. {
  31. this.AddMsgToDic(condition, stackTrace, type);
  32. }
  33. };
  34. }
  35. private void AddMsgToDic(string condition, string stackTrace, LogType type)
  36. {
  37. string msg = "";
  38. DateTime now = DateTime.Now;
  39. string time = string.Format("[{0}.{1} {2}:{3}:{4}]", now.Month, now.Day, now.Hour, now.Minute, now.Second);
  40. switch (type)
  41. {
  42. case LogType.Error:
  43. case LogType.Exception:
  44. if (msgDic[_all].Count == _maxCount)
  45. {
  46. msgDic[_all].RemoveAt(0);
  47. }
  48. if (msgDic[_error].Count == _maxCount)
  49. {
  50. msgDic[_error].RemoveAt(0);
  51. }
  52. msg = string.Format("error@{2}\n{0}\n{1}", condition, stackTrace, time);
  53. msgDic[_all].Add(msg);
  54. msgDic[_error].Add(msg);
  55. _btnLog.m_c1.selectedIndex = 1;
  56. _btnLog.target.alpha = 1;
  57. this.SetBtnLogVisable(true);
  58. break;
  59. case LogType.Log:
  60. if (msgDic[_all].Count == _maxCount)
  61. {
  62. msgDic[_all].RemoveAt(0);
  63. }
  64. if (msgDic[_log].Count == _maxCount)
  65. {
  66. msgDic[_log].RemoveAt(0);
  67. }
  68. msg = string.Format("log@{2}\n{0}\n{1}", condition, stackTrace, time);
  69. msgDic[_all].Add(msg);
  70. msgDic[_log].Add(msg);
  71. break;
  72. }
  73. }
  74. private void CreatBtnLog()
  75. {
  76. if (_btnLog == null)
  77. {
  78. _btnLog = UI_BtnLog.Create();
  79. ViewManager.AddChildToTopLayer(_btnLog.target);
  80. this.SetBtnLogVisable(false);
  81. _btnLog.target.x = 50;
  82. _btnLog.target.y = 1880;
  83. _btnLog.target.onClick.Add(this.OnBtnLogClick);
  84. }
  85. }
  86. private void OnBtnLogClick()
  87. {
  88. ViewManager.Show(ViewName.LOG_VIEW);
  89. _btnLog.m_c1.selectedIndex = 0;
  90. _btnLog.target.alpha = 0;
  91. }
  92. public void SetBtnLogVisable(bool visable)
  93. {
  94. _btnLog.target.visible = visable;
  95. }
  96. }
  97. }