EditorSkeletonPlayer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #if UNITY_EDITOR
  30. using UnityEditor;
  31. using UnityEditor.Callbacks;
  32. using UnityEngine;
  33. namespace Spine.Unity {
  34. /// <summary>
  35. /// Experimental Editor Skeleton Player component enabling Editor playback of the
  36. /// selected animation outside of Play mode for SkeletonAnimation and SkeletonGraphic.
  37. /// </summary>
  38. [ExecuteInEditMode]
  39. [AddComponentMenu("Spine/EditorSkeletonPlayer")]
  40. [RequireComponent(typeof(ISkeletonAnimation))]
  41. public class EditorSkeletonPlayer : MonoBehaviour {
  42. public bool playWhenSelected = true;
  43. public bool playWhenDeselected = true;
  44. public float fixedTrackTime = 0.0f;
  45. private IEditorSkeletonWrapper skeletonWrapper;
  46. private TrackEntry trackEntry;
  47. private string oldAnimationName;
  48. private bool oldLoop;
  49. private double oldTime;
  50. [DidReloadScripts]
  51. private static void OnReloaded () {
  52. // Force start when scripts are reloaded
  53. EditorSkeletonPlayer[] editorSpineAnimations = FindObjectsOfType<EditorSkeletonPlayer>();
  54. foreach (EditorSkeletonPlayer editorSpineAnimation in editorSpineAnimations)
  55. editorSpineAnimation.Start();
  56. }
  57. private void Reset () {
  58. // Note: when a skeleton has a varying number of active materials,
  59. // we're moving this component first in the hierarchy to still be
  60. // able to disable this component.
  61. for (int i = 0; i < 10; ++i)
  62. UnityEditorInternal.ComponentUtility.MoveComponentUp(this);
  63. }
  64. private void Start () {
  65. if (Application.isPlaying) return;
  66. if (skeletonWrapper == null) {
  67. SkeletonAnimation skeletonAnimation;
  68. SkeletonGraphic skeletonGraphic;
  69. if (skeletonAnimation = this.GetComponent<SkeletonAnimation>())
  70. skeletonWrapper = new SkeletonAnimationWrapper(skeletonAnimation);
  71. else if (skeletonGraphic = this.GetComponent<SkeletonGraphic>())
  72. skeletonWrapper = new SkeletonGraphicWrapper(skeletonGraphic);
  73. }
  74. oldTime = EditorApplication.timeSinceStartup;
  75. EditorApplication.update += EditorUpdate;
  76. }
  77. private void OnDestroy () {
  78. EditorApplication.update -= EditorUpdate;
  79. }
  80. private void Update () {
  81. if (enabled == false || Application.isPlaying) return;
  82. if (skeletonWrapper == null) return;
  83. if (skeletonWrapper.State == null || skeletonWrapper.State.Tracks.Count == 0) return;
  84. TrackEntry currentEntry = skeletonWrapper.State.Tracks.Items[0];
  85. if (currentEntry != null && fixedTrackTime != 0) {
  86. currentEntry.TrackTime = fixedTrackTime;
  87. }
  88. }
  89. private void EditorUpdate () {
  90. if (enabled == false || Application.isPlaying) return;
  91. if (skeletonWrapper == null) return;
  92. if (skeletonWrapper.State == null) return;
  93. bool isSelected = Selection.Contains(this.gameObject);
  94. if (!this.playWhenSelected && isSelected) return;
  95. if (!this.playWhenDeselected && !isSelected) return;
  96. if (fixedTrackTime != 0) return;
  97. // Update animation
  98. if (oldAnimationName != skeletonWrapper.AnimationName || oldLoop != skeletonWrapper.Loop) {
  99. SkeletonData skeletonData = skeletonWrapper.SkeletonData;
  100. Spine.Animation animation = (skeletonData == null || skeletonWrapper.AnimationName == null) ?
  101. null : skeletonData.FindAnimation(skeletonWrapper.AnimationName);
  102. if (animation != null)
  103. trackEntry = skeletonWrapper.State.SetAnimation(0, skeletonWrapper.AnimationName, skeletonWrapper.Loop);
  104. else
  105. trackEntry = skeletonWrapper.State.SetEmptyAnimation(0, 0);
  106. oldAnimationName = skeletonWrapper.AnimationName;
  107. oldLoop = skeletonWrapper.Loop;
  108. }
  109. // Update speed
  110. if (trackEntry != null)
  111. trackEntry.TimeScale = skeletonWrapper.Speed;
  112. float deltaTime = (float)(EditorApplication.timeSinceStartup - oldTime);
  113. skeletonWrapper.Update(deltaTime);
  114. oldTime = EditorApplication.timeSinceStartup;
  115. // Force repaint to update animation smoothly
  116. #if UNITY_2017_2_OR_NEWER
  117. EditorApplication.QueuePlayerLoopUpdate();
  118. #else
  119. SceneView.RepaintAll();
  120. #endif
  121. }
  122. private class SkeletonAnimationWrapper : IEditorSkeletonWrapper {
  123. private SkeletonAnimation skeletonAnimation;
  124. public SkeletonAnimationWrapper (SkeletonAnimation skeletonAnimation) {
  125. this.skeletonAnimation = skeletonAnimation;
  126. }
  127. public Spine.SkeletonData SkeletonData {
  128. get {
  129. if (!skeletonAnimation.SkeletonDataAsset) return null;
  130. return skeletonAnimation.SkeletonDataAsset.GetSkeletonData(true);
  131. }
  132. }
  133. public string AnimationName { get { return skeletonAnimation.AnimationName; } }
  134. public bool Loop { get { return skeletonAnimation.loop; } }
  135. public float Speed { get { return skeletonAnimation.timeScale; } }
  136. public Spine.AnimationState State { get { return skeletonAnimation.state; } }
  137. public void Update (float deltaTime) {
  138. skeletonAnimation.Update(deltaTime);
  139. }
  140. }
  141. private class SkeletonGraphicWrapper : IEditorSkeletonWrapper {
  142. private SkeletonGraphic skeletonGraphic;
  143. public SkeletonGraphicWrapper (SkeletonGraphic skeletonGraphic) {
  144. this.skeletonGraphic = skeletonGraphic;
  145. }
  146. public Spine.SkeletonData SkeletonData { get { return skeletonGraphic.SkeletonData; } }
  147. public string AnimationName { get { return skeletonGraphic.startingAnimation; } }
  148. public bool Loop { get { return skeletonGraphic.startingLoop; } }
  149. public float Speed { get { return skeletonGraphic.timeScale; } }
  150. public Spine.AnimationState State { get { return skeletonGraphic.AnimationState; } }
  151. public void Update (float deltaTime) {
  152. skeletonGraphic.Update(deltaTime);
  153. }
  154. }
  155. private interface IEditorSkeletonWrapper {
  156. string AnimationName { get; }
  157. Spine.SkeletonData SkeletonData { get; }
  158. bool Loop { get; }
  159. float Speed { get; }
  160. Spine.AnimationState State { get; }
  161. void Update (float deltaTime);
  162. }
  163. }
  164. }
  165. #endif