EditorGUICustom.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace HQFPSWeapons
  5. {
  6. public static class EditorGUICustom
  7. {
  8. public static event Action OneSecondPassed;
  9. private static double m_OneSecondTimer;
  10. public static GUIStyle TitleLabel { get; private set; }
  11. public static GUIStyle CenteredMiniLabel { get; private set; }
  12. public static GUIStyle CenteredBoldMiniLabel { get; private set; }
  13. public static GUIStyle MiniGreyLabel { get; private set; }
  14. public static GUIStyle BoldMiniGreyLabel { get; private set; }
  15. public static Color HighlightColor1 { get; private set; }
  16. public static Color HighlightColor2 { get; private set; }
  17. public static readonly Color SeparatorColor = EditorGUIUtility.isProSkin ? new Color(0.157f, 0.157f, 0.157f) : new Color(0.5f, 0.5f, 0.5f);
  18. private static GUIStyle m_SeparatorStyle;
  19. static EditorGUICustom()
  20. {
  21. EditorApplication.update += OnEditorUpdate;
  22. m_SeparatorStyle = new GUIStyle();
  23. m_SeparatorStyle.normal.background = EditorGUIUtility.whiteTexture;
  24. m_SeparatorStyle.stretchWidth = true;
  25. m_SeparatorStyle.margin = new RectOffset(0, 0, 7, 7);
  26. TitleLabel = new GUIStyle(EditorStyles.boldLabel);
  27. TitleLabel.fontSize = 12;
  28. TitleLabel.normal.textColor = EditorGUIUtility.isProSkin ? new Color(1,1,1,0.8f) : new Color(0.2f, 0.2f, 0.2f, 0.8f);
  29. TitleLabel.alignment = TextAnchor.UpperCenter;
  30. CenteredMiniLabel = new GUIStyle(EditorStyles.centeredGreyMiniLabel);
  31. CenteredMiniLabel.normal.textColor = new Color(1, 1, 1, 0.8f);
  32. CenteredMiniLabel.fontSize = 11;
  33. CenteredMiniLabel.alignment = TextAnchor.UpperCenter;
  34. CenteredBoldMiniLabel = new GUIStyle(CenteredMiniLabel);
  35. CenteredBoldMiniLabel.fontStyle = FontStyle.Bold;
  36. MiniGreyLabel = new GUIStyle(EditorStyles.centeredGreyMiniLabel);
  37. MiniGreyLabel.alignment = TextAnchor.MiddleLeft;
  38. BoldMiniGreyLabel = new GUIStyle(MiniGreyLabel);
  39. BoldMiniGreyLabel.fontStyle = FontStyle.Bold;
  40. HighlightColor1 = new Color(0.65f, 0.7f, 0.8f, 1f);
  41. HighlightColor2 = new Color(0.5f, 0.5f, 0.55f, 1f);
  42. }
  43. private static void OnEditorUpdate()
  44. {
  45. if(EditorApplication.timeSinceStartup > m_OneSecondTimer + 1f)
  46. {
  47. if(OneSecondPassed != null)
  48. OneSecondPassed();
  49. m_OneSecondTimer = EditorApplication.timeSinceStartup;
  50. }
  51. }
  52. public static void Separator(Color rgb, float thickness = 1)
  53. {
  54. Rect position = GUILayoutUtility.GetRect(GUIContent.none, m_SeparatorStyle, GUILayout.Height(thickness));
  55. if(Event.current.type == EventType.Repaint)
  56. {
  57. Color restoreColor = GUI.color;
  58. GUI.color = rgb;
  59. m_SeparatorStyle.Draw(position, false, false, false, false);
  60. GUI.color = restoreColor;
  61. }
  62. }
  63. public static void Separator(float thickness, GUIStyle splitterStyle)
  64. {
  65. Rect position = GUILayoutUtility.GetRect(GUIContent.none, splitterStyle, GUILayout.Height(thickness));
  66. if(Event.current.type == EventType.Repaint)
  67. {
  68. Color restoreColor = GUI.color;
  69. GUI.color = SeparatorColor;
  70. splitterStyle.Draw(position, false, false, false, false);
  71. GUI.color = restoreColor;
  72. }
  73. }
  74. public static void Separator(float thickness = 1)
  75. {
  76. Separator(thickness, m_SeparatorStyle);
  77. }
  78. public static void Separator(Rect position, Color color)
  79. {
  80. if(Event.current.type == EventType.Repaint)
  81. {
  82. Color restoreColor = GUI.color;
  83. GUI.color = color;
  84. m_SeparatorStyle.Draw(position, false, false, false, false);
  85. GUI.color = restoreColor;
  86. }
  87. }
  88. public static void Separator(Rect position)
  89. {
  90. Separator(position, SeparatorColor);
  91. }
  92. public static void EnumPopupNonAlloc(Rect rect, SerializedProperty property, ref string[] names)
  93. {
  94. property.enumValueIndex = EditorGUI.Popup(rect, property.enumValueIndex, names);
  95. }
  96. public static int IndexOfString(string str, string[] allStrings)
  97. {
  98. for(int i = 0;i < allStrings.Length;i++)
  99. {
  100. if(allStrings[i] == str)
  101. return i;
  102. }
  103. return 0;
  104. }
  105. public static string StringAtIndex(int i, string[] allStrings)
  106. {
  107. return allStrings.Length > i ? allStrings[i] : "";
  108. }
  109. }
  110. }