SkeletonAnimationMulti.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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 (var s in skeletonAnimations)
  53. Destroy(s.gameObject);
  54. skeletonAnimations.Clear();
  55. animationNameTable.Clear();
  56. animationSkeletonTable.Clear();
  57. }
  58. void SetActiveSkeleton (SkeletonAnimation skeletonAnimation) {
  59. foreach (var sa in skeletonAnimations)
  60. sa.gameObject.SetActive(sa == skeletonAnimation);
  61. currentSkeletonAnimation = skeletonAnimation;
  62. }
  63. #region Lifecycle
  64. void Awake () {
  65. Initialize(false);
  66. }
  67. #endregion
  68. #region API
  69. public Dictionary<Animation, SkeletonAnimation> AnimationSkeletonTable { get { return this.animationSkeletonTable; } }
  70. public Dictionary<string, Animation> AnimationNameTable { get { return this.animationNameTable; } }
  71. public SkeletonAnimation CurrentSkeletonAnimation { get { return this.currentSkeletonAnimation; } }
  72. public void Initialize (bool overwrite) {
  73. if (skeletonAnimations.Count != 0 && !overwrite) return;
  74. Clear();
  75. var settings = this.meshGeneratorSettings;
  76. Transform thisTransform = this.transform;
  77. foreach (var sda in skeletonDataAssets) {
  78. var sa = SkeletonAnimation.NewSkeletonAnimationGameObject(sda);
  79. sa.transform.SetParent(thisTransform, false);
  80. sa.SetMeshSettings(settings);
  81. sa.initialFlipX = this.initialFlipX;
  82. sa.initialFlipY = this.initialFlipY;
  83. var skeleton = sa.skeleton;
  84. skeleton.ScaleX = this.initialFlipX ? -1 : 1;
  85. skeleton.ScaleY = this.initialFlipY ? -1 : 1;
  86. sa.Initialize(false);
  87. skeletonAnimations.Add(sa);
  88. }
  89. // Build cache
  90. var animationNameTable = this.animationNameTable;
  91. var animationSkeletonTable = this.animationSkeletonTable;
  92. foreach (var skeletonAnimation in skeletonAnimations) {
  93. foreach (var animationObject in skeletonAnimation.Skeleton.Data.Animations) {
  94. animationNameTable[animationObject.Name] = animationObject;
  95. animationSkeletonTable[animationObject] = skeletonAnimation;
  96. }
  97. }
  98. SetActiveSkeleton(skeletonAnimations[0]);
  99. SetAnimation(initialAnimation, initialLoop);
  100. }
  101. public Animation FindAnimation (string animationName) {
  102. // Analysis disable once LocalVariableHidesMember
  103. Animation animation;
  104. animationNameTable.TryGetValue(animationName, out animation);
  105. return animation;
  106. }
  107. public TrackEntry SetAnimation (string animationName, bool loop) {
  108. return SetAnimation(FindAnimation(animationName), loop);
  109. }
  110. public TrackEntry SetAnimation (Animation animation, bool loop) {
  111. if (animation == null) return null;
  112. SkeletonAnimation skeletonAnimation;
  113. animationSkeletonTable.TryGetValue(animation, out skeletonAnimation);
  114. if (skeletonAnimation != null) {
  115. SetActiveSkeleton(skeletonAnimation);
  116. skeletonAnimation.skeleton.SetToSetupPose();
  117. var trackEntry = skeletonAnimation.state.SetAnimation(MainTrackIndex, animation, loop);
  118. skeletonAnimation.Update(0);
  119. return trackEntry;
  120. }
  121. return null;
  122. }
  123. public void SetEmptyAnimation (float mixDuration) {
  124. currentSkeletonAnimation.state.SetEmptyAnimation(MainTrackIndex, mixDuration);
  125. }
  126. public void ClearAnimation () {
  127. currentSkeletonAnimation.state.ClearTrack(MainTrackIndex);
  128. }
  129. public TrackEntry GetCurrent () {
  130. return currentSkeletonAnimation.state.GetCurrent(MainTrackIndex);
  131. }
  132. #endregion
  133. }
  134. }