GradeintEffectPropertyDrawer.cs 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace MPUIKIT.Editor {
  4. [CustomPropertyDrawer(typeof(GradientEffect))]
  5. public class GradeintEffectPropertyDrawer : PropertyDrawer {
  6. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
  7. EditorGUI.BeginProperty(position, label, property);
  8. {
  9. SerializedProperty Enabled = property.FindPropertyRelative("m_Enabled");
  10. bool enabled = Enabled.boolValue;
  11. SerializedProperty gradientType = property.FindPropertyRelative("m_GradientType");
  12. GradientType gradType = (GradientType) gradientType.enumValueIndex;
  13. SerializedProperty gradient = property.FindPropertyRelative("m_Gradient");
  14. SerializedProperty rotation = property.FindPropertyRelative("m_Rotation");
  15. SerializedProperty cornerColors = property.FindPropertyRelative("m_CornerGradientColors");
  16. Rect line = position;
  17. line.height = EditorGUIUtility.singleLineHeight;
  18. EditorGUI.BeginChangeCheck();
  19. {
  20. EditorGUI.showMixedValue = Enabled.hasMultipleDifferentValues;
  21. enabled = EditorGUI.Toggle(line, "Gradient", enabled);
  22. EditorGUI.showMixedValue = false;
  23. if (enabled) {
  24. line.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  25. EditorGUI.showMixedValue = gradientType.hasMultipleDifferentValues;
  26. gradType = (GradientType) EditorGUI.EnumPopup(line, "Type", gradType);
  27. EditorGUI.showMixedValue = false;
  28. }
  29. }
  30. if (EditorGUI.EndChangeCheck()) {
  31. Enabled.boolValue = enabled;
  32. gradientType.enumValueIndex = (int) gradType;
  33. }
  34. if (enabled) {
  35. if (gradType == GradientType.Corner) {
  36. if (cornerColors.arraySize != 4)
  37. cornerColors.arraySize = 4;
  38. line.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  39. float colFieldWidth = line.width / 2f - 5f;
  40. line.width = colFieldWidth;
  41. EditorGUI.PropertyField(line, cornerColors.GetArrayElementAtIndex(0), GUIContent.none);
  42. line.x += colFieldWidth + 10;
  43. EditorGUI.PropertyField(line, cornerColors.GetArrayElementAtIndex(1), GUIContent.none);
  44. line.x -= colFieldWidth + 10;
  45. line.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  46. EditorGUI.PropertyField(line, cornerColors.GetArrayElementAtIndex(2), GUIContent.none);
  47. line.x += colFieldWidth + 10;
  48. EditorGUI.PropertyField(line, cornerColors.GetArrayElementAtIndex(3), GUIContent.none);
  49. line.x -= colFieldWidth + 10;
  50. line.width = colFieldWidth * 2 + 10;
  51. }
  52. else {
  53. line.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  54. EditorGUI.showMixedValue = gradient.hasMultipleDifferentValues;
  55. EditorGUI.PropertyField(line, gradient, false);
  56. if (gradType == GradientType.Linear) {
  57. line.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  58. EditorGUI.showMixedValue = rotation.hasMultipleDifferentValues;
  59. EditorGUI.PropertyField(line, rotation, new GUIContent("Rotation"));
  60. }
  61. EditorGUI.showMixedValue = false;
  62. }
  63. }
  64. }
  65. EditorGUI.EndProperty();
  66. }
  67. public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
  68. SerializedProperty enabled = property.FindPropertyRelative("m_Enabled");
  69. if (enabled.boolValue) {
  70. SerializedProperty gradientMode = property.FindPropertyRelative("m_GradientType");
  71. if (gradientMode.enumValueIndex == (int) GradientType.Radial) {
  72. return EditorGUIUtility.singleLineHeight * 3 + EditorGUIUtility.standardVerticalSpacing * 2;
  73. }
  74. return EditorGUIUtility.singleLineHeight * 4 + EditorGUIUtility.standardVerticalSpacing * 3;
  75. }
  76. return EditorGUIUtility.singleLineHeight;
  77. }
  78. }
  79. }