SkeletonAnimationInspector.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using UnityEditor;
  31. using UnityEngine;
  32. using Spine;
  33. namespace Spine.Unity.Editor {
  34. [CustomEditor(typeof(SkeletonAnimation))]
  35. [CanEditMultipleObjects]
  36. public class SkeletonAnimationInspector : SkeletonRendererInspector {
  37. protected SerializedProperty animationName, loop, timeScale, autoReset;
  38. protected bool wasAnimationNameChanged;
  39. protected bool requireRepaint;
  40. readonly GUIContent LoopLabel = new GUIContent("Loop", "Whether or not .AnimationName should loop. This only applies to the initial animation specified in the inspector, or any subsequent Animations played through .AnimationName. Animations set through state.SetAnimation are unaffected.");
  41. readonly GUIContent TimeScaleLabel = new GUIContent("Time Scale", "The rate at which animations progress over time. 1 means normal speed. 0.5 means 50% speed.");
  42. protected override void OnEnable () {
  43. base.OnEnable();
  44. animationName = serializedObject.FindProperty("_animationName");
  45. loop = serializedObject.FindProperty("loop");
  46. timeScale = serializedObject.FindProperty("timeScale");
  47. }
  48. protected override void DrawInspectorGUI (bool multi) {
  49. base.DrawInspectorGUI(multi);
  50. if (!TargetIsValid) return;
  51. bool sameData = SpineInspectorUtility.TargetsUseSameData(serializedObject);
  52. if (multi) {
  53. foreach (var o in targets)
  54. TrySetAnimation(o, multi);
  55. EditorGUILayout.Space();
  56. if (!sameData) {
  57. #if UNITY_5_3_OR_NEWER
  58. EditorGUILayout.DelayedTextField(animationName);
  59. #else
  60. animationName.stringValue = EditorGUILayout.TextField(animationName.displayName, animationName.stringValue);
  61. #endif
  62. } else {
  63. EditorGUI.BeginChangeCheck();
  64. EditorGUILayout.PropertyField(animationName);
  65. wasAnimationNameChanged |= EditorGUI.EndChangeCheck(); // Value used in the next update.
  66. }
  67. EditorGUILayout.PropertyField(loop);
  68. EditorGUILayout.PropertyField(timeScale);
  69. foreach (var o in targets) {
  70. var component = o as SkeletonAnimation;
  71. component.timeScale = Mathf.Max(component.timeScale, 0);
  72. }
  73. } else {
  74. TrySetAnimation(target, multi);
  75. EditorGUILayout.Space();
  76. EditorGUI.BeginChangeCheck();
  77. EditorGUILayout.PropertyField(animationName);
  78. wasAnimationNameChanged |= EditorGUI.EndChangeCheck(); // Value used in the next update.
  79. EditorGUILayout.PropertyField(loop, LoopLabel);
  80. EditorGUILayout.PropertyField(timeScale, TimeScaleLabel);
  81. var component = (SkeletonAnimation)target;
  82. component.timeScale = Mathf.Max(component.timeScale, 0);
  83. EditorGUILayout.Space();
  84. }
  85. if (!isInspectingPrefab) {
  86. if (requireRepaint) {
  87. SceneView.RepaintAll();
  88. requireRepaint = false;
  89. }
  90. }
  91. }
  92. protected void TrySetAnimation (Object o, bool multi) {
  93. var skeletonAnimation = o as SkeletonAnimation;
  94. if (skeletonAnimation == null) return;
  95. if (!skeletonAnimation.valid)
  96. return;
  97. if (!isInspectingPrefab) {
  98. if (wasAnimationNameChanged) {
  99. if (!Application.isPlaying) {
  100. if (skeletonAnimation.state != null) skeletonAnimation.state.ClearTrack(0);
  101. skeletonAnimation.skeleton.SetToSetupPose();
  102. }
  103. Spine.Animation animationToUse = skeletonAnimation.skeleton.Data.FindAnimation(animationName.stringValue);
  104. if (!Application.isPlaying) {
  105. if (animationToUse != null) animationToUse.PoseSkeleton(skeletonAnimation.Skeleton, 0f);
  106. skeletonAnimation.Update(0);
  107. skeletonAnimation.LateUpdate();
  108. requireRepaint = true;
  109. } else {
  110. if (animationToUse != null)
  111. skeletonAnimation.state.SetAnimation(0, animationToUse, loop.boolValue);
  112. else
  113. skeletonAnimation.state.ClearTrack(0);
  114. }
  115. wasAnimationNameChanged = false;
  116. }
  117. // Reflect animationName serialized property in the inspector even if SetAnimation API was used.
  118. if (!multi && Application.isPlaying) {
  119. TrackEntry current = skeletonAnimation.state.GetCurrent(0);
  120. if (current != null) {
  121. if (skeletonAnimation.AnimationName != animationName.stringValue)
  122. animationName.stringValue = current.Animation.Name;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }