CubismFadeStateObserver.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. _layerIndex = layerIndex;
  119. _layerWeight = (_layerIndex == 0)
  120. ? 1.0f
  121. : animator.GetLayerWeight(_layerIndex);
  122. var animatorClipInfo = controller.GetNextAnimatorClipInfo(layerIndex);
  123. _isDefaulState = (animatorClipInfo == null || animatorClipInfo.Length == 0);
  124. if (_isDefaulState)
  125. {
  126. // Get the motion of Default State only for the first time.
  127. animatorClipInfo = controller.GetCurrentAnimatorClipInfo(layerIndex);
  128. }
  129. // Set playing motions end time.
  130. if ((_playingMotions.Count > 0) && (_playingMotions[_playingMotions.Count - 1].Motion != null))
  131. {
  132. var motion = _playingMotions[_playingMotions.Count - 1];
  133. var time = Time.time;
  134. var newEndTime = time + motion.Motion.FadeOutTime;
  135. motion.EndTime = newEndTime;
  136. while (motion.IsLooping)
  137. {
  138. if ((motion.StartTime + motion.Motion.MotionLength) >= time)
  139. {
  140. break;
  141. }
  142. motion.StartTime += motion.Motion.MotionLength;
  143. }
  144. _playingMotions[_playingMotions.Count - 1] = motion;
  145. }
  146. for (var i = 0; i < animatorClipInfo.Length; ++i)
  147. {
  148. CubismFadePlayingMotion playingMotion;
  149. var instanceId = -1;
  150. var events = animatorClipInfo[i].clip.events;
  151. for (var k = 0; k < events.Length; ++k)
  152. {
  153. if (events[k].functionName != "InstanceId")
  154. {
  155. continue;
  156. }
  157. instanceId = events[k].intParameter;
  158. break;
  159. }
  160. var motionIndex = -1;
  161. for (var j = 0; j < _cubismFadeMotionList.MotionInstanceIds.Length; ++j)
  162. {
  163. if (_cubismFadeMotionList.MotionInstanceIds[j] != instanceId)
  164. {
  165. continue;
  166. }
  167. motionIndex = j;
  168. break;
  169. }
  170. playingMotion.Motion = (motionIndex == -1)
  171. ? null
  172. : _cubismFadeMotionList.CubismFadeMotionObjects[motionIndex];
  173. playingMotion.Speed = 1.0f;
  174. playingMotion.StartTime = Time.time;
  175. playingMotion.FadeInStartTime = Time.time;
  176. if(playingMotion.Motion == null)
  177. {
  178. continue;
  179. }
  180. playingMotion.EndTime = (playingMotion.Motion.MotionLength <= 0)
  181. ? -1
  182. : playingMotion.StartTime + playingMotion.Motion.MotionLength;
  183. playingMotion.IsLooping = animatorClipInfo[i].clip.isLooping;
  184. playingMotion.Weight = 0.0f;
  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. }