add debug info

This commit is contained in:
monitor1394
2022-04-10 22:27:27 +08:00
parent 24be6df68e
commit 56d0b7ffb4
6 changed files with 195 additions and 3 deletions

View File

@@ -0,0 +1,154 @@
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine.UI;
namespace XCharts
{
[Serializable]
public class DebugInfo
{
private bool m_ShowDebugInfo = false;
private TextStyle m_DebugInfoTextStyle = new TextStyle()
{
fontSize = 18,
backgroundColor = new Color32(32, 32, 32, 170),
color = Color.white
};
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;
private float m_LastCheckShowTime = 0f;
private int m_LastRefreshCount = 0;
private BaseChart m_Chart;
private ChartLabel m_Label;
private List<float> m_FpsList = new List<float>();
public float fps { get; private set; }
public float avgFps { get; private set; }
public int refreshCount { get; internal set; }
internal int clickChartCount { get; set; }
public void Init(BaseChart chart)
{
m_Chart = chart;
m_Label = AddDebugInfoObject("debug", chart.transform, m_DebugInfoTextStyle, chart.theme);
}
public void Update()
{
if (clickChartCount >= 3)
{
m_ShowDebugInfo = !m_ShowDebugInfo;
ChartHelper.SetActive(m_Label.gameObject.transform, m_ShowDebugInfo);
clickChartCount = 0;
m_LastCheckShowTime = Time.realtimeSinceStartup;
return;
}
if (Time.realtimeSinceStartup - m_LastCheckShowTime > 0.5f)
{
m_LastCheckShowTime = Time.realtimeSinceStartup;
clickChartCount = 0;
}
if (!m_ShowDebugInfo || m_Label == null)
return;
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;
s_Sb.AppendFormat("v{0}\n", XChartsMgr.version);
s_Sb.AppendFormat("fps : {0:f0} / {1:f0}\n", fps, avgFps);
s_Sb.AppendFormat("draw : {0}\n", refreshCount);
var dataCount = m_Chart.series.GetAllSerieDataCount();
SetValueWithKInfo(s_Sb, "data", dataCount);
var vertCount = 0;
foreach (var serie in m_Chart.series.list)
vertCount += serie.runtimeVertCount;
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);
m_Label.SetText(s_Sb.ToString());
}
}
}
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)
{
var total = 0f;
foreach (var v in list) total += v;
return total / list.Count;
}
private ChartLabel AddDebugInfoObject(string name, Transform parent, TextStyle textStyle,
ChartTheme theme)
{
var anchorMax = new Vector2(0, 1);
var anchorMin = new Vector2(0, 1);
var pivot = new Vector2(0, 1);
var sizeDelta = new Vector2(130, 150);
var labelGameObject = ChartHelper.AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
labelGameObject.transform.SetAsLastSibling();
labelGameObject.hideFlags = m_Chart.chartHideFlags;
ChartHelper.SetActive(labelGameObject, m_ShowDebugInfo);
var image = ChartHelper.GetOrAddComponent<Image>(labelGameObject);
image.color = textStyle.backgroundColor;
var label = new ChartLabel();
label.gameObject = labelGameObject;
label.label = ChartHelper.AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax,
pivot, sizeDelta, textStyle, theme.common);
label.SetAutoSize(true);
label.label.SetActive(true);
label.label.SetAlignment(textStyle.GetAlignment(TextAnchor.UpperLeft));
label.label.SetLocalPosition(new Vector2(3, -3));
label.SetText("30");
label.SetLabelColor(textStyle.color);
return label;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9ff63fec5c2f94046b4560565057db8c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1190,6 +1190,7 @@ namespace XCharts
public float runtimeWidth { get; internal set; }
public float runtimeHeight { get; internal set; }
public Rect runtimeRect { get; internal set; }
public int runtimeVertCount { get; internal set; }
public List<SerieData> runtimeSortedData { get { return m_SortedData; } }
public List<SerieData> rootData { get { return m_RootData; } }
public bool nameDirty { get { return m_NameDirty; } }

View File

@@ -98,6 +98,16 @@ namespace XCharts
SeriesHelper.ClearNameDirty(this);
}
public int GetAllSerieDataCount()
{
var count = 0;
foreach (var serie in list)
{
count += serie.dataCount;
}
return count;
}
/// <summary>
/// 清空所有系列的数据
/// </summary>
@@ -259,7 +269,7 @@ namespace XCharts
/// </summary>
public void RemoveAll()
{
foreach(var serie in m_Series) serie.AnimationFadeIn();
foreach (var serie in m_Series) serie.AnimationFadeIn();
m_Series.Clear();
}

View File

@@ -80,6 +80,8 @@ namespace XCharts
internal bool m_ReinitTitle = false;
internal bool m_CheckAnimation = false;
internal bool m_IsPlayingAnimation = false;
internal int m_BasePainterVertCount;
internal int m_TopPainterVertCount;
internal protected List<string> m_LegendRealShowName = new List<string>();
protected List<Painter> m_PainterList = new List<Painter>();
internal Painter m_PainterTop;
@@ -109,6 +111,8 @@ namespace XCharts
m_ComponentHandlers.Add(new VisualMapHandler(this));
m_ComponentHandlers.Add(new DataZoomHandler(this));
foreach (var draw in m_ComponentHandlers) draw.Init();
m_DebugInfo.Init(this);
}
protected override void Awake()
@@ -161,6 +165,7 @@ namespace XCharts
Internal_CheckAnimation();
foreach (var draw in m_DrawSeries) draw.Update();
foreach (var draw in m_ComponentHandlers) draw.Update();
m_DebugInfo.Update();
}
public Painter GetPainter(int index)
@@ -791,6 +796,12 @@ namespace XCharts
{
}
public override void OnPointerClick(PointerEventData eventData)
{
m_DebugInfo.clickChartCount++;
base.OnPointerClick(eventData);
}
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
@@ -883,6 +894,7 @@ namespace XCharts
{
m_OnCustomDrawBaseCallback(vh);
}
m_BasePainterVertCount = vh.currentVertCount;
}
protected virtual void OnDrawPainterSerie(VertexHelper vh, Painter painter)
@@ -892,6 +904,7 @@ namespace XCharts
var maxSeries = m_Series.Count;
var rate = Mathf.CeilToInt(maxSeries * 1.0f / maxPainter);
m_PainterTop.Refresh();
m_DebugInfo.refreshCount++;
for (int i = painter.index * rate; i < (painter.index + 1) * rate && i < maxSeries; i++)
{
var serie = m_Series.GetSerie(i);
@@ -904,6 +917,7 @@ namespace XCharts
{
m_OnCustomDrawSerieAfterCallback(vh, serie);
}
serie.runtimeVertCount = vh.currentVertCount;
}
m_RefreshLabel = true;
}
@@ -919,6 +933,7 @@ namespace XCharts
m_OnCustomDrawTopCallback(vh);
}
DrawTooltip(vh);
m_TopPainterVertCount = vh.currentVertCount;
}
protected virtual void DrawPainterSerie(VertexHelper vh, Serie serie)

View File

@@ -22,6 +22,7 @@ namespace XCharts
[SerializeField] protected bool m_DebugMode = false;
[SerializeField] protected bool m_EnableTextMeshPro = false;
[SerializeField] protected Background m_Background = Background.defaultBackground;
protected DebugInfo m_DebugInfo = new DebugInfo();
protected Painter m_Painter;
protected int m_SiblingIndex;
@@ -57,7 +58,7 @@ namespace XCharts
protected Vector2 graphAnchorMin { get { return m_GraphMaxAnchor; } }
protected Vector2 graphPivot { get { return m_GraphPivot; } }
public HideFlags chartHideFlags { get { return m_DebugMode ? HideFlags.None : HideFlags.HideInHierarchy; } }
public DebugInfo debug { get { return m_DebugInfo; } }
private ScrollRect m_ScrollRect;
@@ -129,7 +130,7 @@ namespace XCharts
}
}
private void CheckTextMeshPro()
{