SpineAnimationStateClipInspector.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated September 24, 2021. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2022, 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.Unity.Playables;
  30. using UnityEditor;
  31. using UnityEngine.Timeline;
  32. namespace Spine.Unity.Editor {
  33. [CustomEditor(typeof(SpineAnimationStateClip))]
  34. [CanEditMultipleObjects]
  35. public class SpineAnimationStateClipInspector : UnityEditor.Editor {
  36. protected SerializedProperty templateProp = null;
  37. protected class ClipInfo {
  38. public TimelineClip timelineClip;
  39. public float previousBlendInDuration = -1.0f;
  40. public float unblendedMixDuration = 0.2f;
  41. }
  42. protected ClipInfo[] clipInfo = null;
  43. public void OnEnable () {
  44. templateProp = serializedObject.FindProperty("template");
  45. System.Array.Resize(ref clipInfo, targets.Length);
  46. for (int i = 0; i < targets.Length; ++i) {
  47. var clip = (SpineAnimationStateClip)targets[i];
  48. clipInfo[i] = new ClipInfo();
  49. clipInfo[i].timelineClip = FindTimelineClip(clip);
  50. }
  51. }
  52. public override void OnInspectorGUI () {
  53. serializedObject.Update();
  54. EditorGUILayout.PropertyField(templateProp);
  55. for (int i = 0; i < targets.Length; ++i) {
  56. var targetClip = (SpineAnimationStateClip)targets[i];
  57. if (targetClip.template.useBlendDuration)
  58. AdjustMixDuration(targetClip, clipInfo[i]);
  59. }
  60. serializedObject.ApplyModifiedProperties();
  61. }
  62. protected void AdjustMixDuration (SpineAnimationStateClip targetClip, ClipInfo timelineClipInfo) {
  63. if (timelineClipInfo == null)
  64. return;
  65. var timelineClip = timelineClipInfo.timelineClip;
  66. if (timelineClip == null)
  67. return;
  68. float blendInDur = System.Math.Max((float)timelineClip.blendInDuration, (float)timelineClip.easeInDuration);
  69. bool isBlendingNow = blendInDur > 0;
  70. bool wasBlendingBefore = timelineClipInfo.previousBlendInDuration > 0;
  71. if (isBlendingNow) {
  72. if (!wasBlendingBefore) {
  73. timelineClipInfo.unblendedMixDuration = targetClip.template.mixDuration;
  74. }
  75. targetClip.template.mixDuration = blendInDur;
  76. EditorUtility.SetDirty(targetClip);
  77. } else if (wasBlendingBefore) {
  78. targetClip.template.mixDuration = timelineClipInfo.unblendedMixDuration;
  79. EditorUtility.SetDirty(targetClip);
  80. }
  81. timelineClipInfo.previousBlendInDuration = blendInDur;
  82. }
  83. protected TimelineClip FindTimelineClip (SpineAnimationStateClip targetClip) {
  84. string[] guids = AssetDatabase.FindAssets("t:TimelineAsset");
  85. foreach (string guid in guids) {
  86. TimelineAsset timeline = (TimelineAsset)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(TimelineAsset));
  87. foreach (var track in timeline.GetOutputTracks()) {
  88. foreach (var clip in track.GetClips()) {
  89. if (clip.asset.GetType() == typeof(SpineAnimationStateClip) && object.ReferenceEquals(clip.asset, targetClip)) {
  90. return clip;
  91. }
  92. }
  93. }
  94. }
  95. return null;
  96. }
  97. }
  98. }