SpineSkeletonFlipMixerBehaviour.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_2018_1_OR_NEWER
  30. #define PLAYABLE_DIRECTOR_HAS_STOPPED_EVENT
  31. #endif
  32. using Spine.Unity;
  33. using System;
  34. using UnityEngine;
  35. using UnityEngine.Playables;
  36. using UnityEngine.Timeline;
  37. namespace Spine.Unity.Playables {
  38. public class SpineSkeletonFlipMixerBehaviour : PlayableBehaviour {
  39. float originalScaleX, originalScaleY;
  40. float baseScaleX, baseScaleY;
  41. SpinePlayableHandleBase playableHandle;
  42. bool m_FirstFrameHappened;
  43. #if PLAYABLE_DIRECTOR_HAS_STOPPED_EVENT
  44. PlayableDirector director = null;
  45. public override void OnPlayableCreate (Playable playable) {
  46. director = playable.GetGraph().GetResolver() as PlayableDirector;
  47. if (director)
  48. director.stopped += OnDirectorStopped;
  49. }
  50. public override void OnPlayableDestroy (Playable playable) {
  51. if (director)
  52. director.stopped -= OnDirectorStopped;
  53. base.OnPlayableDestroy(playable);
  54. }
  55. void OnDirectorStopped (PlayableDirector obj) {
  56. OnStop();
  57. }
  58. #else
  59. public override void OnGraphStop (Playable playable) {
  60. OnStop();
  61. }
  62. #endif
  63. public override void ProcessFrame (Playable playable, FrameData info, object playerData) {
  64. playableHandle = playerData as SpinePlayableHandleBase;
  65. if (playableHandle == null)
  66. return;
  67. Skeleton skeleton = playableHandle.Skeleton;
  68. if (!m_FirstFrameHappened) {
  69. originalScaleX = skeleton.ScaleX;
  70. originalScaleY = skeleton.ScaleY;
  71. baseScaleX = Mathf.Abs(originalScaleX);
  72. baseScaleY = Mathf.Abs(originalScaleY);
  73. m_FirstFrameHappened = true;
  74. }
  75. int inputCount = playable.GetInputCount();
  76. float totalWeight = 0f;
  77. float greatestWeight = 0f;
  78. int currentInputs = 0;
  79. for (int i = 0; i < inputCount; i++) {
  80. float inputWeight = playable.GetInputWeight(i);
  81. ScriptPlayable<SpineSkeletonFlipBehaviour> inputPlayable = (ScriptPlayable<SpineSkeletonFlipBehaviour>)playable.GetInput(i);
  82. SpineSkeletonFlipBehaviour input = inputPlayable.GetBehaviour();
  83. totalWeight += inputWeight;
  84. if (inputWeight > greatestWeight) {
  85. SetSkeletonScaleFromFlip(skeleton, input.flipX, input.flipY);
  86. greatestWeight = inputWeight;
  87. }
  88. if (!Mathf.Approximately(inputWeight, 0f))
  89. currentInputs++;
  90. }
  91. if (currentInputs != 1 && 1f - totalWeight > greatestWeight) {
  92. skeleton.ScaleX = originalScaleX;
  93. skeleton.ScaleY = originalScaleY;
  94. }
  95. }
  96. public void SetSkeletonScaleFromFlip (Skeleton skeleton, bool flipX, bool flipY) {
  97. skeleton.ScaleX = flipX ? -baseScaleX : baseScaleX;
  98. skeleton.ScaleY = flipY ? -baseScaleY : baseScaleY;
  99. }
  100. public void OnStop () {
  101. m_FirstFrameHappened = false;
  102. if (playableHandle == null)
  103. return;
  104. Skeleton skeleton = playableHandle.Skeleton;
  105. skeleton.ScaleX = originalScaleX;
  106. skeleton.ScaleY = originalScaleY;
  107. }
  108. }
  109. }