SkeletonAnimationHandleExample.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System.Collections.Generic;
  30. using UnityEngine;
  31. namespace Spine.Unity.Examples {
  32. // This is an example of an animation handle. This is implemented with strings as state names.
  33. // Strings can serve as the identifier when Mecanim is used as the state machine and state source.
  34. // If you don't use Mecanim, using custom ScriptableObjects may be a more efficient way to store information about the state and its connection with specific Spine animations.
  35. // This animation handle implementation also comes with a dummy implementation of transition-handling.
  36. public class SkeletonAnimationHandleExample : MonoBehaviour {
  37. public SkeletonAnimation skeletonAnimation;
  38. public List<StateNameToAnimationReference> statesAndAnimations = new List<StateNameToAnimationReference>();
  39. public List<AnimationTransition> transitions = new List<AnimationTransition>(); // Alternately, an AnimationPair-Animation Dictionary (commented out) can be used for more efficient lookups.
  40. [System.Serializable]
  41. public class StateNameToAnimationReference {
  42. public string stateName;
  43. public AnimationReferenceAsset animation;
  44. }
  45. [System.Serializable]
  46. public class AnimationTransition {
  47. public AnimationReferenceAsset from;
  48. public AnimationReferenceAsset to;
  49. public AnimationReferenceAsset transition;
  50. }
  51. //readonly Dictionary<Spine.AnimationStateData.AnimationPair, Spine.Animation> transitionDictionary = new Dictionary<AnimationStateData.AnimationPair, Animation>(Spine.AnimationStateData.AnimationPairComparer.Instance);
  52. public Spine.Animation TargetAnimation { get; private set; }
  53. void Awake () {
  54. // Initialize AnimationReferenceAssets
  55. foreach (var entry in statesAndAnimations) {
  56. entry.animation.Initialize();
  57. }
  58. foreach (var entry in transitions) {
  59. entry.from.Initialize();
  60. entry.to.Initialize();
  61. entry.transition.Initialize();
  62. }
  63. // Build Dictionary
  64. //foreach (var entry in transitions) {
  65. // transitionDictionary.Add(new AnimationStateData.AnimationPair(entry.from.Animation, entry.to.Animation), entry.transition.Animation);
  66. //}
  67. }
  68. /// <summary>Sets the horizontal flip state of the skeleton based on a nonzero float. If negative, the skeleton is flipped. If positive, the skeleton is not flipped.</summary>
  69. public void SetFlip (float horizontal) {
  70. if (horizontal != 0) {
  71. skeletonAnimation.Skeleton.ScaleX = horizontal > 0 ? 1f : -1f;
  72. }
  73. }
  74. /// <summary>Plays an animation based on the state name.</summary>
  75. public void PlayAnimationForState (string stateShortName, int layerIndex) {
  76. PlayAnimationForState(StringToHash(stateShortName), layerIndex);
  77. }
  78. /// <summary>Plays an animation based on the hash of the state name.</summary>
  79. public void PlayAnimationForState (int shortNameHash, int layerIndex) {
  80. var foundAnimation = GetAnimationForState(shortNameHash);
  81. if (foundAnimation == null)
  82. return;
  83. PlayNewAnimation(foundAnimation, layerIndex);
  84. }
  85. /// <summary>Gets a Spine Animation based on the state name.</summary>
  86. public Spine.Animation GetAnimationForState (string stateShortName) {
  87. return GetAnimationForState(StringToHash(stateShortName));
  88. }
  89. /// <summary>Gets a Spine Animation based on the hash of the state name.</summary>
  90. public Spine.Animation GetAnimationForState (int shortNameHash) {
  91. var foundState = statesAndAnimations.Find(entry => StringToHash(entry.stateName) == shortNameHash);
  92. return (foundState == null) ? null : foundState.animation;
  93. }
  94. /// <summary>Play an animation. If a transition animation is defined, the transition is played before the target animation being passed.</summary>
  95. public void PlayNewAnimation (Spine.Animation target, int layerIndex) {
  96. Spine.Animation transition = null;
  97. Spine.Animation current = null;
  98. current = GetCurrentAnimation(layerIndex);
  99. if (current != null)
  100. transition = TryGetTransition(current, target);
  101. if (transition != null) {
  102. skeletonAnimation.AnimationState.SetAnimation(layerIndex, transition, false);
  103. skeletonAnimation.AnimationState.AddAnimation(layerIndex, target, true, 0f);
  104. } else {
  105. skeletonAnimation.AnimationState.SetAnimation(layerIndex, target, true);
  106. }
  107. this.TargetAnimation = target;
  108. }
  109. /// <summary>Play a non-looping animation once then continue playing the state animation.</summary>
  110. public void PlayOneShot (Spine.Animation oneShot, int layerIndex) {
  111. var state = skeletonAnimation.AnimationState;
  112. state.SetAnimation(0, oneShot, false);
  113. var transition = TryGetTransition(oneShot, TargetAnimation);
  114. if (transition != null)
  115. state.AddAnimation(0, transition, false, 0f);
  116. state.AddAnimation(0, this.TargetAnimation, true, 0f);
  117. }
  118. Spine.Animation TryGetTransition (Spine.Animation from, Spine.Animation to) {
  119. foreach (var transition in transitions) {
  120. if (transition.from.Animation == from && transition.to.Animation == to) {
  121. return transition.transition.Animation;
  122. }
  123. }
  124. return null;
  125. //Spine.Animation foundTransition = null;
  126. //transitionDictionary.TryGetValue(new AnimationStateData.AnimationPair(from, to), out foundTransition);
  127. //return foundTransition;
  128. }
  129. Spine.Animation GetCurrentAnimation (int layerIndex) {
  130. var currentTrackEntry = skeletonAnimation.AnimationState.GetCurrent(layerIndex);
  131. return (currentTrackEntry != null) ? currentTrackEntry.Animation : null;
  132. }
  133. int StringToHash (string s) {
  134. return Animator.StringToHash(s);
  135. }
  136. }
  137. }