SpineAnimationTesterTool.cs 5.6 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 System.Text;
  34. using UnityEngine;
  35. namespace Spine.Unity.Examples {
  36. public class SpineAnimationTesterTool : MonoBehaviour, IHasSkeletonDataAsset, IHasSkeletonComponent {
  37. public SkeletonAnimation skeletonAnimation;
  38. public SkeletonDataAsset SkeletonDataAsset { get { return skeletonAnimation.SkeletonDataAsset; } }
  39. public ISkeletonComponent SkeletonComponent { get { return skeletonAnimation; } }
  40. public bool useOverrideMixDuration;
  41. public float overrideMixDuration = 0.2f;
  42. public bool useOverrideAttachmentThreshold = true;
  43. [Range(0f, 1f)]
  44. public float attachmentThreshold = 0.5f;
  45. public bool useOverrideDrawOrderThreshold;
  46. [Range(0f, 1f)]
  47. public float drawOrderThreshold = 0.5f;
  48. [System.Serializable]
  49. public struct AnimationControl {
  50. [SpineAnimation]
  51. public string animationName;
  52. public bool loop;
  53. public KeyCode key;
  54. [Space]
  55. public bool useCustomMixDuration;
  56. public float mixDuration;
  57. //public bool useChainToControl;
  58. //public int chainToControl;
  59. }
  60. [System.Serializable]
  61. public class ControlledTrack {
  62. public List<AnimationControl> controls = new List<AnimationControl>();
  63. }
  64. [Space]
  65. public List<ControlledTrack> trackControls = new List<ControlledTrack>();
  66. [Header("UI")]
  67. public UnityEngine.UI.Text boundAnimationsText;
  68. public UnityEngine.UI.Text skeletonNameText;
  69. void OnValidate () {
  70. // Fill in the SkeletonData asset name
  71. if (skeletonNameText != null) {
  72. if (skeletonAnimation != null && skeletonAnimation.skeletonDataAsset != null) {
  73. skeletonNameText.text = SkeletonDataAsset.name.Replace("_SkeletonData", "");
  74. }
  75. }
  76. // Fill in the control list.
  77. if (boundAnimationsText != null) {
  78. var boundAnimationsStringBuilder = new StringBuilder();
  79. boundAnimationsStringBuilder.AppendLine("Animation Controls:");
  80. for (int trackIndex = 0; trackIndex < trackControls.Count; trackIndex++) {
  81. if (trackIndex > 0)
  82. boundAnimationsStringBuilder.AppendLine();
  83. boundAnimationsStringBuilder.AppendFormat("---- Track {0} ---- \n", trackIndex);
  84. foreach (var ba in trackControls[trackIndex].controls) {
  85. string animationName = ba.animationName;
  86. if (string.IsNullOrEmpty(animationName))
  87. animationName = "SetEmptyAnimation";
  88. boundAnimationsStringBuilder.AppendFormat("[{0}] {1}\n", ba.key.ToString(), animationName);
  89. }
  90. }
  91. boundAnimationsText.text = boundAnimationsStringBuilder.ToString();
  92. }
  93. }
  94. void Start () {
  95. if (useOverrideMixDuration) {
  96. skeletonAnimation.AnimationState.Data.DefaultMix = overrideMixDuration;
  97. }
  98. }
  99. void Update () {
  100. var animationState = skeletonAnimation.AnimationState;
  101. // For each track
  102. for (int trackIndex = 0; trackIndex < trackControls.Count; trackIndex++) {
  103. // For each control in the track
  104. foreach (var control in trackControls[trackIndex].controls) {
  105. // Check each control, and play the appropriate animation.
  106. if (Input.GetKeyDown(control.key)) {
  107. TrackEntry trackEntry;
  108. if (!string.IsNullOrEmpty(control.animationName)) {
  109. trackEntry = animationState.SetAnimation(trackIndex, control.animationName, control.loop);
  110. } else {
  111. float mix = control.useCustomMixDuration ? control.mixDuration : animationState.Data.DefaultMix;
  112. trackEntry = animationState.SetEmptyAnimation(trackIndex, mix);
  113. }
  114. if (trackEntry != null) {
  115. if (control.useCustomMixDuration)
  116. trackEntry.MixDuration = control.mixDuration;
  117. if (useOverrideAttachmentThreshold)
  118. trackEntry.AttachmentThreshold = attachmentThreshold;
  119. if (useOverrideDrawOrderThreshold)
  120. trackEntry.DrawOrderThreshold = drawOrderThreshold;
  121. }
  122. // Don't parse more than one animation per track.
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }