AnimationModifierTool.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.Tools
  7. {
  8. /// <summary>[Editor-Only] [Pro-Only]
  9. /// A base <see cref="AnimancerToolsWindow.Tool"/> for modifying <see cref="AnimationClip"/>s.
  10. /// </summary>
  11. /// <remarks>
  12. /// <strong>Documentation:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/manual/tools">
  14. /// Animancer Tools</see>
  15. /// </remarks>
  16. /// https://kybernetik.com.au/animancer/api/Animancer.Editor.Tools/AnimationModifierTool
  17. ///
  18. [Serializable]
  19. public abstract class AnimationModifierTool : AnimancerToolsWindow.Tool
  20. {
  21. /************************************************************************************************************************/
  22. [SerializeField]
  23. private AnimationClip _Animation;
  24. /// <summary>The currently selected <see cref="AnimationClip"/> asset.</summary>
  25. public AnimationClip Animation => _Animation;
  26. /************************************************************************************************************************/
  27. /// <inheritdoc/>
  28. public override void OnEnable(int index)
  29. {
  30. base.OnEnable(index);
  31. OnAnimationChanged();
  32. }
  33. /************************************************************************************************************************/
  34. /// <inheritdoc/>
  35. public override void OnSelectionChanged()
  36. {
  37. if (Selection.activeObject is AnimationClip animation)
  38. {
  39. _Animation = animation;
  40. OnAnimationChanged();
  41. }
  42. }
  43. /************************************************************************************************************************/
  44. /// <summary>Called whenever the selected <see cref="Animation"/> changes.</summary>
  45. protected virtual void OnAnimationChanged() { }
  46. /************************************************************************************************************************/
  47. /// <inheritdoc/>
  48. public override void DoBodyGUI()
  49. {
  50. AnimancerToolsWindow.BeginChangeCheck();
  51. var animation = AnimancerGUI.DoObjectFieldGUI("Animation", _Animation, false);
  52. if (AnimancerToolsWindow.EndChangeCheck(ref _Animation, animation))
  53. OnAnimationChanged();
  54. }
  55. /************************************************************************************************************************/
  56. /// <summary>Calls <see cref="AnimancerToolsWindow.Tool.SaveModifiedAsset"/> on the animation.</summary>
  57. protected bool SaveAs()
  58. {
  59. AnimancerGUI.Deselect();
  60. if (SaveModifiedAsset(
  61. "Save Modified Animation",
  62. "Where would you like to save the new animation?",
  63. _Animation,
  64. Modify))
  65. {
  66. _Animation = null;
  67. OnAnimationChanged();
  68. return true;
  69. }
  70. else return false;
  71. }
  72. /************************************************************************************************************************/
  73. /// <summary>Override this to apply the desired modifications to the `animation` before it is saved.</summary>
  74. protected virtual void Modify(AnimationClip animation) { }
  75. /************************************************************************************************************************/
  76. }
  77. }
  78. #endif