EditorUIUtils.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #if UNITY_EDITOR
  2. namespace CodeStage.AdvancedFPSCounter.Editor.UI
  3. {
  4. using UnityEditor;
  5. using UnityEngine;
  6. internal struct EditorUIUtils : System.IDisposable
  7. {
  8. public static GUIStyle richBoldFoldout;
  9. public static GUIStyle richMiniLabel;
  10. public static GUIStyle line;
  11. public static GUIStyle panelWithBackground;
  12. public static void SetupStyles()
  13. {
  14. if (richBoldFoldout != null) return;
  15. richBoldFoldout = new GUIStyle(EditorStyles.foldout)
  16. {
  17. richText = true,
  18. fontStyle = FontStyle.Bold
  19. };
  20. richMiniLabel = new GUIStyle(EditorStyles.miniLabel)
  21. {
  22. wordWrap = true,
  23. richText = true
  24. };
  25. panelWithBackground = new GUIStyle(GUI.skin.box)
  26. {
  27. padding = new RectOffset()
  28. };
  29. line = new GUIStyle(GUI.skin.box);
  30. }
  31. public static void Separator(int padding = 0)
  32. {
  33. if (padding != 0) GUILayout.Space(padding);
  34. var position = EditorGUILayout.GetControlRect(false, 1f);
  35. position = EditorGUI.PrefixLabel(position, GUIContent.none);
  36. var bgTexture = line.normal.background;
  37. #if UNITY_2019_3_OR_NEWER
  38. if (bgTexture == null)
  39. {
  40. var scaledBackgrounds = line.normal.scaledBackgrounds;
  41. if (scaledBackgrounds != null && scaledBackgrounds.Length > 0)
  42. {
  43. bgTexture = line.normal.scaledBackgrounds[0];
  44. }
  45. }
  46. #endif
  47. if (bgTexture != null)
  48. {
  49. var texCoordinates = new Rect(0f, 1f, 1f, 1f - 1f / bgTexture.height);
  50. GUI.DrawTextureWithTexCoords(position, bgTexture, texCoordinates);
  51. }
  52. if (padding != 0) GUILayout.Space(padding);
  53. }
  54. public static void Header(string header)
  55. {
  56. var rect = EditorGUILayout.GetControlRect(false, 24);
  57. rect.y += 8f;
  58. rect = EditorGUI.IndentedRect(rect);
  59. GUI.Label(rect, header, EditorStyles.boldLabel);
  60. }
  61. public static void Indent(int topPadding = 2)
  62. {
  63. EditorGUI.indentLevel++;
  64. GUILayout.Space(topPadding);
  65. }
  66. public static void UnIndent(int bottomPadding = 5)
  67. {
  68. EditorGUI.indentLevel--;
  69. GUILayout.Space(bottomPadding);
  70. }
  71. public static void DoubleIndent(int topPadding = 2)
  72. {
  73. Indent(topPadding);
  74. Indent(0);
  75. }
  76. public static void DoubleUnIndent(int bottomPadding = 5)
  77. {
  78. UnIndent(0);
  79. UnIndent(bottomPadding);
  80. }
  81. public static bool Foldout(SerializedProperty foldout, string caption)
  82. {
  83. Separator(5);
  84. GUILayout.BeginHorizontal(panelWithBackground);
  85. GUILayout.Space(13);
  86. foldout.isExpanded = EditorGUI.Foldout(EditorGUILayout.GetControlRect(), foldout.isExpanded, caption, true, richBoldFoldout);
  87. GUILayout.EndHorizontal();
  88. return foldout.isExpanded;
  89. }
  90. public static bool ToggleFoldout(SerializedProperty toggle, SerializedProperty foldout, string caption, bool bold = true, bool separator = true, bool background = true)
  91. {
  92. if (separator) Separator(5);
  93. if (background)
  94. {
  95. GUILayout.BeginHorizontal(panelWithBackground);
  96. }
  97. else
  98. {
  99. GUILayout.BeginHorizontal();
  100. }
  101. var currentLabelWidth = EditorGUIUtility.labelWidth;
  102. EditorGUIUtility.labelWidth = 1;
  103. EditorGUILayout.PropertyField(toggle, GUIContent.none, GUILayout.ExpandWidth(false));
  104. EditorGUIUtility.labelWidth = currentLabelWidth;
  105. GUILayout.Space(10);
  106. var rect = EditorGUILayout.GetControlRect();
  107. foldout.isExpanded = EditorGUI.Foldout(rect, foldout.isExpanded, caption, true, bold ? richBoldFoldout : EditorStyles.foldout);
  108. GUILayout.EndHorizontal();
  109. return toggle.boolValue;
  110. }
  111. public static bool DrawProperty(SerializedProperty property, System.Action setter, params GUILayoutOption[] options)
  112. {
  113. return DrawProperty(property, (GUIContent)null, setter, options);
  114. }
  115. public static bool DrawProperty(SerializedProperty property, string content, System.Action setter, params GUILayoutOption[] options)
  116. {
  117. return DrawProperty(property, new GUIContent(content), setter, options);
  118. }
  119. public static bool DrawProperty(SerializedProperty property, GUIContent content, System.Action setter, params GUILayoutOption[] options)
  120. {
  121. EditorGUI.BeginChangeCheck();
  122. EditorGUILayout.PropertyField(property, content, options);
  123. if (EditorGUI.EndChangeCheck())
  124. {
  125. setter.Invoke();
  126. return true;
  127. }
  128. return false;
  129. }
  130. // ----------------------------------------------------------------------------
  131. // tooling for "using" keyword
  132. // ----------------------------------------------------------------------------
  133. private readonly LayoutMode mode;
  134. public static EditorUIUtils Horizontal(params GUILayoutOption[] options)
  135. {
  136. return Horizontal(GUIStyle.none, options);
  137. }
  138. public static EditorUIUtils Horizontal(GUIStyle style, params GUILayoutOption[] options)
  139. {
  140. return new EditorUIUtils(LayoutMode.Horizontal, style, options);
  141. }
  142. public static EditorUIUtils Vertical(params GUILayoutOption[] options)
  143. {
  144. return Vertical(GUIStyle.none, options);
  145. }
  146. public static EditorUIUtils Vertical(GUIStyle style, params GUILayoutOption[] options)
  147. {
  148. return new EditorUIUtils(LayoutMode.Vertical, style, options);
  149. }
  150. private EditorUIUtils(LayoutMode layoutMode, GUIStyle style, params GUILayoutOption[] options)
  151. {
  152. mode = layoutMode;
  153. if (mode == LayoutMode.Horizontal)
  154. {
  155. GUILayout.BeginHorizontal(style, options);
  156. }
  157. else
  158. {
  159. GUILayout.BeginVertical(style, options);
  160. }
  161. }
  162. public void Dispose()
  163. {
  164. if (mode == LayoutMode.Horizontal)
  165. {
  166. GUILayout.EndHorizontal();
  167. }
  168. else
  169. {
  170. GUILayout.EndVertical();
  171. }
  172. }
  173. private enum LayoutMode : byte
  174. {
  175. Horizontal,
  176. Vertical
  177. }
  178. }
  179. }
  180. #endif