暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InspectorUtils.cs 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Common
  5. {
  6. /// <summary>
  7. /// Utilities to draw foldout headers in Inspector
  8. /// </summary>
  9. internal class InspectorUtils
  10. {
  11. /// <summary>Draw a splitter separator</summary>
  12. /// <param name="isBoxed">[Optional] add margin if the splitter is boxed</param>
  13. public static void DrawSplitter(bool isBoxed = false)
  14. {
  15. var rect = GUILayoutUtility.GetRect(1f, 1f);
  16. float xMin = rect.xMin;
  17. // Splitter rect should be full-width
  18. rect.xMin = 0f;
  19. rect.width += 4f;
  20. if (isBoxed)
  21. {
  22. rect.xMin = xMin == 7.0 ? 4.0f : EditorGUIUtility.singleLineHeight;
  23. rect.width -= 1;
  24. }
  25. if (Event.current.type != EventType.Repaint)
  26. return;
  27. EditorGUI.DrawRect(rect, !EditorGUIUtility.isProSkin
  28. ? new Color(0.6f, 0.6f, 0.6f, 1.333f)
  29. : new Color(0.12f, 0.12f, 0.12f, 1.333f));
  30. }
  31. /// <summary>Draw a header</summary>
  32. /// <param name="title">Title of the header</param>
  33. public static void DrawHeader(string title)
  34. => DrawHeader(EditorGUIUtility.TrTextContent(title));
  35. /// <summary>Draw a header</summary>
  36. /// <param name="title">Title of the header</param>
  37. public static void DrawHeader(GUIContent title)
  38. {
  39. var backgroundRect = GUILayoutUtility.GetRect(1f, 17f);
  40. var labelRect = backgroundRect;
  41. labelRect.xMax -= 20f;
  42. var foldoutRect = backgroundRect;
  43. foldoutRect.y += 1f;
  44. foldoutRect.width = 13f;
  45. foldoutRect.height = 13f;
  46. // Background rect should be full-width
  47. backgroundRect.xMin = 0f;
  48. backgroundRect.width += 4f;
  49. // Background
  50. float backgroundTint = EditorGUIUtility.isProSkin ? 0.1f : 1f;
  51. EditorGUI.DrawRect(backgroundRect, new Color(backgroundTint, backgroundTint, backgroundTint, 0.2f));
  52. // Title
  53. EditorGUI.LabelField(labelRect, title, EditorStyles.boldLabel);
  54. }
  55. /// <summary> Draw a foldout header </summary>
  56. /// <param name="title"> The title of the header </param>
  57. /// <param name="state"> The state of the header </param>
  58. /// <param name="isBoxed"> [optional] is the eader contained in a box style ? </param>
  59. /// <param name="hasMoreOptions"> [optional] Delegate used to draw the right state of the advanced button. If null, no button drawn. </param>
  60. /// <param name="toggleMoreOption"> [optional] Callback call when advanced button clicked. Should be used to toggle its state. </param>
  61. /// <returns>return the state of the foldout header</returns>
  62. public static bool DrawHeaderFoldout(string title, bool state, bool isBoxed = false, Func<bool> hasMoreOptions = null, Action toggleMoreOption = null)
  63. => DrawHeaderFoldout(EditorGUIUtility.TrTextContent(title), state, isBoxed, hasMoreOptions, toggleMoreOption);
  64. /// <summary> Draw a foldout header </summary>
  65. /// <param name="title"> The title of the header </param>
  66. /// <param name="state"> The state of the header </param>
  67. /// <param name="isBoxed"> [optional] is the eader contained in a box style ? </param>
  68. /// <param name="hasMoreOptions"> [optional] Delegate used to draw the right state of the advanced button. If null, no button drawn. </param>
  69. /// <param name="toggleMoreOptions"> [optional] Callback call when advanced button clicked. Should be used to toggle its state. </param>
  70. /// <returns>return the state of the foldout header</returns>
  71. public static bool DrawHeaderFoldout(GUIContent title, bool state, bool isBoxed = false, Func<bool> hasMoreOptions = null, Action toggleMoreOptions = null)
  72. {
  73. const float height = 17f;
  74. var backgroundRect = GUILayoutUtility.GetRect(1f, height);
  75. float xMin = backgroundRect.xMin;
  76. var labelRect = backgroundRect;
  77. labelRect.xMin += 16f;
  78. labelRect.xMax -= 20f;
  79. var foldoutRect = backgroundRect;
  80. foldoutRect.y += 1f;
  81. foldoutRect.width = 13f;
  82. foldoutRect.height = 13f;
  83. foldoutRect.x = labelRect.xMin + 15 * (EditorGUI.indentLevel - 1); //fix for presset
  84. // More options 1/2
  85. var moreOptionsRect = new Rect();
  86. if (hasMoreOptions != null)
  87. {
  88. moreOptionsRect = backgroundRect;
  89. moreOptionsRect.x += moreOptionsRect.width - 16 - 1;
  90. moreOptionsRect.height = 15;
  91. moreOptionsRect.width = 16;
  92. }
  93. // Background rect should be full-width
  94. backgroundRect.xMin = 0f;
  95. backgroundRect.width += 4f;
  96. if (isBoxed)
  97. {
  98. labelRect.xMin += 5;
  99. foldoutRect.xMin += 5;
  100. backgroundRect.xMin = xMin == 7.0 ? 4.0f : EditorGUIUtility.singleLineHeight;
  101. backgroundRect.width -= 1;
  102. }
  103. // Background
  104. float backgroundTint = EditorGUIUtility.isProSkin ? 0.1f : 1f;
  105. EditorGUI.DrawRect(backgroundRect, new Color(backgroundTint, backgroundTint, backgroundTint, 0.2f));
  106. // Title
  107. EditorGUI.LabelField(labelRect, title, EditorStyles.boldLabel);
  108. // Active checkbox
  109. state = GUI.Toggle(foldoutRect, state, GUIContent.none, EditorStyles.foldout);
  110. var e = Event.current;
  111. if (e.type == EventType.MouseDown && backgroundRect.Contains(e.mousePosition) && !moreOptionsRect.Contains(e.mousePosition) && e.button == 0)
  112. {
  113. state = !state;
  114. e.Use();
  115. }
  116. return state;
  117. }
  118. }
  119. }