AnimationStateMecanimState.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Spine;
  5. using Spine.Unity;
  6. public class AnimationStateMecanimState : StateMachineBehaviour {
  7. #region Inspector
  8. public AnimationReferenceAsset animation;
  9. [System.Serializable]
  10. public struct AnimationTransition {
  11. public AnimationReferenceAsset from;
  12. public AnimationReferenceAsset transition;
  13. }
  14. [UnityEngine.Serialization.FormerlySerializedAs("transitions")]
  15. public List<AnimationTransition> fromTransitions = new List<AnimationTransition>();
  16. #endregion
  17. Spine.AnimationState state;
  18. public void Initialize (Animator animator) {
  19. if (state == null) {
  20. var animationStateComponent = (animator.GetComponent(typeof(IAnimationStateComponent))) as IAnimationStateComponent;
  21. state = animationStateComponent.AnimationState;
  22. }
  23. }
  24. override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
  25. if (state == null) {
  26. Initialize(animator);
  27. }
  28. float timeScale = stateInfo.speed;
  29. var current = state.GetCurrent(layerIndex);
  30. bool transitionPlayed = false;
  31. if (current != null && fromTransitions.Count > 0) {
  32. foreach (var t in fromTransitions) {
  33. if (t.from.Animation == current.Animation) {
  34. var transitionEntry = state.SetAnimation(layerIndex, t.transition.Animation, false);
  35. transitionEntry.TimeScale = timeScale;
  36. transitionPlayed = true;
  37. break;
  38. }
  39. }
  40. }
  41. TrackEntry trackEntry;
  42. if (transitionPlayed) {
  43. trackEntry = state.AddAnimation(layerIndex, animation.Animation, stateInfo.loop, 0);
  44. } else {
  45. trackEntry = state.SetAnimation(layerIndex, animation.Animation, stateInfo.loop);
  46. }
  47. trackEntry.TimeScale = timeScale;
  48. }
  49. }