Files
XCharts/Runtime/Component/Debug/DebugInfo.cs

176 lines
6.5 KiB
C#
Raw Normal View History

2021-11-23 13:20:07 +08:00
using System;
using System.Collections.Generic;
using System.Text;
2022-05-22 22:17:38 +08:00
using UnityEngine;
2021-11-23 13:20:07 +08:00
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
2021-11-23 13:20:07 +08:00
{
[Serializable]
public class DebugInfo
{
2022-04-26 08:24:45 +08:00
#pragma warning disable 0414
2022-03-09 07:26:15 +08:00
[SerializeField] private bool m_Show = true;
2022-04-26 08:24:45 +08:00
#pragma warning restore 0414
2022-02-25 08:10:09 +08:00
[SerializeField] private bool m_ShowDebugInfo = false;
2022-03-04 22:17:32 +08:00
[SerializeField] protected bool m_ShowAllChartObject = false;
2022-02-25 08:10:09 +08:00
[SerializeField] protected bool m_FoldSeries = false;
2021-11-23 13:20:07 +08:00
[SerializeField]
2022-04-26 08:24:45 +08:00
private LabelStyle m_LabelStyle = new LabelStyle()
2021-11-23 13:20:07 +08:00
{
2022-04-26 08:24:45 +08:00
background = new ImageStyle()
{
2022-05-22 22:17:38 +08:00
color = new Color32(32, 32, 32, 170)
2022-04-26 08:24:45 +08:00
},
textStyle = new TextStyle()
{
2022-05-22 22:17:38 +08:00
fontSize = 18,
color = Color.white
2022-04-26 08:24:45 +08:00
}
2021-11-23 13:20:07 +08:00
};
private static StringBuilder s_Sb = new StringBuilder();
private static readonly float INTERVAL = 0.2f;
private static readonly float MAXCACHE = 20;
private int m_FrameCount = 0;
private float m_LastTime = 0f;
2021-12-08 08:31:32 +08:00
private float m_LastCheckShowTime = 0f;
2021-11-23 13:20:07 +08:00
private int m_LastRefreshCount = 0;
private BaseChart m_Chart;
private ChartLabel m_Label;
private List<float> m_FpsList = new List<float>();
2022-07-04 08:30:43 +08:00
/// <summary>
/// Whether show debug component.
2023-11-11 23:32:24 +08:00
/// ||是否显示Debug组件。
2022-07-04 08:30:43 +08:00
/// </summary>
public bool show { get { return m_Show; } set { m_Show = value; } }
/// <summary>
/// Whether show children components of chart in hierarchy view.
2023-11-11 23:32:24 +08:00
/// ||是否在Hierarchy试图显示所有chart下的节点。
2022-07-04 08:30:43 +08:00
/// </summary>
2022-05-05 13:10:04 +08:00
public bool showAllChartObject { get { return m_ShowAllChartObject; } set { m_ShowAllChartObject = value; } }
2022-07-04 08:30:43 +08:00
/// <summary>
/// Whether to fold series in inspector view.
2023-11-11 23:32:24 +08:00
/// ||是否在Inspector上折叠Serie。
2022-07-04 08:30:43 +08:00
/// </summary>
2022-02-25 08:10:09 +08:00
public bool foldSeries { get { return m_FoldSeries; } set { m_FoldSeries = value; } }
2022-07-04 08:30:43 +08:00
/// <summary>
/// frame rate.
2023-11-11 23:32:24 +08:00
/// ||当前帧率。
2022-07-04 08:30:43 +08:00
/// </summary>
2021-11-23 13:20:07 +08:00
public float fps { get; private set; }
2022-07-04 08:30:43 +08:00
/// <summary>
/// The average frame rate.
2023-11-11 23:32:24 +08:00
/// ||平均帧率。
2022-07-04 08:30:43 +08:00
/// </summary>
2021-11-23 13:20:07 +08:00
public float avgFps { get; private set; }
2022-07-04 08:30:43 +08:00
/// <summary>
/// The fefresh count of chart per second.
2023-11-11 23:32:24 +08:00
/// ||图表每秒刷新次数。
2022-07-04 08:30:43 +08:00
/// </summary>
2021-11-23 13:20:07 +08:00
public int refreshCount { get; internal set; }
2021-12-08 08:31:32 +08:00
internal int clickChartCount { get; set; }
2021-11-23 13:20:07 +08:00
public void Init(BaseChart chart)
{
m_Chart = chart;
m_Label = AddDebugInfoObject("debug", chart.transform, m_LabelStyle, chart.theme, chart.childrenNodeNames);
2021-11-23 13:20:07 +08:00
}
public void Update()
{
2022-05-22 22:17:38 +08:00
if (clickChartCount > 2)
2021-12-08 08:31:32 +08:00
{
2022-02-25 08:10:09 +08:00
m_ShowDebugInfo = !m_ShowDebugInfo;
ChartHelper.SetActive(m_Label.transform, m_ShowDebugInfo);
2021-12-08 08:31:32 +08:00
clickChartCount = 0;
m_LastCheckShowTime = Time.realtimeSinceStartup;
return;
}
if (Time.realtimeSinceStartup - m_LastCheckShowTime > 0.5f)
{
m_LastCheckShowTime = Time.realtimeSinceStartup;
clickChartCount = 0;
}
2022-02-25 08:10:09 +08:00
if (!m_ShowDebugInfo || m_Label == null)
2021-12-08 08:31:32 +08:00
return;
2021-11-23 13:20:07 +08:00
m_FrameCount++;
if (Time.realtimeSinceStartup - m_LastTime >= INTERVAL)
{
fps = m_FrameCount / (Time.realtimeSinceStartup - m_LastTime);
m_FrameCount = 0;
m_LastTime = Time.realtimeSinceStartup;
if (m_LastRefreshCount == refreshCount)
{
m_LastRefreshCount = 0;
refreshCount = 0;
}
m_LastRefreshCount = refreshCount;
if (m_FpsList.Count > MAXCACHE)
{
m_FpsList.RemoveAt(0);
}
m_FpsList.Add(fps);
avgFps = GetAvg(m_FpsList);
if (m_Label != null)
{
s_Sb.Length = 0;
2021-12-08 08:31:32 +08:00
s_Sb.AppendFormat("v{0}\n", XChartsMgr.version);
2021-11-23 13:20:07 +08:00
s_Sb.AppendFormat("fps : {0:f0} / {1:f0}\n", fps, avgFps);
2021-12-08 08:31:32 +08:00
s_Sb.AppendFormat("draw : {0}\n", refreshCount);
var dataCount = m_Chart.GetAllSerieDataCount();
SetValueWithKInfo(s_Sb, "data", dataCount);
var vertCount = 0;
foreach (var serie in m_Chart.series)
vertCount += serie.context.vertCount;
SetValueWithKInfo(s_Sb, "b-vert", m_Chart.m_BasePainterVertCount);
SetValueWithKInfo(s_Sb, "s-vert", vertCount);
SetValueWithKInfo(s_Sb, "t-vert", m_Chart.m_TopPainterVertCount, false);
2021-11-23 13:20:07 +08:00
m_Label.SetText(s_Sb.ToString());
}
}
}
2021-12-08 08:31:32 +08:00
private static void SetValueWithKInfo(StringBuilder s_Sb, string key, int value, bool newLine = true)
{
if (value >= 1000)
s_Sb.AppendFormat("{0} : {1:f1}k", key, value * 0.001f);
else
s_Sb.AppendFormat("{0} : {1}", key, value);
if (newLine)
s_Sb.Append("\n");
}
private static float GetAvg(List<float> list)
2021-11-23 13:20:07 +08:00
{
var total = 0f;
foreach (var v in list) total += v;
return total / list.Count;
}
2022-04-26 08:24:45 +08:00
private ChartLabel AddDebugInfoObject(string name, Transform parent, LabelStyle labelStyle,
ThemeStyle theme, List<string> childrenNodeNames)
2021-11-23 13:20:07 +08:00
{
var anchorMax = new Vector2(0, 1);
var anchorMin = new Vector2(0, 1);
var pivot = new Vector2(0, 1);
var sizeDelta = new Vector2(100, 100);
var labelGameObject = ChartHelper.AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta, -1, childrenNodeNames);
2021-12-08 08:31:32 +08:00
labelGameObject.transform.SetAsLastSibling();
2021-11-23 13:20:07 +08:00
labelGameObject.hideFlags = m_Chart.chartHideFlags;
2022-02-25 08:10:09 +08:00
ChartHelper.SetActive(labelGameObject, m_ShowDebugInfo);
2021-12-08 08:31:32 +08:00
2022-04-26 08:24:45 +08:00
var label = ChartHelper.AddChartLabel("info", labelGameObject.transform, labelStyle, theme.common,
2022-05-22 22:17:38 +08:00
"", Color.clear, TextAnchor.UpperLeft);
2022-04-26 08:24:45 +08:00
label.SetActive(labelStyle.show);
2021-11-23 13:20:07 +08:00
return label;
}
}
}