CubismFadeStateObserver.cs 7.8 KB

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