AkiraPixelWind/Assets/Axibug/Script/Runtime/Debuger/DebuggerComponent.RuntimeMemorySummaryWindow.cs

118 lines
4.2 KiB
C#
Raw Normal View History

2022-12-29 18:20:40 +08:00
using Axibug;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
namespace Axibug.Runtime
{
public sealed partial class DebuggerComponent : GameComponent
{
private sealed partial class RuntimeMemorySummaryWindow : ScrollableDebuggerWindowBase
{
private readonly List<Record> m_Records = new List<Record>();
private readonly Comparison<Record> m_RecordComparer = RecordComparer;
private DateTime m_SampleTime = DateTime.MinValue;
private int m_SampleCount = 0;
private long m_SampleSize = 0L;
protected override void OnDrawScrollableWindow()
{
GUILayout.Label("<b>Runtime Memory Summary</b>");
GUILayout.BeginVertical("box");
{
if (GUILayout.Button("Take Sample", GUILayout.Height(30f)))
{
TakeSample();
}
if (m_SampleTime <= DateTime.MinValue)
{
GUILayout.Label("<b>Please take sample first.</b>");
}
else
{
GUILayout.Label(Utility.Text.Format("<b>{0} Objects ({1}) obtained at {2:yyyy-MM-dd HH:mm:ss}.</b>", m_SampleCount, GetByteLengthString(m_SampleSize), m_SampleTime.ToLocalTime()));
GUILayout.BeginHorizontal();
{
GUILayout.Label("<b>Type</b>");
GUILayout.Label("<b>Count</b>", GUILayout.Width(120f));
GUILayout.Label("<b>Size</b>", GUILayout.Width(120f));
}
GUILayout.EndHorizontal();
for (int i = 0; i < m_Records.Count; i++)
{
GUILayout.BeginHorizontal();
{
GUILayout.Label(m_Records[i].Name);
GUILayout.Label(m_Records[i].Count.ToString(), GUILayout.Width(120f));
GUILayout.Label(GetByteLengthString(m_Records[i].Size), GUILayout.Width(120f));
}
GUILayout.EndHorizontal();
}
}
}
GUILayout.EndVertical();
}
private void TakeSample()
{
m_Records.Clear();
m_SampleTime = DateTime.UtcNow;
m_SampleCount = 0;
m_SampleSize = 0L;
UnityEngine.Object[] samples = UnityEngine.Resources.FindObjectsOfTypeAll<UnityEngine.Object>();
for (int i = 0; i < samples.Length; i++)
{
long sampleSize = 0L;
sampleSize = Profiler.GetRuntimeMemorySizeLong(samples[i]);
string name = samples[i].GetType().Name;
m_SampleCount++;
m_SampleSize += sampleSize;
Record record = null;
foreach (Record r in m_Records)
{
if (r.Name == name)
{
record = r;
break;
}
}
if (record == null)
{
record = new Record(name);
m_Records.Add(record);
}
record.Count++;
record.Size += sampleSize;
}
m_Records.Sort(m_RecordComparer);
}
private static int RecordComparer(Record a, Record b)
{
int result = b.Size.CompareTo(a.Size);
if (result != 0)
{
return result;
}
result = a.Count.CompareTo(b.Count);
if (result != 0)
{
return result;
}
return a.Name.CompareTo(b.Name);
}
}
}
}