Histogram.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using UnityEngine;
  2. namespace UnityEditor.Performance.ProfileAnalyzer
  3. {
  4. internal class Histogram
  5. {
  6. Draw2D m_2D;
  7. Color m_ColorBarBackground;
  8. DisplayUnits m_Units;
  9. public void SetUnits(Units units)
  10. {
  11. m_Units = new DisplayUnits(units);
  12. }
  13. public Histogram(Draw2D draw2D, Units units)
  14. {
  15. m_2D = draw2D;
  16. SetUnits(units);
  17. m_ColorBarBackground = new Color(0.5f, 0.5f, 0.5f);
  18. }
  19. public Histogram(Draw2D draw2D, Units units, Color barBackground)
  20. {
  21. m_2D = draw2D;
  22. SetUnits(units);
  23. m_ColorBarBackground = barBackground;
  24. }
  25. public void DrawStart(float width)
  26. {
  27. EditorGUILayout.BeginHorizontal(GUILayout.Width(width + 10));
  28. EditorGUILayout.BeginVertical();
  29. }
  30. public void DrawEnd(float width, float min, float max, float spacing)
  31. {
  32. EditorGUILayout.BeginHorizontal();
  33. float halfWidth = width / 2;
  34. GUIStyle leftAlignStyle = new GUIStyle(GUI.skin.label);
  35. leftAlignStyle.contentOffset = new Vector2(-5, 0);
  36. leftAlignStyle.alignment = TextAnchor.MiddleLeft;
  37. GUIStyle rightAlignStyle = new GUIStyle(GUI.skin.label);
  38. rightAlignStyle.contentOffset = new Vector2(-5, 0);
  39. rightAlignStyle.alignment = TextAnchor.MiddleRight;
  40. EditorGUILayout.LabelField(m_Units.ToString(min, false, 5, true), leftAlignStyle, GUILayout.Width(halfWidth));
  41. EditorGUILayout.LabelField(m_Units.ToString(max, false, 5, true), rightAlignStyle, GUILayout.Width(halfWidth));
  42. EditorGUILayout.EndHorizontal();
  43. EditorGUILayout.EndVertical();
  44. EditorGUILayout.EndHorizontal();
  45. }
  46. public void DrawBackground(float width, float height, int bucketCount, float min, float max, float spacing)
  47. {
  48. //bucketCount = (range == 0f) ? 1 : bucketCount;
  49. float x = (spacing / 2);
  50. float y = 0;
  51. float w = ((width + spacing) / bucketCount) - spacing;
  52. float h = height;
  53. for (int i = 0; i < bucketCount; i++)
  54. {
  55. m_2D.DrawFilledBox(x, y, w, h, m_ColorBarBackground);
  56. x += w;
  57. x += spacing;
  58. }
  59. }
  60. public void DrawData(float width, float height, int[] buckets, int totalFrameCount, float min, float max, Color barColor, float spacing)
  61. {
  62. float range = max - min;
  63. //int bucketCount = (range == 0f) ? 1 : buckets.Length;
  64. int bucketCount = buckets.Length;
  65. float x = (spacing / 2);
  66. float y = 0;
  67. float w = ((width + spacing) / bucketCount) - spacing;
  68. float h = height;
  69. float bucketWidth = (range / bucketCount);
  70. Rect rect = GUILayoutUtility.GetLastRect();
  71. for (int bucketAt = 0; bucketAt < bucketCount; bucketAt++)
  72. {
  73. var count = buckets[bucketAt];
  74. float barHeight = (h * count) / totalFrameCount;
  75. if (count > 0) // Make sure we always slow a small bar if non zero
  76. barHeight = Mathf.Max(1.0f, barHeight);
  77. m_2D.DrawFilledBox(x, y, w, barHeight, barColor);
  78. float bucketStart = min + (bucketAt * bucketWidth);
  79. float bucketEnd = bucketStart + bucketWidth;
  80. var tooltip = string.Format("{0}-{1}\n{2} {3}\n\nBar width: {4}",
  81. m_Units.ToTooltipString(bucketStart, false),
  82. m_Units.ToTooltipString(bucketEnd, true),
  83. count,
  84. count == 1 ? "frame" : "frames",
  85. m_Units.ToTooltipString(bucketWidth, true)
  86. );
  87. var content = new GUIContent("", tooltip);
  88. GUI.Label(new Rect(rect.x + x, rect.y + y, w, h), content);
  89. x += w;
  90. x += spacing;
  91. }
  92. }
  93. public void Draw(float width, float height, int[] buckets, int totalFrameCount, float min, float max, Color barColor)
  94. {
  95. DrawStart(width);
  96. float spacing = 2;
  97. if (m_2D.DrawStart(width, height, Draw2D.Origin.BottomLeft))
  98. {
  99. DrawBackground(width, height, buckets.Length, min, max, spacing);
  100. DrawData(width, height, buckets, totalFrameCount, min, max, barColor, spacing);
  101. m_2D.DrawEnd();
  102. }
  103. DrawEnd(width, min, max, spacing);
  104. }
  105. }
  106. }