BaseAnimancerComponentEditor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Animancer.Editor
  7. {
  8. /// <summary>[Editor-Only] A custom Inspector for <see cref="IAnimancerComponent"/>s.</summary>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/BaseAnimancerComponentEditor
  10. public abstract class BaseAnimancerComponentEditor : UnityEditor.Editor
  11. {
  12. /************************************************************************************************************************/
  13. [NonSerialized]
  14. private IAnimancerComponent[] _Targets;
  15. /// <summary><see cref="UnityEditor.Editor.targets"/> casted to <see cref="IAnimancerComponent"/>.</summary>
  16. public IAnimancerComponent[] Targets
  17. => _Targets;
  18. /************************************************************************************************************************/
  19. /// <summary>Initializes this <see cref="UnityEditor.Editor"/>.</summary>
  20. protected virtual void OnEnable()
  21. {
  22. var targets = this.targets;
  23. _Targets = new IAnimancerComponent[targets.Length];
  24. GatherTargets();
  25. }
  26. /************************************************************************************************************************/
  27. /// <summary>
  28. /// Copies the <see cref="UnityEditor.Editor.targets"/> into the <see cref="_Targets"/> array.
  29. /// </summary>
  30. private void GatherTargets()
  31. {
  32. for (int i = 0; i < _Targets.Length; i++)
  33. _Targets[i] = (IAnimancerComponent)targets[i];
  34. }
  35. /************************************************************************************************************************/
  36. /// <summary>Called by the Unity editor to draw the custom Inspector GUI elements.</summary>
  37. public override void OnInspectorGUI()
  38. {
  39. // Normally the targets wouldn't change after OnEnable, but the trick AnimancerComponent.Reset uses to
  40. // swap the type of an existing component when a new one is added causes the old target to be destroyed.
  41. GatherTargets();
  42. serializedObject.Update();
  43. DoSerializedFieldsGUI();
  44. serializedObject.ApplyModifiedProperties();
  45. }
  46. /************************************************************************************************************************/
  47. /// <summary>Draws the rest of the Inspector fields after the Animator field.</summary>
  48. protected void DoSerializedFieldsGUI()
  49. {
  50. var property = serializedObject.GetIterator();
  51. if (!property.NextVisible(true))
  52. return;
  53. do
  54. {
  55. var path = property.propertyPath;
  56. if (path == "m_Script")
  57. continue;
  58. using (var label = PooledGUIContent.Acquire(property))
  59. {
  60. // Let the target try to override.
  61. if (DoOverridePropertyGUI(path, property, label))
  62. continue;
  63. // Otherwise draw the property normally.
  64. EditorGUILayout.PropertyField(property, label, true);
  65. }
  66. }
  67. while (property.NextVisible(false));
  68. }
  69. /************************************************************************************************************************/
  70. /// <summary>[Editor-Only]
  71. /// Draws any custom GUI for the `property`.
  72. /// The return value indicates whether the GUI should replace the regular call to
  73. /// <see cref="EditorGUILayout.PropertyField(SerializedProperty, GUIContent, bool, GUILayoutOption[])"/>.
  74. /// True = GUI was drawn, so don't draw the regular GUI.
  75. /// False = Draw the regular GUI.
  76. /// </summary>
  77. protected virtual bool DoOverridePropertyGUI(string path, SerializedProperty property, GUIContent label)
  78. => false;
  79. /************************************************************************************************************************/
  80. }
  81. }
  82. #endif