SkeletonAnimationMulti.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Spine;
  5. using Spine.Unity;
  6. namespace Spine.Unity {
  7. using Animation = Spine.Animation;
  8. using AnimationState = Spine.AnimationState;
  9. public class SkeletonAnimationMulti : MonoBehaviour {
  10. const int MainTrackIndex = 0;
  11. public bool initialFlipX, initialFlipY;
  12. public string initialAnimation;
  13. public bool initialLoop;
  14. [Space]
  15. public List<SkeletonDataAsset> skeletonDataAssets = new List<SkeletonDataAsset>();
  16. [Header("Settings")]
  17. public MeshGenerator.Settings meshGeneratorSettings = MeshGenerator.Settings.Default;
  18. readonly List<SkeletonAnimation> skeletonAnimations = new List<SkeletonAnimation>();
  19. readonly Dictionary<string, Animation> animationNameTable = new Dictionary<string, Animation>();
  20. readonly Dictionary<Animation, SkeletonAnimation> animationSkeletonTable = new Dictionary<Animation, SkeletonAnimation>();
  21. //Stateful
  22. SkeletonAnimation currentSkeletonAnimation;
  23. void Clear () {
  24. foreach (var s in skeletonAnimations)
  25. Destroy(s.gameObject);
  26. skeletonAnimations.Clear();
  27. animationNameTable.Clear();
  28. animationSkeletonTable.Clear();
  29. }
  30. void SetActiveSkeleton (SkeletonAnimation skeletonAnimation) {
  31. foreach (var sa in skeletonAnimations)
  32. sa.gameObject.SetActive(sa == skeletonAnimation);
  33. currentSkeletonAnimation = skeletonAnimation;
  34. }
  35. #region Lifecycle
  36. void Awake () {
  37. Initialize(false);
  38. }
  39. #endregion
  40. #region API
  41. public Dictionary<Animation, SkeletonAnimation> AnimationSkeletonTable { get { return this.animationSkeletonTable; } }
  42. public Dictionary<string, Animation> AnimationNameTable { get { return this.animationNameTable; } }
  43. public SkeletonAnimation CurrentSkeletonAnimation { get { return this.currentSkeletonAnimation; } }
  44. public void Initialize (bool overwrite) {
  45. if (skeletonAnimations.Count != 0 && !overwrite) return;
  46. Clear();
  47. var settings = this.meshGeneratorSettings;
  48. Transform thisTransform = this.transform;
  49. foreach (var sda in skeletonDataAssets) {
  50. var sa = SkeletonAnimation.NewSkeletonAnimationGameObject(sda);
  51. sa.transform.SetParent(thisTransform, false);
  52. sa.SetMeshSettings(settings);
  53. sa.initialFlipX = this.initialFlipX;
  54. sa.initialFlipY = this.initialFlipY;
  55. var skeleton = sa.skeleton;
  56. skeleton.ScaleX = this.initialFlipX ? 1 : -1;
  57. skeleton.ScaleY = this.initialFlipY ? 1 : -1;
  58. sa.Initialize(false);
  59. skeletonAnimations.Add(sa);
  60. }
  61. // Build cache
  62. var animationNameTable = this.animationNameTable;
  63. var animationSkeletonTable = this.animationSkeletonTable;
  64. foreach (var skeletonAnimation in skeletonAnimations) {
  65. foreach (var animationObject in skeletonAnimation.Skeleton.Data.Animations) {
  66. animationNameTable[animationObject.Name] = animationObject;
  67. animationSkeletonTable[animationObject] = skeletonAnimation;
  68. }
  69. }
  70. SetActiveSkeleton(skeletonAnimations[0]);
  71. SetAnimation(initialAnimation, initialLoop);
  72. }
  73. public Animation FindAnimation (string animationName) {
  74. // Analysis disable once LocalVariableHidesMember
  75. Animation animation;
  76. animationNameTable.TryGetValue(animationName, out animation);
  77. return animation;
  78. }
  79. public TrackEntry SetAnimation (string animationName, bool loop) {
  80. return SetAnimation(FindAnimation(animationName), loop);
  81. }
  82. public TrackEntry SetAnimation (Animation animation, bool loop) {
  83. if (animation == null) return null;
  84. SkeletonAnimation skeletonAnimation;
  85. animationSkeletonTable.TryGetValue(animation, out skeletonAnimation);
  86. if (skeletonAnimation != null) {
  87. SetActiveSkeleton(skeletonAnimation);
  88. skeletonAnimation.skeleton.SetToSetupPose();
  89. return skeletonAnimation.state.SetAnimation(MainTrackIndex, animation, loop);
  90. }
  91. return null;
  92. }
  93. public void SetEmptyAnimation (float mixDuration) {
  94. currentSkeletonAnimation.state.SetEmptyAnimation(MainTrackIndex, mixDuration);
  95. }
  96. public void ClearAnimation () {
  97. currentSkeletonAnimation.state.ClearTrack(MainTrackIndex);
  98. }
  99. public TrackEntry GetCurrent () {
  100. return currentSkeletonAnimation.state.GetCurrent(MainTrackIndex);
  101. }
  102. #endregion
  103. }
  104. }