ClipState.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 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. /// <strong>Documentation:</strong>
  12. /// <see href="https://kybernetik.com.au/animancer/docs/manual/playing/states">
  13. /// States</see>
  14. /// </remarks>
  15. /// https://kybernetik.com.au/animancer/api/Animancer/ClipState
  16. ///
  17. public class ClipState : AnimancerState
  18. {
  19. /************************************************************************************************************************/
  20. #region Fields and Properties
  21. /************************************************************************************************************************/
  22. private AnimationClip _Clip;
  23. /// <summary>The <see cref="AnimationClip"/> which this state plays.</summary>
  24. public override AnimationClip Clip
  25. {
  26. get => _Clip;
  27. set
  28. {
  29. Validate.AssertAnimationClip(value, true, $"set {nameof(ClipState)}.{nameof(Clip)}");
  30. if (ChangeMainObject(ref _Clip, value))
  31. {
  32. _Length = value.length;
  33. var isLooping = value.isLooping;
  34. if (_IsLooping != isLooping)
  35. {
  36. _IsLooping = isLooping;
  37. OnIsLoopingChangedRecursive(isLooping);
  38. }
  39. }
  40. }
  41. }
  42. /// <summary>The <see cref="AnimationClip"/> which this state plays.</summary>
  43. public override Object MainObject
  44. {
  45. get => _Clip;
  46. set => Clip = (AnimationClip)value;
  47. }
  48. #if UNITY_EDITOR
  49. /// <inheritdoc/>
  50. public override Type MainObjectType
  51. => typeof(AnimationClip);
  52. #endif
  53. /************************************************************************************************************************/
  54. private float _Length;
  55. /// <summary>The <see cref="AnimationClip.length"/>.</summary>
  56. public override float Length
  57. => _Length;
  58. /************************************************************************************************************************/
  59. private bool _IsLooping;
  60. /// <summary>The <see cref="Motion.isLooping"/>.</summary>
  61. public override bool IsLooping
  62. => _IsLooping;
  63. /************************************************************************************************************************/
  64. /// <inheritdoc/>
  65. public override void GetEventDispatchInfo(
  66. out float length,
  67. out float normalizedTime,
  68. out bool isLooping)
  69. {
  70. length = _Length;
  71. normalizedTime = length != 0
  72. ? Time / length
  73. : 0;
  74. isLooping = _IsLooping;
  75. }
  76. /************************************************************************************************************************/
  77. /// <inheritdoc/>
  78. public override Vector3 AverageVelocity
  79. => _Clip.averageSpeed;
  80. /************************************************************************************************************************/
  81. #region Inverse Kinematics
  82. /************************************************************************************************************************/
  83. /// <inheritdoc/>
  84. public override bool ApplyAnimatorIK
  85. {
  86. get => _Playable.IsValid() && ((AnimationClipPlayable)_Playable).GetApplyPlayableIK();
  87. set
  88. {
  89. Validate.AssertPlayable(this);
  90. ((AnimationClipPlayable)_Playable).SetApplyPlayableIK(value);
  91. }
  92. }
  93. /************************************************************************************************************************/
  94. /// <inheritdoc/>
  95. public override bool ApplyFootIK
  96. {
  97. get => _Playable.IsValid() && ((AnimationClipPlayable)_Playable).GetApplyFootIK();
  98. set
  99. {
  100. Validate.AssertPlayable(this);
  101. ((AnimationClipPlayable)_Playable).SetApplyFootIK(value);
  102. }
  103. }
  104. /************************************************************************************************************************/
  105. #endregion
  106. /************************************************************************************************************************/
  107. #endregion
  108. /************************************************************************************************************************/
  109. #region Methods
  110. /************************************************************************************************************************/
  111. /// <summary>Creates a new <see cref="ClipState"/> and sets its <see cref="Clip"/>.</summary>
  112. /// <exception cref="ArgumentNullException">The `clip` is null.</exception>
  113. public ClipState(AnimationClip clip)
  114. {
  115. Validate.AssertAnimationClip(clip, true, $"create {nameof(ClipState)}");
  116. _Clip = clip;
  117. _Length = clip.length;
  118. _IsLooping = clip.isLooping;
  119. }
  120. /************************************************************************************************************************/
  121. /// <summary>Creates and assigns the <see cref="AnimationClipPlayable"/> managed by this node.</summary>
  122. protected override void CreatePlayable(out Playable playable)
  123. {
  124. playable = AnimationClipPlayable.Create(Graph._PlayableGraph, _Clip);
  125. }
  126. /************************************************************************************************************************/
  127. /// <inheritdoc/>
  128. public override void RecreatePlayable()
  129. {
  130. var playable = (AnimationClipPlayable)_Playable;
  131. var footIK = playable.GetApplyFootIK();
  132. var playableIK = playable.GetApplyPlayableIK();
  133. base.RecreatePlayable();
  134. playable = (AnimationClipPlayable)_Playable;
  135. playable.SetApplyFootIK(footIK);
  136. playable.SetApplyPlayableIK(playableIK);
  137. }
  138. /************************************************************************************************************************/
  139. /// <inheritdoc/>
  140. public override void Destroy()
  141. {
  142. _Clip = null;
  143. base.Destroy();
  144. }
  145. /************************************************************************************************************************/
  146. /// <inheritdoc/>
  147. public override AnimancerState Clone(CloneContext context)
  148. {
  149. var clip = context.GetCloneOrOriginal(_Clip);
  150. var clone = new ClipState(clip);
  151. clone.CopyFrom(this, context);
  152. return clone;
  153. }
  154. /************************************************************************************************************************/
  155. #endregion
  156. /************************************************************************************************************************/
  157. }
  158. }