GroupDrawer.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace HQFPSWeapons
  4. {
  5. [CustomPropertyDrawer(typeof(GroupAttribute))]
  6. public class GroupDrawer : PropertyDrawer
  7. {
  8. private const int k_HeaderPadding = 8;
  9. private const int k_HorizontalPadding = 16;
  10. private const int k_VerticalPadding = 8;
  11. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  12. {
  13. Color normalGUIColor = GUI.color;
  14. Color normalContentColor = GUI.contentColor;
  15. if(property.isExpanded)
  16. GUI.Box(position, "");
  17. Rect rect = new Rect(position.x, position.y, position.width, 24);
  18. GUIStyle buttonStyle = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Scene).button;
  19. buttonStyle.fontStyle = FontStyle.Bold;
  20. buttonStyle.alignment = TextAnchor.MiddleLeft;
  21. buttonStyle.padding = new RectOffset(k_HeaderPadding, 0, 0, 0);
  22. if(!EditorGUIUtility.isProSkin)
  23. GUI.contentColor = new Color(0.3f, 0.3f, 0.3f, 1f);
  24. GUI.color = property.isExpanded ? normalGUIColor : new Color(normalGUIColor.r, normalGUIColor.g, normalGUIColor.b, 0.6f);
  25. if(GUI.Button(EditorGUI.IndentedRect(rect), property.displayName, buttonStyle))
  26. property.isExpanded = !property.isExpanded;
  27. GUI.contentColor = normalContentColor;
  28. GUI.color = normalGUIColor;
  29. rect = new Rect(rect.x + k_HorizontalPadding, rect.y + k_VerticalPadding, rect.width - k_HorizontalPadding * 2, EditorGUIUtility.singleLineHeight);
  30. rect.y = rect.yMax + EditorGUIUtility.standardVerticalSpacing;
  31. normalGUIColor = GUI.color;
  32. GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, 0.825f);
  33. if(property.isExpanded)
  34. {
  35. foreach(SerializedProperty child in property.GetChildren())
  36. {
  37. EditorGUI.PropertyField(rect, child, true);
  38. rect.y = rect.y + EditorGUI.GetPropertyHeight(child, true) + EditorGUIUtility.standardVerticalSpacing;
  39. }
  40. }
  41. GUI.color = normalGUIColor;
  42. }
  43. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  44. {
  45. return EditorGUI.GetPropertyHeight(property, true) + k_HeaderPadding + (property.isExpanded ? k_VerticalPadding : 0);
  46. }
  47. }
  48. }