AnimationBehaviour.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. namespace UnityUIPlayables
  5. {
  6. [Serializable]
  7. public abstract class AnimationBehaviour : PlayableBehaviour
  8. {
  9. [SerializeField] [Tooltip("When set to zero, the length of one loop equals the length of the clip.")]
  10. private float _loopDuration;
  11. [SerializeField] private LoopType _loopType;
  12. [SerializeField] private Curve _curve;
  13. private readonly CurveEvaluateService _evaluateService = new();
  14. public float LoopDuration => _loopDuration;
  15. public LoopType LoopType => _loopType;
  16. public Curve Curve => _curve;
  17. public float EvaluateCurve(float time, float duration)
  18. {
  19. var loopDuration = _loopDuration > 0 ? _loopDuration : duration;
  20. return _loopType switch
  21. {
  22. LoopType.Repeat => _evaluateService.EvaluateRepeat(_curve, time, loopDuration),
  23. LoopType.Reverse => _evaluateService.EvaluateReverse(_curve, time, loopDuration),
  24. LoopType.PingPong => _evaluateService.EvaluatePingPong(_curve, time, loopDuration),
  25. _ => throw new ArgumentOutOfRangeException()
  26. };
  27. }
  28. }
  29. }