DebuggerComponent.RuntimeMemoryInformationWindow.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 RuntimeMemoryInformationWindow<T> : ScrollableDebuggerWindowBase where T : UnityEngine.Object
  19. {
  20. private const int ShowSampleCount = 300;
  21. private readonly List<Sample> m_Samples = new List<Sample>();
  22. private readonly Comparison<Sample> m_SampleComparer = SampleComparer;
  23. private DateTime m_SampleTime = DateTime.MinValue;
  24. private long m_SampleSize = 0L;
  25. private long m_DuplicateSampleSize = 0L;
  26. private int m_DuplicateSimpleCount = 0;
  27. protected override void OnDrawScrollableWindow()
  28. {
  29. string typeName = typeof(T).Name;
  30. GUILayout.Label(Utility.Text.Format("<b>{0} Runtime Memory Information</b>", typeName));
  31. GUILayout.BeginVertical("box");
  32. {
  33. if (GUILayout.Button(Utility.Text.Format("Take Sample for {0}", typeName), GUILayout.Height(30f)))
  34. {
  35. TakeSample();
  36. }
  37. if (m_SampleTime <= DateTime.MinValue)
  38. {
  39. GUILayout.Label(Utility.Text.Format("<b>Please take sample for {0} first.</b>", typeName));
  40. }
  41. else
  42. {
  43. if (m_DuplicateSimpleCount > 0)
  44. {
  45. GUILayout.Label(Utility.Text.Format("<b>{0} {1}s ({2}) obtained at {3}, while {4} {1}s ({5}) might be duplicated.</b>", m_Samples.Count.ToString(), typeName, GetByteLengthString(m_SampleSize), m_SampleTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"), m_DuplicateSimpleCount.ToString(), GetByteLengthString(m_DuplicateSampleSize)));
  46. }
  47. else
  48. {
  49. GUILayout.Label(Utility.Text.Format("<b>{0} {1}s ({2}) obtained at {3}.</b>", m_Samples.Count.ToString(), typeName, GetByteLengthString(m_SampleSize), m_SampleTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")));
  50. }
  51. if (m_Samples.Count > 0)
  52. {
  53. GUILayout.BeginHorizontal();
  54. {
  55. GUILayout.Label(Utility.Text.Format("<b>{0} Name</b>", typeName));
  56. GUILayout.Label("<b>Type</b>", GUILayout.Width(240f));
  57. GUILayout.Label("<b>Size</b>", GUILayout.Width(80f));
  58. }
  59. GUILayout.EndHorizontal();
  60. }
  61. int count = 0;
  62. for (int i = 0; i < m_Samples.Count; i++)
  63. {
  64. GUILayout.BeginHorizontal();
  65. {
  66. GUILayout.Label(m_Samples[i].Highlight ? Utility.Text.Format("<color=yellow>{0}</color>", m_Samples[i].Name) : m_Samples[i].Name);
  67. GUILayout.Label(m_Samples[i].Highlight ? Utility.Text.Format("<color=yellow>{0}</color>", m_Samples[i].Type) : m_Samples[i].Type, GUILayout.Width(240f));
  68. GUILayout.Label(m_Samples[i].Highlight ? Utility.Text.Format("<color=yellow>{0}</color>", GetByteLengthString(m_Samples[i].Size)) : GetByteLengthString(m_Samples[i].Size), GUILayout.Width(80f));
  69. }
  70. GUILayout.EndHorizontal();
  71. count++;
  72. if (count >= ShowSampleCount)
  73. {
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. GUILayout.EndVertical();
  80. }
  81. private void TakeSample()
  82. {
  83. m_SampleTime = DateTime.UtcNow;
  84. m_SampleSize = 0L;
  85. m_DuplicateSampleSize = 0L;
  86. m_DuplicateSimpleCount = 0;
  87. m_Samples.Clear();
  88. T[] samples = Resources.FindObjectsOfTypeAll<T>();
  89. for (int i = 0; i < samples.Length; i++)
  90. {
  91. long sampleSize = 0L;
  92. #if UNITY_5_6_OR_NEWER
  93. sampleSize = Profiler.GetRuntimeMemorySizeLong(samples[i]);
  94. #else
  95. sampleSize = Profiler.GetRuntimeMemorySize(samples[i]);
  96. #endif
  97. m_SampleSize += sampleSize;
  98. m_Samples.Add(new Sample(samples[i].name, samples[i].GetType().Name, sampleSize));
  99. }
  100. m_Samples.Sort(m_SampleComparer);
  101. for (int i = 1; i < m_Samples.Count; i++)
  102. {
  103. if (m_Samples[i].Name == m_Samples[i - 1].Name && m_Samples[i].Type == m_Samples[i - 1].Type && m_Samples[i].Size == m_Samples[i - 1].Size)
  104. {
  105. m_Samples[i].Highlight = true;
  106. m_DuplicateSampleSize += m_Samples[i].Size;
  107. m_DuplicateSimpleCount++;
  108. }
  109. }
  110. }
  111. private static int SampleComparer(Sample a, Sample b)
  112. {
  113. int result = b.Size.CompareTo(a.Size);
  114. if (result != 0)
  115. {
  116. return result;
  117. }
  118. result = a.Type.CompareTo(b.Type);
  119. if (result != 0)
  120. {
  121. return result;
  122. }
  123. return a.Name.CompareTo(b.Name);
  124. }
  125. }
  126. }
  127. }