CubismFadeStateObserver.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. if (animator.gameObject.name == "kp_zuochi2")
  112. {
  113. }
  114. var fadeController = animator.gameObject.GetComponent<CubismFadeController>();
  115. // Fail silently...
  116. if (fadeController == null)
  117. {
  118. return;
  119. }
  120. _cubismFadeMotionList = fadeController.CubismFadeMotionList;
  121. _layerIndex = layerIndex;
  122. _layerWeight = (_layerIndex == 0)
  123. ? 1.0f
  124. : animator.GetLayerWeight(_layerIndex);
  125. var animatorClipInfo = controller.GetNextAnimatorClipInfo(layerIndex);
  126. _isDefaulState = (animatorClipInfo.Length == 0);
  127. if (_isDefaulState)
  128. {
  129. // Get the motion of Default State only for the first time.
  130. animatorClipInfo = controller.GetCurrentAnimatorClipInfo(layerIndex);
  131. }
  132. // Set playing motions end time.
  133. if ((_playingMotions.Count > 0) && (_playingMotions[_playingMotions.Count - 1].Motion != null))
  134. {
  135. var motion = _playingMotions[_playingMotions.Count - 1];
  136. var time = Time.time;
  137. var newEndTime = time + motion.Motion.FadeOutTime;
  138. motion.EndTime = newEndTime;
  139. while (motion.IsLooping)
  140. {
  141. if ((motion.StartTime + motion.Motion.MotionLength) >= time)
  142. {
  143. break;
  144. }
  145. motion.StartTime += motion.Motion.MotionLength;
  146. }
  147. _playingMotions[_playingMotions.Count - 1] = motion;
  148. }
  149. for (var i = 0; i < animatorClipInfo.Length; ++i)
  150. {
  151. CubismFadePlayingMotion playingMotion;
  152. var instanceId = -1;
  153. var events = animatorClipInfo[i].clip.events;
  154. for (var k = 0; k < events.Length; ++k)
  155. {
  156. if (events[k].functionName != "InstanceId")
  157. {
  158. continue;
  159. }
  160. instanceId = events[k].intParameter;
  161. break;
  162. }
  163. var motionIndex = -1;
  164. for (var j = 0; j < _cubismFadeMotionList.MotionInstanceIds.Length; ++j)
  165. {
  166. if (_cubismFadeMotionList.MotionInstanceIds[j] != instanceId)
  167. {
  168. continue;
  169. }
  170. motionIndex = j;
  171. break;
  172. }
  173. if (animator.gameObject.name == "kp_zuochi2")
  174. {
  175. Debug.Log("zoya000:" + motionIndex);
  176. Debug.Log("zoya111:" + _cubismFadeMotionList.CubismFadeMotionObjects[motionIndex]);
  177. }
  178. playingMotion.Motion = (motionIndex == -1)
  179. ? null
  180. : _cubismFadeMotionList.CubismFadeMotionObjects[motionIndex];
  181. Debug.Log("zoya:" + playingMotion.Motion);
  182. playingMotion.Speed = 1.0f;
  183. playingMotion.StartTime = Time.time;
  184. playingMotion.FadeInStartTime = Time.time;
  185. playingMotion.EndTime = (playingMotion.Motion.MotionLength <= 0)
  186. ? -1
  187. : playingMotion.StartTime + playingMotion.Motion.MotionLength;
  188. playingMotion.IsLooping = animatorClipInfo[i].clip.isLooping;
  189. playingMotion.Weight = 0.0f;
  190. _playingMotions.Add(playingMotion);
  191. }
  192. }
  193. /// <summary>
  194. /// Called by Unity.
  195. /// </summary>
  196. /// <param name="animator">Animator.</param>
  197. /// <param name="stateInfo">Animator state info.</param>
  198. /// <param name="layerIndex">Index of the layer.</param>
  199. public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  200. {
  201. _isStateTransitionFinished = true;
  202. }
  203. #endregion
  204. }
  205. }