Polymorphic.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer
  4. {
  5. /************************************************************************************************************************/
  6. /// <summary>An object that will be drawn by a <see cref="Editor.PolymorphicDrawer"/>.</summary>
  7. public interface IPolymorphic { }
  8. /************************************************************************************************************************/
  9. /// <summary>An <see cref="IPolymorphic"/> with a <see cref="Reset"/> method.</summary>
  10. public interface IPolymorphicReset : IPolymorphic
  11. {
  12. /// <summary>Called when an instance of this type is created in a [<see cref="SerializeReference"/>] field.</summary>
  13. void Reset();
  14. }
  15. /************************************************************************************************************************/
  16. /// <summary>The attributed field will be drawn by a <see cref="Editor.PolymorphicDrawer"/>.</summary>
  17. public sealed class PolymorphicAttribute : PropertyAttribute { }
  18. /************************************************************************************************************************/
  19. }
  20. #if UNITY_EDITOR
  21. namespace Animancer.Editor
  22. {
  23. using UnityEditor;
  24. /// <summary>[Editor-Only]
  25. /// A <see cref="PropertyDrawer"/> for <see cref="IPolymorphic"/> and <see cref="PolymorphicAttribute"/>.
  26. /// </summary>
  27. [CustomPropertyDrawer(typeof(IPolymorphic), true)]
  28. [CustomPropertyDrawer(typeof(PolymorphicAttribute), true)]
  29. public class PolymorphicDrawer : PropertyDrawer
  30. {
  31. /************************************************************************************************************************/
  32. /// <inheritdoc/>
  33. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  34. => EditorGUI.GetPropertyHeight(property, label, true);
  35. /************************************************************************************************************************/
  36. /// <inheritdoc/>
  37. public override void OnGUI(Rect area, SerializedProperty property, GUIContent label)
  38. {
  39. using (new TypeSelectionButton(area, property, true))
  40. EditorGUI.PropertyField(area, property, label, true);
  41. }
  42. /************************************************************************************************************************/
  43. }
  44. }
  45. #endif