TimelineExtensions.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  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 returned as difference to setup pose
  37. /// instead of absolute values.</summary>
  38. public static Vector2 Evaluate (this TranslateTimeline timeline, float time, SkeletonData skeletonData = null) {
  39. if (time < timeline.Frames[0]) return Vector2.zero;
  40. float x, y;
  41. timeline.GetCurveValue(out x, out y, time);
  42. if (skeletonData == null) {
  43. return new Vector2(x, y);
  44. } else {
  45. BoneData boneData = skeletonData.Bones.Items[timeline.BoneIndex];
  46. return new Vector2(boneData.X + x, boneData.Y + y);
  47. }
  48. }
  49. /// <summary>Evaluates the resulting value of a pair of split translate timelines at a given time.
  50. /// SkeletonData can be accessed from Skeleton.Data or from SkeletonDataAsset.GetSkeletonData.
  51. /// If no SkeletonData is given, values are returned as difference to setup pose
  52. /// instead of absolute values.</summary>
  53. public static Vector2 Evaluate (TranslateXTimeline xTimeline, TranslateYTimeline yTimeline,
  54. float time, SkeletonData skeletonData = null) {
  55. float x = 0, y = 0;
  56. if (xTimeline != null && time > xTimeline.Frames[0]) x = xTimeline.GetCurveValue(time);
  57. if (yTimeline != null && time > yTimeline.Frames[0]) y = yTimeline.GetCurveValue(time);
  58. if (skeletonData == null) {
  59. return new Vector2(x, y);
  60. } else {
  61. var bonesItems = skeletonData.Bones.Items;
  62. BoneData boneDataX = bonesItems[xTimeline.BoneIndex];
  63. BoneData boneDataY = bonesItems[yTimeline.BoneIndex];
  64. return new Vector2(boneDataX.X + x, boneDataY.Y + 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. /// <summary>Gets the IBoneTimeline timeline of a given type for a given boneIndex.
  82. /// You can get the boneIndex using SkeletonData.FindBoneIndex.
  83. /// The root bone is always boneIndex 0.
  84. /// This will return null if a timeline of the given type is not found.</summary>
  85. public static T FindTimelineForBone<T> (this Animation a, int boneIndex) where T : class, IBoneTimeline {
  86. foreach (var timeline in a.Timelines) {
  87. T translateTimeline = timeline as T;
  88. if (translateTimeline != null && translateTimeline.BoneIndex == boneIndex)
  89. return translateTimeline;
  90. }
  91. return null;
  92. }
  93. }
  94. }