ClipState.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.Animations;
  5. using UnityEngine.Playables;
  6. using Object = UnityEngine.Object;
  7. namespace Animancer
  8. {
  9. /// <summary>An <see cref="AnimancerState"/> which plays an <see cref="AnimationClip"/>.</summary>
  10. /// <remarks>
  11. /// Documentation: <see href="https://kybernetik.com.au/animancer/docs/manual/playing/states">States</see>
  12. /// </remarks>
  13. /// https://kybernetik.com.au/animancer/api/Animancer/ClipState
  14. ///
  15. public class ClipState : AnimancerState
  16. {
  17. /************************************************************************************************************************/
  18. /// <summary>An <see cref="ITransition{TState}"/> that creates a <see cref="ClipState"/>.</summary>
  19. public interface ITransition : ITransition<ClipState> { }
  20. /************************************************************************************************************************/
  21. #region Fields and Properties
  22. /************************************************************************************************************************/
  23. /// <summary>The <see cref="AnimationClip"/> which this state plays.</summary>
  24. private AnimationClip _Clip;
  25. /// <summary>The <see cref="AnimationClip"/> which this state plays.</summary>
  26. public override AnimationClip Clip
  27. {
  28. get => _Clip;
  29. set
  30. {
  31. Validate.AssertNotLegacy(value);
  32. ChangeMainObject(ref _Clip, value);
  33. }
  34. }
  35. /// <summary>The <see cref="AnimationClip"/> which this state plays.</summary>
  36. public override Object MainObject
  37. {
  38. get => _Clip;
  39. set => Clip = (AnimationClip)value;
  40. }
  41. /************************************************************************************************************************/
  42. /// <summary>The <see cref="AnimationClip.length"/>.</summary>
  43. public override float Length => _Clip.length;
  44. /************************************************************************************************************************/
  45. /// <summary>The <see cref="Motion.isLooping"/>.</summary>
  46. public override bool IsLooping => _Clip.isLooping;
  47. /************************************************************************************************************************/
  48. /// <inheritdoc/>
  49. public override Vector3 AverageVelocity => _Clip.averageSpeed;
  50. /************************************************************************************************************************/
  51. #region Inverse Kinematics
  52. /************************************************************************************************************************/
  53. /// <inheritdoc/>
  54. public override bool ApplyAnimatorIK
  55. {
  56. get
  57. {
  58. Validate.AssertPlayable(this);
  59. return ((AnimationClipPlayable)_Playable).GetApplyPlayableIK();
  60. }
  61. set
  62. {
  63. Validate.AssertPlayable(this);
  64. ((AnimationClipPlayable)_Playable).SetApplyPlayableIK(value);
  65. }
  66. }
  67. /************************************************************************************************************************/
  68. /// <inheritdoc/>
  69. public override bool ApplyFootIK
  70. {
  71. get
  72. {
  73. Validate.AssertPlayable(this);
  74. return ((AnimationClipPlayable)_Playable).GetApplyFootIK();
  75. }
  76. set
  77. {
  78. Validate.AssertPlayable(this);
  79. ((AnimationClipPlayable)_Playable).SetApplyFootIK(value);
  80. }
  81. }
  82. /************************************************************************************************************************/
  83. #endregion
  84. /************************************************************************************************************************/
  85. #endregion
  86. /************************************************************************************************************************/
  87. #region Methods
  88. /************************************************************************************************************************/
  89. /// <summary>Creates a new <see cref="ClipState"/> and sets its <see cref="Clip"/>.</summary>
  90. /// <exception cref="ArgumentNullException">The `clip` is null.</exception>
  91. public ClipState(AnimationClip clip)
  92. {
  93. Validate.AssertNotLegacy(clip);
  94. _Clip = clip;
  95. }
  96. /************************************************************************************************************************/
  97. /// <summary>Creates and assigns the <see cref="AnimationClipPlayable"/> managed by this node.</summary>
  98. protected override void CreatePlayable(out Playable playable)
  99. {
  100. var clipPlayable = AnimationClipPlayable.Create(Root._Graph, _Clip);
  101. playable = clipPlayable;
  102. }
  103. /************************************************************************************************************************/
  104. /// <inheritdoc/>
  105. public override void Destroy()
  106. {
  107. _Clip = null;
  108. base.Destroy();
  109. }
  110. /************************************************************************************************************************/
  111. #endregion
  112. /************************************************************************************************************************/
  113. #region Inspector
  114. #if UNITY_EDITOR
  115. /************************************************************************************************************************/
  116. /// <summary>[Editor-Only] Returns a <see cref="Drawer"/> for this state.</summary>
  117. protected internal override Editor.IAnimancerNodeDrawer CreateDrawer() => new Drawer(this);
  118. /************************************************************************************************************************/
  119. /// <inheritdoc/>
  120. public class Drawer : Editor.AnimancerStateDrawer<ClipState>
  121. {
  122. /************************************************************************************************************************/
  123. /// <summary>Creates a new <see cref="Drawer"/> to manage the Inspector GUI for the `state`.</summary>
  124. public Drawer(ClipState state) : base(state) { }
  125. /************************************************************************************************************************/
  126. /// <inheritdoc/>
  127. protected override void AddContextMenuFunctions(UnityEditor.GenericMenu menu)
  128. {
  129. menu.AddDisabledItem(new GUIContent(
  130. $"{DetailsPrefix}Animation Type: {Editor.AnimationBindings.GetAnimationType(Target._Clip)}"));
  131. base.AddContextMenuFunctions(menu);
  132. Editor.AnimancerEditorUtilities.AddContextMenuIK(menu, Target);
  133. }
  134. /************************************************************************************************************************/
  135. }
  136. /************************************************************************************************************************/
  137. #endif
  138. #endregion
  139. /************************************************************************************************************************/
  140. }
  141. }