No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GUIHelpers.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #if UNITY_EDITOR
  2. using System.IO;
  3. using UnityEditor;
  4. namespace UnityEngine.InputSystem.Editor
  5. {
  6. internal static class GUIHelpers
  7. {
  8. public static class Styles
  9. {
  10. public static readonly GUIStyle lineSeparator = new GUIStyle().WithFixedHeight(1).WithMargin(new RectOffset(0, 0, 2, 2));
  11. }
  12. private const string kIconPath = "Packages/com.unity.inputsystem/InputSystem/Editor/Icons/";
  13. public static void DrawLineSeparator(string label = null)
  14. {
  15. var hasLabel = !string.IsNullOrEmpty(label);
  16. EditorGUILayout.BeginVertical();
  17. var rect = GUILayoutUtility.GetRect(GUIContent.none, Styles.lineSeparator, GUILayout.ExpandWidth(true));
  18. var labelRect = new Rect();
  19. GUIContent labelContent = null;
  20. if (hasLabel)
  21. {
  22. labelContent = new GUIContent(label);
  23. labelRect = GUILayoutUtility.GetRect(labelContent, EditorStyles.miniLabel, GUILayout.ExpandWidth(true));
  24. }
  25. EditorGUILayout.EndVertical();
  26. if (Event.current.type != EventType.Repaint)
  27. return;
  28. var orgColor = GUI.color;
  29. var tintColor = EditorGUIUtility.isProSkin ? new Color(0.12f, 0.12f, 0.12f, 1.333f) : new Color(0.6f, 0.6f, 0.6f, 1.333f);
  30. GUI.color = GUI.color * tintColor;
  31. GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture);
  32. GUI.color = orgColor;
  33. if (hasLabel)
  34. EditorGUI.LabelField(labelRect, labelContent, EditorStyles.miniLabel);
  35. }
  36. public static Texture2D LoadIcon(string name)
  37. {
  38. var skinPrefix = EditorGUIUtility.isProSkin ? "d_" : "";
  39. var scale = Mathf.Clamp((int)EditorGUIUtility.pixelsPerPoint, 0, 4);
  40. var scalePostFix = scale > 1 ? $"@{scale}x" : "";
  41. if (name.IndexOfAny(Path.GetInvalidFileNameChars()) > -1)
  42. name = string.Join("_", name.Split(Path.GetInvalidFileNameChars()));
  43. var path = Path.Combine(kIconPath, skinPrefix + name + scalePostFix + ".png");
  44. return AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  45. }
  46. public static GUIStyle WithNormalBackground(this GUIStyle style, Texture2D background)
  47. {
  48. style.normal.background = background;
  49. return style;
  50. }
  51. public static GUIStyle WithFontSize(this GUIStyle style, int fontSize)
  52. {
  53. style.fontSize = fontSize;
  54. return style;
  55. }
  56. public static GUIStyle WithFontStyle(this GUIStyle style, FontStyle fontStyle)
  57. {
  58. style.fontStyle = fontStyle;
  59. return style;
  60. }
  61. public static GUIStyle WithAlignment(this GUIStyle style, TextAnchor alignment)
  62. {
  63. style.alignment = alignment;
  64. return style;
  65. }
  66. public static GUIStyle WithMargin(this GUIStyle style, RectOffset margin)
  67. {
  68. style.margin = margin;
  69. return style;
  70. }
  71. public static GUIStyle WithBorder(this GUIStyle style, RectOffset border)
  72. {
  73. style.border = border;
  74. return style;
  75. }
  76. public static GUIStyle WithPadding(this GUIStyle style, RectOffset padding)
  77. {
  78. style.padding = padding;
  79. return style;
  80. }
  81. public static GUIStyle WithFixedWidth(this GUIStyle style, int fixedWidth)
  82. {
  83. style.fixedWidth = fixedWidth;
  84. return style;
  85. }
  86. public static GUIStyle WithFixedHeight(this GUIStyle style, int fixedHeight)
  87. {
  88. style.fixedHeight = fixedHeight;
  89. return style;
  90. }
  91. public static GUIStyle WithRichText(this GUIStyle style, bool richText = true)
  92. {
  93. style.richText = richText;
  94. return style;
  95. }
  96. public static GUIStyle WithFont(this GUIStyle style, Font font)
  97. {
  98. style.font = font;
  99. return style;
  100. }
  101. public static GUIStyle WithContentOffset(this GUIStyle style, Vector2 contentOffset)
  102. {
  103. style.contentOffset = contentOffset;
  104. return style;
  105. }
  106. public static GUIStyle WithNormalTextColor(this GUIStyle style, Color textColor)
  107. {
  108. style.normal.textColor = textColor;
  109. return style;
  110. }
  111. }
  112. }
  113. #endif // UNITY_EDITOR