暫無描述
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace XCharts.Runtime
  6. {
  7. [Serializable]
  8. public class DebugInfo
  9. {
  10. #pragma warning disable 0414
  11. [SerializeField] private bool m_Show = true;
  12. #pragma warning restore 0414
  13. [SerializeField] private bool m_ShowDebugInfo = false;
  14. [SerializeField] protected bool m_ShowAllChartObject = false;
  15. [SerializeField] protected bool m_FoldSeries = false;
  16. [SerializeField]
  17. private LabelStyle m_LabelStyle = new LabelStyle()
  18. {
  19. background = new ImageStyle()
  20. {
  21. color = new Color32(32, 32, 32, 170)
  22. },
  23. textStyle = new TextStyle()
  24. {
  25. fontSize = 18,
  26. color = Color.white
  27. }
  28. };
  29. private static StringBuilder s_Sb = new StringBuilder();
  30. private static readonly float INTERVAL = 0.2f;
  31. private static readonly float MAXCACHE = 20;
  32. private int m_FrameCount = 0;
  33. private float m_LastTime = 0f;
  34. private float m_LastCheckShowTime = 0f;
  35. private int m_LastRefreshCount = 0;
  36. private BaseChart m_Chart;
  37. private ChartLabel m_Label;
  38. private List<float> m_FpsList = new List<float>();
  39. /// <summary>
  40. /// Whether show debug component.
  41. /// |是否显示Debug组件。
  42. /// </summary>
  43. public bool show { get { return m_Show; } set { m_Show = value; } }
  44. /// <summary>
  45. /// Whether show children components of chart in hierarchy view.
  46. /// |是否在Hierarchy试图显示所有chart下的节点。
  47. /// </summary>
  48. public bool showAllChartObject { get { return m_ShowAllChartObject; } set { m_ShowAllChartObject = value; } }
  49. /// <summary>
  50. /// Whether to fold series in inspector view.
  51. /// |是否在Inspector上折叠Serie。
  52. /// </summary>
  53. public bool foldSeries { get { return m_FoldSeries; } set { m_FoldSeries = value; } }
  54. /// <summary>
  55. /// frame rate.
  56. /// |当前帧率。
  57. /// </summary>
  58. public float fps { get; private set; }
  59. /// <summary>
  60. /// The average frame rate.
  61. /// |平均帧率。
  62. /// </summary>
  63. public float avgFps { get; private set; }
  64. /// <summary>
  65. /// The fefresh count of chart per second.
  66. /// |图表每秒刷新次数。
  67. /// </summary>
  68. public int refreshCount { get; internal set; }
  69. internal int clickChartCount { get; set; }
  70. public void Init(BaseChart chart)
  71. {
  72. m_Chart = chart;
  73. m_Label = AddDebugInfoObject("debug", chart.transform, m_LabelStyle, chart.theme);
  74. }
  75. public void Update()
  76. {
  77. if (clickChartCount > 2)
  78. {
  79. m_ShowDebugInfo = !m_ShowDebugInfo;
  80. ChartHelper.SetActive(m_Label.transform, m_ShowDebugInfo);
  81. clickChartCount = 0;
  82. m_LastCheckShowTime = Time.realtimeSinceStartup;
  83. return;
  84. }
  85. if (Time.realtimeSinceStartup - m_LastCheckShowTime > 0.5f)
  86. {
  87. m_LastCheckShowTime = Time.realtimeSinceStartup;
  88. clickChartCount = 0;
  89. }
  90. if (!m_ShowDebugInfo || m_Label == null)
  91. return;
  92. m_FrameCount++;
  93. if (Time.realtimeSinceStartup - m_LastTime >= INTERVAL)
  94. {
  95. fps = m_FrameCount / (Time.realtimeSinceStartup - m_LastTime);
  96. m_FrameCount = 0;
  97. m_LastTime = Time.realtimeSinceStartup;
  98. if (m_LastRefreshCount == refreshCount)
  99. {
  100. m_LastRefreshCount = 0;
  101. refreshCount = 0;
  102. }
  103. m_LastRefreshCount = refreshCount;
  104. if (m_FpsList.Count > MAXCACHE)
  105. {
  106. m_FpsList.RemoveAt(0);
  107. }
  108. m_FpsList.Add(fps);
  109. avgFps = GetAvg(m_FpsList);
  110. if (m_Label != null)
  111. {
  112. s_Sb.Length = 0;
  113. s_Sb.AppendFormat("v{0}\n", XChartsMgr.version);
  114. s_Sb.AppendFormat("fps : {0:f0} / {1:f0}\n", fps, avgFps);
  115. s_Sb.AppendFormat("draw : {0}\n", refreshCount);
  116. var dataCount = m_Chart.GetAllSerieDataCount();
  117. SetValueWithKInfo(s_Sb, "data", dataCount);
  118. var vertCount = 0;
  119. foreach (var serie in m_Chart.series)
  120. vertCount += serie.context.vertCount;
  121. SetValueWithKInfo(s_Sb, "b-vert", m_Chart.m_BasePainterVertCount);
  122. SetValueWithKInfo(s_Sb, "s-vert", vertCount);
  123. SetValueWithKInfo(s_Sb, "t-vert", m_Chart.m_TopPainterVertCount, false);
  124. m_Label.SetText(s_Sb.ToString());
  125. }
  126. }
  127. }
  128. private static void SetValueWithKInfo(StringBuilder s_Sb, string key, int value, bool newLine = true)
  129. {
  130. if (value >= 1000)
  131. s_Sb.AppendFormat("{0} : {1:f1}k", key, value * 0.001f);
  132. else
  133. s_Sb.AppendFormat("{0} : {1}", key, value);
  134. if (newLine)
  135. s_Sb.Append("\n");
  136. }
  137. private static float GetAvg(List<float> list)
  138. {
  139. var total = 0f;
  140. foreach (var v in list) total += v;
  141. return total / list.Count;
  142. }
  143. private ChartLabel AddDebugInfoObject(string name, Transform parent, LabelStyle labelStyle,
  144. ThemeStyle theme)
  145. {
  146. var anchorMax = new Vector2(0, 1);
  147. var anchorMin = new Vector2(0, 1);
  148. var pivot = new Vector2(0, 1);
  149. var sizeDelta = new Vector2(100, 100);
  150. var labelGameObject = ChartHelper.AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
  151. labelGameObject.transform.SetAsLastSibling();
  152. labelGameObject.hideFlags = m_Chart.chartHideFlags;
  153. ChartHelper.SetActive(labelGameObject, m_ShowDebugInfo);
  154. var label = ChartHelper.AddChartLabel("info", labelGameObject.transform, labelStyle, theme.common,
  155. "", Color.clear, TextAnchor.UpperLeft);
  156. label.SetActive(labelStyle.show);
  157. return label;
  158. }
  159. }
  160. }