SkeletonAnimationMulti.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using Spine;
  30. using Spine.Unity;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using UnityEngine;
  34. namespace Spine.Unity {
  35. using Animation = Spine.Animation;
  36. using AnimationState = Spine.AnimationState;
  37. public class SkeletonAnimationMulti : MonoBehaviour {
  38. const int MainTrackIndex = 0;
  39. public bool initialFlipX, initialFlipY;
  40. public string initialAnimation;
  41. public bool initialLoop;
  42. [Space]
  43. public List<SkeletonDataAsset> skeletonDataAssets = new List<SkeletonDataAsset>();
  44. [Header("Settings")]
  45. public MeshGenerator.Settings meshGeneratorSettings = MeshGenerator.Settings.Default;
  46. readonly List<SkeletonAnimation> skeletonAnimations = new List<SkeletonAnimation>();
  47. readonly Dictionary<string, Animation> animationNameTable = new Dictionary<string, Animation>();
  48. readonly Dictionary<Animation, SkeletonAnimation> animationSkeletonTable = new Dictionary<Animation, SkeletonAnimation>();
  49. //Stateful
  50. SkeletonAnimation currentSkeletonAnimation;
  51. void Clear () {
  52. foreach (SkeletonAnimation skeletonAnimation in skeletonAnimations)
  53. Destroy(skeletonAnimation.gameObject);
  54. skeletonAnimations.Clear();
  55. animationNameTable.Clear();
  56. animationSkeletonTable.Clear();
  57. }
  58. void SetActiveSkeleton (int index) {
  59. if (index < 0 || index >= skeletonAnimations.Count)
  60. SetActiveSkeleton(null);
  61. else
  62. SetActiveSkeleton(skeletonAnimations[index]);
  63. }
  64. void SetActiveSkeleton (SkeletonAnimation skeletonAnimation) {
  65. foreach (SkeletonAnimation iter in skeletonAnimations)
  66. iter.gameObject.SetActive(iter == skeletonAnimation);
  67. currentSkeletonAnimation = skeletonAnimation;
  68. }
  69. #region Lifecycle
  70. void Awake () {
  71. Initialize(false);
  72. }
  73. #endregion
  74. #region API
  75. public Dictionary<Animation, SkeletonAnimation> AnimationSkeletonTable { get { return this.animationSkeletonTable; } }
  76. public Dictionary<string, Animation> AnimationNameTable { get { return this.animationNameTable; } }
  77. public SkeletonAnimation CurrentSkeletonAnimation { get { return this.currentSkeletonAnimation; } }
  78. public List<SkeletonAnimation> SkeletonAnimations { get { return skeletonAnimations; } }
  79. public void Initialize (bool overwrite) {
  80. if (skeletonAnimations.Count != 0 && !overwrite) return;
  81. Clear();
  82. MeshGenerator.Settings settings = this.meshGeneratorSettings;
  83. Transform thisTransform = this.transform;
  84. foreach (SkeletonDataAsset dataAsset in skeletonDataAssets) {
  85. SkeletonAnimation newSkeletonAnimation = SkeletonAnimation.NewSkeletonAnimationGameObject(dataAsset);
  86. newSkeletonAnimation.transform.SetParent(thisTransform, false);
  87. newSkeletonAnimation.SetMeshSettings(settings);
  88. newSkeletonAnimation.initialFlipX = this.initialFlipX;
  89. newSkeletonAnimation.initialFlipY = this.initialFlipY;
  90. Skeleton skeleton = newSkeletonAnimation.skeleton;
  91. skeleton.ScaleX = this.initialFlipX ? -1 : 1;
  92. skeleton.ScaleY = this.initialFlipY ? -1 : 1;
  93. newSkeletonAnimation.Initialize(false);
  94. skeletonAnimations.Add(newSkeletonAnimation);
  95. }
  96. // Build cache
  97. Dictionary<string, Animation> animationNameTable = this.animationNameTable;
  98. Dictionary<Animation, SkeletonAnimation> animationSkeletonTable = this.animationSkeletonTable;
  99. foreach (SkeletonAnimation skeletonAnimation in skeletonAnimations) {
  100. foreach (Animation animationObject in skeletonAnimation.Skeleton.Data.Animations) {
  101. animationNameTable[animationObject.Name] = animationObject;
  102. animationSkeletonTable[animationObject] = skeletonAnimation;
  103. }
  104. }
  105. SetActiveSkeleton(skeletonAnimations[0]);
  106. SetAnimation(initialAnimation, initialLoop);
  107. }
  108. public Animation FindAnimation (string animationName) {
  109. Animation animation;
  110. animationNameTable.TryGetValue(animationName, out animation);
  111. return animation;
  112. }
  113. public TrackEntry SetAnimation (string animationName, bool loop) {
  114. return SetAnimation(FindAnimation(animationName), loop);
  115. }
  116. public TrackEntry SetAnimation (Animation animation, bool loop) {
  117. if (animation == null) return null;
  118. SkeletonAnimation skeletonAnimation;
  119. animationSkeletonTable.TryGetValue(animation, out skeletonAnimation);
  120. if (skeletonAnimation != null) {
  121. SetActiveSkeleton(skeletonAnimation);
  122. skeletonAnimation.skeleton.SetToSetupPose();
  123. TrackEntry trackEntry = skeletonAnimation.state.SetAnimation(MainTrackIndex, animation, loop);
  124. skeletonAnimation.Update(0);
  125. return trackEntry;
  126. }
  127. return null;
  128. }
  129. public void SetEmptyAnimation (float mixDuration) {
  130. currentSkeletonAnimation.state.SetEmptyAnimation(MainTrackIndex, mixDuration);
  131. }
  132. public void ClearAnimation () {
  133. currentSkeletonAnimation.state.ClearTrack(MainTrackIndex);
  134. }
  135. public TrackEntry GetCurrent () {
  136. return currentSkeletonAnimation.state.GetCurrent(MainTrackIndex);
  137. }
  138. #endregion
  139. }
  140. }