CubismFadeStateObserver.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.Animations;
  10. namespace Live2D.Cubism.Framework.MotionFade
  11. {
  12. public class CubismFadeStateObserver : StateMachineBehaviour, ICubismFadeState
  13. {
  14. #region variable
  15. /// <summary>
  16. /// Cubism fade motion list.
  17. /// </summary>
  18. private CubismFadeMotionList _cubismFadeMotionList;
  19. /// <summary>
  20. /// Cubism playing motion list.
  21. /// </summary>
  22. private List<CubismFadePlayingMotion> _playingMotions;
  23. /// <summary>
  24. /// State that attached this is default.
  25. /// </summary>
  26. private bool _isDefaulState;
  27. /// <summary>
  28. /// Layer index that attached this.
  29. /// </summary>
  30. private int _layerIndex;
  31. /// <summary>
  32. /// Weight of layer that attached this.
  33. /// </summary>
  34. private float _layerWeight;
  35. /// <summary>
  36. /// State that attached this is transition finished.
  37. /// </summary>
  38. private bool _isStateTransitionFinished;
  39. #endregion
  40. #region Fade State Interface
  41. /// <summary>
  42. /// Get cubism playing motion list.
  43. /// </summary>
  44. /// <returns>Cubism playing motion list.</returns>
  45. public List<CubismFadePlayingMotion> GetPlayingMotions()
  46. {
  47. return _playingMotions;
  48. }
  49. /// <summary>
  50. /// Is default state.
  51. /// </summary>
  52. /// <returns><see langword="true"/> State is default; <see langword="false"/> otherwise.</returns>
  53. public bool IsDefaultState()
  54. {
  55. return _isDefaulState;
  56. }
  57. /// <summary>
  58. /// Get layer weight.
  59. /// </summary>
  60. /// <returns>Layer weight.</returns>
  61. public float GetLayerWeight()
  62. {
  63. return _layerWeight;
  64. }
  65. /// <summary>
  66. /// Get state transition finished.
  67. /// </summary>
  68. /// <returns><see langword="true"/> State transition is finished; <see langword="false"/> otherwise.</returns>
  69. public bool GetStateTransitionFinished()
  70. {
  71. return _isStateTransitionFinished;
  72. }
  73. /// <summary>
  74. /// Set state transition finished.
  75. /// </summary>
  76. /// <param name="isFinished">State is finished.</param>
  77. public void SetStateTransitionFinished(bool isFinished)
  78. {
  79. _isStateTransitionFinished = isFinished;
  80. }
  81. /// <summary>
  82. /// Stop animation.
  83. /// </summary>
  84. /// <param name="index">Playing motion index.</param>
  85. public void StopAnimation(int index)
  86. {
  87. _playingMotions.RemoveAt(index);
  88. }
  89. #endregion
  90. #region Unity Event Handling
  91. /// <summary>
  92. /// Called by Unity.
  93. /// </summary>
  94. private void OnEnable()
  95. {
  96. _isStateTransitionFinished = false;
  97. if (_playingMotions == null)
  98. {
  99. _playingMotions = new List<CubismFadePlayingMotion>();
  100. }
  101. }
  102. /// <summary>
  103. /// Called by Unity.
  104. /// </summary>
  105. /// <param name="animator">Animator.</param>
  106. /// <param name="stateInfo">Animator state info.</param>
  107. /// <param name="layerIndex">Index of the layer.</param>
  108. /// <param name="controller">Animation controller playable.</param>
  109. public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller)
  110. {
  111. var fadeController = animator.gameObject.GetComponent<CubismFadeController>();
  112. // Fail silently...
  113. if (fadeController == null)
  114. {
  115. return;
  116. }
  117. _cubismFadeMotionList = fadeController.CubismFadeMotionList;
  118. _isStateTransitionFinished = false;
  119. _layerIndex = layerIndex;
  120. _layerWeight = (_layerIndex == 0)
  121. ? 1.0f
  122. : animator.GetLayerWeight(_layerIndex);
  123. var animatorClipInfo = controller.GetNextAnimatorClipInfo(layerIndex);
  124. _isDefaulState = (animatorClipInfo.Length == 0);
  125. if (_isDefaulState)
  126. {
  127. // Get the motion of Default State only for the first time.
  128. animatorClipInfo = controller.GetCurrentAnimatorClipInfo(layerIndex);
  129. }
  130. // Set playing motions end time.
  131. if ((_playingMotions.Count > 0) && (_playingMotions[_playingMotions.Count - 1].Motion != null))
  132. {
  133. var motion = _playingMotions[_playingMotions.Count - 1];
  134. var time = Time.time;
  135. var newEndTime = time + motion.Motion.FadeOutTime;
  136. if (motion.EndTime < 0.0f || newEndTime < motion.EndTime)
  137. {
  138. motion.EndTime = newEndTime;
  139. }
  140. while (motion.IsLooping)
  141. {
  142. if ((motion.StartTime + motion.Motion.MotionLength) >= time)
  143. {
  144. break;
  145. }
  146. motion.StartTime += motion.Motion.MotionLength;
  147. }
  148. _playingMotions[_playingMotions.Count - 1] = motion;
  149. }
  150. for (var i = 0; i < animatorClipInfo.Length; ++i)
  151. {
  152. CubismFadePlayingMotion playingMotion;
  153. var instanceId = -1;
  154. var events = animatorClipInfo[i].clip.events;
  155. for(var k = 0; k < events.Length; ++k)
  156. {
  157. if(events[k].functionName != "InstanceId")
  158. {
  159. continue;
  160. }
  161. instanceId = events[k].intParameter;
  162. break;
  163. }
  164. var motionIndex = -1;
  165. for (var j = 0; j < _cubismFadeMotionList.MotionInstanceIds.Length; ++j)
  166. {
  167. if (_cubismFadeMotionList.MotionInstanceIds[j] != instanceId)
  168. {
  169. continue;
  170. }
  171. motionIndex = j;
  172. break;
  173. }
  174. playingMotion.Motion = (motionIndex == -1)
  175. ? null
  176. : _cubismFadeMotionList.CubismFadeMotionObjects[motionIndex];
  177. playingMotion.Speed = 1.0f;
  178. playingMotion.StartTime = Time.time;
  179. playingMotion.FadeInStartTime = Time.time;
  180. playingMotion.EndTime = -1.0f;
  181. playingMotion.IsLooping = animatorClipInfo[i].clip.isLooping;
  182. playingMotion.Weight = 0.0f;
  183. playingMotion.InstanceId = instanceId;
  184. playingMotion.IsAnimationEndEventInvoked = false;
  185. _playingMotions.Add(playingMotion);
  186. }
  187. }
  188. /// <summary>
  189. /// Called by Unity.
  190. /// </summary>
  191. /// <param name="animator">Animator.</param>
  192. /// <param name="stateInfo">Animator state info.</param>
  193. /// <param name="layerIndex">Index of the layer.</param>
  194. public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  195. {
  196. _isStateTransitionFinished = true;
  197. }
  198. #endregion
  199. }
  200. }