TimelineExtensions.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 UnityEngine;
  30. using System.Collections.Generic;
  31. using System.Collections;
  32. namespace Spine.Unity.AnimationTools {
  33. public static class TimelineExtensions {
  34. /// <summary>Evaluates the resulting value of a TranslateTimeline at a given time.
  35. /// SkeletonData can be accessed from Skeleton.Data or from SkeletonDataAsset.GetSkeletonData.
  36. /// If no SkeletonData is given, values are computed relative to setup pose instead of local-absolute.</summary>
  37. public static Vector2 Evaluate (this TranslateTimeline timeline, float time, SkeletonData skeletonData = null) {
  38. const int PREV_TIME = -3, PREV_X = -2, PREV_Y = -1;
  39. const int X = 1, Y = 2;
  40. var frames = timeline.frames;
  41. if (time < frames[0]) return Vector2.zero;
  42. float x, y;
  43. if (time >= frames[frames.Length - TranslateTimeline.ENTRIES]) { // Time is after last frame.
  44. x = frames[frames.Length + PREV_X];
  45. y = frames[frames.Length + PREV_Y];
  46. }
  47. else {
  48. // Interpolate between the previous frame and the current frame.
  49. int frame = Animation.BinarySearch(frames, time, TranslateTimeline.ENTRIES);
  50. x = frames[frame + PREV_X];
  51. y = frames[frame + PREV_Y];
  52. float frameTime = frames[frame];
  53. float percent = timeline.GetCurvePercent(frame / TranslateTimeline.ENTRIES - 1,
  54. 1 - (time - frameTime) / (frames[frame + PREV_TIME] - frameTime));
  55. x += (frames[frame + X] - x) * percent;
  56. y += (frames[frame + Y] - y) * percent;
  57. }
  58. Vector2 xy = new Vector2(x, y);
  59. if (skeletonData == null) {
  60. return xy;
  61. }
  62. else {
  63. var boneData = skeletonData.bones.Items[timeline.boneIndex];
  64. return xy + new Vector2(boneData.x, boneData.y);
  65. }
  66. }
  67. /// <summary>Gets the translate timeline for a given boneIndex.
  68. /// You can get the boneIndex using SkeletonData.FindBoneIndex.
  69. /// The root bone is always boneIndex 0.
  70. /// This will return null if a TranslateTimeline is not found.</summary>
  71. public static TranslateTimeline FindTranslateTimelineForBone (this Animation a, int boneIndex) {
  72. foreach (var timeline in a.timelines) {
  73. if (timeline.GetType().IsSubclassOf(typeof(TranslateTimeline)))
  74. continue;
  75. var translateTimeline = timeline as TranslateTimeline;
  76. if (translateTimeline != null && translateTimeline.boneIndex == boneIndex)
  77. return translateTimeline;
  78. }
  79. return null;
  80. }
  81. }
  82. }