DebuggerComponent.RuntimeMemorySummaryWindow.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using System;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. #if UNITY_5_5_OR_NEWER
  12. using UnityEngine.Profiling;
  13. #endif
  14. namespace UnityGameFramework.Runtime
  15. {
  16. public sealed partial class DebuggerComponent : GameFrameworkComponent
  17. {
  18. private sealed partial class RuntimeMemorySummaryWindow : ScrollableDebuggerWindowBase
  19. {
  20. private readonly List<Record> m_Records = new List<Record>();
  21. private readonly Comparison<Record> m_RecordComparer = RecordComparer;
  22. private DateTime m_SampleTime = DateTime.MinValue;
  23. private int m_SampleCount = 0;
  24. private long m_SampleSize = 0L;
  25. protected override void OnDrawScrollableWindow()
  26. {
  27. GUILayout.Label("<b>Runtime Memory Summary</b>");
  28. GUILayout.BeginVertical("box");
  29. {
  30. if (GUILayout.Button("Take Sample", GUILayout.Height(30f)))
  31. {
  32. TakeSample();
  33. }
  34. if (m_SampleTime <= DateTime.MinValue)
  35. {
  36. GUILayout.Label("<b>Please take sample first.</b>");
  37. }
  38. else
  39. {
  40. GUILayout.Label(Utility.Text.Format("<b>{0} Objects ({1}) obtained at {2}.</b>", m_SampleCount.ToString(), GetByteLengthString(m_SampleSize), m_SampleTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")));
  41. GUILayout.BeginHorizontal();
  42. {
  43. GUILayout.Label("<b>Type</b>");
  44. GUILayout.Label("<b>Count</b>", GUILayout.Width(120f));
  45. GUILayout.Label("<b>Size</b>", GUILayout.Width(120f));
  46. }
  47. GUILayout.EndHorizontal();
  48. for (int i = 0; i < m_Records.Count; i++)
  49. {
  50. GUILayout.BeginHorizontal();
  51. {
  52. GUILayout.Label(m_Records[i].Name);
  53. GUILayout.Label(m_Records[i].Count.ToString(), GUILayout.Width(120f));
  54. GUILayout.Label(GetByteLengthString(m_Records[i].Size), GUILayout.Width(120f));
  55. }
  56. GUILayout.EndHorizontal();
  57. }
  58. }
  59. }
  60. GUILayout.EndVertical();
  61. }
  62. private void TakeSample()
  63. {
  64. m_Records.Clear();
  65. m_SampleTime = DateTime.UtcNow;
  66. m_SampleCount = 0;
  67. m_SampleSize = 0L;
  68. UnityEngine.Object[] samples = Resources.FindObjectsOfTypeAll<UnityEngine.Object>();
  69. for (int i = 0; i < samples.Length; i++)
  70. {
  71. long sampleSize = 0L;
  72. #if UNITY_5_6_OR_NEWER
  73. sampleSize = Profiler.GetRuntimeMemorySizeLong(samples[i]);
  74. #else
  75. sampleSize = Profiler.GetRuntimeMemorySize(samples[i]);
  76. #endif
  77. string name = samples[i].GetType().Name;
  78. m_SampleCount++;
  79. m_SampleSize += sampleSize;
  80. Record record = null;
  81. foreach (Record r in m_Records)
  82. {
  83. if (r.Name == name)
  84. {
  85. record = r;
  86. break;
  87. }
  88. }
  89. if (record == null)
  90. {
  91. record = new Record(name);
  92. m_Records.Add(record);
  93. }
  94. record.Count++;
  95. record.Size += sampleSize;
  96. }
  97. m_Records.Sort(m_RecordComparer);
  98. }
  99. private static int RecordComparer(Record a, Record b)
  100. {
  101. int result = b.Size.CompareTo(a.Size);
  102. if (result != 0)
  103. {
  104. return result;
  105. }
  106. result = a.Count.CompareTo(b.Count);
  107. if (result != 0)
  108. {
  109. return result;
  110. }
  111. return a.Name.CompareTo(b.Name);
  112. }
  113. }
  114. }
  115. }