MultiPropertyAttributeDrawer.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace UnityUIPlayables.Editor
  5. {
  6. [CustomPropertyDrawer(typeof(MultiPropertyAttribute), true)]
  7. public class MultiPropertyAttributeDrawer : PropertyDrawer
  8. {
  9. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  10. {
  11. var attributes = GetAttributes();
  12. var propertyDrawers = GetPropertyDrawers();
  13. if (attributes.Any(attr => !attr.IsVisible(property)))
  14. {
  15. return;
  16. }
  17. foreach (var attr in attributes)
  18. {
  19. attr.OnPreGUI(position, property);
  20. }
  21. using (var ccs = new EditorGUI.ChangeCheckScope())
  22. {
  23. if (propertyDrawers.Length == 0)
  24. {
  25. EditorGUI.PropertyField(position, property, label, true);
  26. }
  27. else
  28. {
  29. propertyDrawers.Last().OnGUI(position, property, label);
  30. }
  31. foreach (var attr in attributes.Reverse())
  32. {
  33. attr.OnPostGUI(position, property, ccs.changed);
  34. }
  35. }
  36. }
  37. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  38. {
  39. var attributes = GetAttributes();
  40. var propertyDrawers = GetPropertyDrawers();
  41. if (attributes.Any(attr => !attr.IsVisible(property)))
  42. {
  43. return -EditorGUIUtility.standardVerticalSpacing;
  44. }
  45. var height = propertyDrawers.Length == 0
  46. ? EditorGUI.GetPropertyHeight(property, label, true)
  47. : propertyDrawers.Last().GetPropertyHeight(property, label);
  48. return height;
  49. }
  50. private MultiPropertyAttribute[] GetAttributes()
  51. {
  52. var attr = (MultiPropertyAttribute) attribute;
  53. if (attr.Attributes == null)
  54. {
  55. attr.Attributes = fieldInfo
  56. .GetCustomAttributes(typeof(MultiPropertyAttribute), false)
  57. .Cast<MultiPropertyAttribute>()
  58. .OrderBy(x => x.order)
  59. .ToArray();
  60. }
  61. return attr.Attributes;
  62. }
  63. private IAttributePropertyDrawer[] GetPropertyDrawers()
  64. {
  65. var attr = (MultiPropertyAttribute) attribute;
  66. if (attr.PropertyDrawers == null)
  67. {
  68. attr.PropertyDrawers = fieldInfo
  69. .GetCustomAttributes(typeof(MultiPropertyAttribute), false)
  70. .OfType<IAttributePropertyDrawer>()
  71. .OrderBy(x => ((MultiPropertyAttribute) x).order)
  72. .ToArray();
  73. }
  74. return attr.PropertyDrawers;
  75. }
  76. }
  77. }