Newer
Older
Simple-Multiplayer-Unity3D / Multiplayer Project / Library / PackageCache / com.unity.performance.profile-analyzer@1.2.2 / Editor / DepthSliceDropdown.cs
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEditor.IMGUI.Controls;
  6. namespace UnityEditor.Performance.ProfileAnalyzer
  7. {
  8. class DepthSliceDropdown : AdvancedDropdown
  9. {
  10. class DepthSliceDropdownItem : AdvancedDropdownItem
  11. {
  12. public int depthSlice;
  13. public int depthSliceLeft;
  14. public int depthSliceRight;
  15. public DepthSliceDropdownItem(int depthSlice)
  16. : base(DepthSliceUI.DepthFilterToString(depthSlice))
  17. {
  18. this.depthSlice = depthSlice;
  19. depthSliceLeft = depthSlice;
  20. depthSliceRight = depthSlice;
  21. }
  22. public DepthSliceDropdownItem(int depthSliceLeft, int depthSliceRight, bool leftIsMain)
  23. : base(DepthSliceUI.DepthFilterToString(depthSliceLeft, depthSliceRight, leftIsMain))
  24. {
  25. depthSlice = Math.Max(depthSliceLeft, depthSliceRight);
  26. this.depthSliceLeft = depthSliceLeft;
  27. this.depthSliceRight = depthSliceRight;
  28. }
  29. }
  30. Action<int, int, int> m_Callback = null;
  31. int m_DepthSliceCount;
  32. int m_DepthSliceCountRight;
  33. int m_CurrentDepthSliceA;
  34. int m_CurrentDepthSliceB;
  35. int m_DepthDiff;
  36. static FieldInfo m_DataSourceFieldInfo;
  37. static Type m_DataSourceTypeInfo;
  38. static PropertyInfo m_SelectedIdsFieldInfo;
  39. public DepthSliceDropdown(int depthSliceCount, int currentDepthSliceA, int currentDepthSliceB, Action<int, int, int> callback, int depthDiff, int depthSliceCountRight = ProfileAnalyzer.kDepthAll) : base(new AdvancedDropdownState())
  40. {
  41. m_DepthSliceCount = depthSliceCount;
  42. m_DepthSliceCountRight = depthSliceCountRight;
  43. m_CurrentDepthSliceA = currentDepthSliceA;
  44. m_CurrentDepthSliceB = currentDepthSliceB;
  45. m_Callback = callback;
  46. m_DepthDiff = depthDiff;
  47. if (m_DataSourceFieldInfo == null || m_DataSourceFieldInfo == null || m_SelectedIdsFieldInfo == null)
  48. {
  49. Assembly assem = typeof(AdvancedDropdown).Assembly;
  50. var advancedDropdownTypeInfo = typeof(AdvancedDropdown);
  51. m_DataSourceTypeInfo = assem.GetType("UnityEditor.IMGUI.Controls.CallbackDataSource");
  52. m_DataSourceFieldInfo = advancedDropdownTypeInfo.GetField("m_DataSource", BindingFlags.NonPublic | BindingFlags.Instance);
  53. m_SelectedIdsFieldInfo = m_DataSourceTypeInfo.GetProperty("selectedIDs", BindingFlags.Public | BindingFlags.Instance);
  54. }
  55. }
  56. protected override AdvancedDropdownItem BuildRoot()
  57. {
  58. var root = new AdvancedDropdownItem("Depth Slice");
  59. var allItem = new DepthSliceDropdownItem(ProfileAnalyzer.kDepthAll);
  60. root.AddChild(allItem);
  61. if (m_CurrentDepthSliceA == ProfileAnalyzer.kDepthAll && m_CurrentDepthSliceB == ProfileAnalyzer.kDepthAll)
  62. (m_SelectedIdsFieldInfo.GetValue(m_DataSourceFieldInfo.GetValue(this)) as List<int>).Add(allItem.id);
  63. var count = m_DepthSliceCountRight == ProfileAnalyzer.kDepthAll ? m_DepthSliceCount :
  64. Math.Max(m_DepthSliceCount + Math.Max(0, m_DepthDiff), m_DepthSliceCountRight - Math.Min(0, m_DepthDiff));
  65. var leftIsMain = m_DepthDiff < 0;
  66. var mainThreshold = leftIsMain ? m_DepthSliceCount : m_DepthSliceCountRight;
  67. var secondaryMinThreshold = Math.Abs(m_DepthDiff);
  68. var secondaryMaxThreshold = (leftIsMain ? m_DepthSliceCountRight : m_DepthSliceCount) + secondaryMinThreshold;
  69. var startIndex = 1;
  70. for (int i = startIndex; i <= count; i++)
  71. {
  72. var selected = false;
  73. AdvancedDropdownItem child;
  74. if (m_DepthSliceCountRight != ProfileAnalyzer.kDepthAll)
  75. {
  76. var left = Mathf.Clamp(i - Math.Max(0, m_DepthDiff), 1, m_DepthSliceCount);
  77. var right = Mathf.Clamp(i - Math.Max(0, -m_DepthDiff), 1, m_DepthSliceCountRight);
  78. if (m_DepthSliceCount <= 0)
  79. left = -1;
  80. else if (m_DepthSliceCountRight <= 0)
  81. right = -1;
  82. else
  83. {
  84. // Separators only make sense if there is data on both sides
  85. // did we pass the threshold of the main's max depth and started clamping it down?
  86. if (i == mainThreshold + 1
  87. // ... or the threshold of the secondary's negative depth when adjusted for the depth diff, and stoped clamping it up?
  88. || (secondaryMinThreshold != 0 && i == secondaryMinThreshold + 1)
  89. // ... or the threshold of the secondary's max depth when adjusted for the depth diff, and started clamping it down?
  90. || (i == secondaryMaxThreshold + 1))
  91. root.AddSeparator();
  92. }
  93. child = new DepthSliceDropdownItem(left, right, leftIsMain);
  94. selected = m_CurrentDepthSliceA == left && m_CurrentDepthSliceB == right;
  95. }
  96. else
  97. {
  98. child = new DepthSliceDropdownItem(i);
  99. selected = m_CurrentDepthSliceA == i;
  100. }
  101. root.AddChild(child);
  102. if (selected)
  103. (m_SelectedIdsFieldInfo.GetValue(m_DataSourceFieldInfo.GetValue(this)) as List<int>).Add(child.id);
  104. }
  105. return root;
  106. }
  107. protected override void ItemSelected(AdvancedDropdownItem item)
  108. {
  109. base.ItemSelected(item);
  110. if (m_Callback != null)
  111. {
  112. var sliceItem = (item as DepthSliceDropdownItem);
  113. m_Callback(sliceItem.depthSlice, sliceItem.depthSliceLeft, sliceItem.depthSliceRight);
  114. }
  115. }
  116. }
  117. }