SkeletonAnimationExt.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. namespace Spine.Unity
  2. {
  3. public static class SkeletonAnimationExt
  4. {
  5. /// <summary>
  6. /// 当前动作的时间
  7. /// </summary>
  8. /// <param name="animation"></param>
  9. /// <returns>单位: 秒</returns>
  10. public static float GetDuration(this SkeletonAnimation animation)
  11. {
  12. if (!animation.valid)
  13. {
  14. return 0;
  15. }
  16. else
  17. {
  18. TrackEntry entry = animation.state.GetCurrent(0);
  19. return entry?.Animation.Duration ?? 0;
  20. }
  21. }
  22. /// <summary>
  23. /// 当前动作的时间
  24. /// </summary>
  25. /// <param name="animation"></param>
  26. /// <returns>单位: 毫秒</returns>
  27. public static int GetDurationMS(this SkeletonAnimation animation)
  28. {
  29. var duration = GetDuration(animation);
  30. return UnityEngine.Mathf.CeilToInt(duration * 1000);
  31. }
  32. /// <summary>
  33. /// 指定动作的持续时间
  34. /// </summary>
  35. /// <param name="animation"></param>
  36. /// <param name="aniName">动作名字</param>
  37. /// <returns>单位: 秒</returns>
  38. public static float GetDuration(this SkeletonAnimation animation, string aniName)
  39. {
  40. var animationObject = animation.skeletonDataAsset.GetSkeletonData(false).FindAnimation(aniName);
  41. return animationObject.Duration;
  42. }
  43. }
  44. }