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.

SpriteStateDrawer.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEditor.UIElements;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.UI
  6. {
  7. [CustomPropertyDrawer(typeof(SpriteState), true)]
  8. /// <summary>
  9. /// This is a PropertyDrawer for SpriteState. It is implemented using the standard Unity PropertyDrawer framework.
  10. /// </summary>
  11. public class SpriteStateDrawer : PropertyDrawer
  12. {
  13. const string kHighlightedSprite = "m_HighlightedSprite";
  14. const string kPressedSprite = "m_PressedSprite";
  15. const string kSelectedSprite = "m_SelectedSprite";
  16. const string kDisabledSprite = "m_DisabledSprite";
  17. const string kVisualElementName = "SpriteState";
  18. public override void OnGUI(Rect rect, SerializedProperty prop, GUIContent label)
  19. {
  20. Rect drawRect = rect;
  21. drawRect.height = EditorGUIUtility.singleLineHeight;
  22. SerializedProperty highlightedSprite = prop.FindPropertyRelative(kHighlightedSprite);
  23. SerializedProperty pressedSprite = prop.FindPropertyRelative(kPressedSprite);
  24. SerializedProperty selectedSprite = prop.FindPropertyRelative(kSelectedSprite);
  25. SerializedProperty disabledSprite = prop.FindPropertyRelative(kDisabledSprite);
  26. EditorGUI.PropertyField(drawRect, highlightedSprite);
  27. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  28. EditorGUI.PropertyField(drawRect, pressedSprite);
  29. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  30. EditorGUI.PropertyField(drawRect, selectedSprite);
  31. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  32. EditorGUI.PropertyField(drawRect, disabledSprite);
  33. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  34. }
  35. public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
  36. {
  37. return 4 * EditorGUIUtility.singleLineHeight + 3 * EditorGUIUtility.standardVerticalSpacing;
  38. }
  39. public override VisualElement CreatePropertyGUI(SerializedProperty property)
  40. {
  41. var container = new VisualElement();
  42. container.name = kVisualElementName;
  43. var properties = new[]
  44. {
  45. property.FindPropertyRelative(kHighlightedSprite),
  46. property.FindPropertyRelative(kPressedSprite),
  47. property.FindPropertyRelative(kSelectedSprite),
  48. property.FindPropertyRelative(kDisabledSprite)
  49. };
  50. foreach (var prop in properties)
  51. {
  52. container.Add(new PropertyField(prop));
  53. }
  54. return container;
  55. }
  56. }
  57. }