Curve.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4. namespace UnityUIPlayables
  5. {
  6. [Serializable]
  7. public class Curve
  8. {
  9. [SerializeField] private CurveType _curveType;
  10. [SerializeField] [EnabledIf(nameof(_curveType), (int) CurveType.Easing)]
  11. private EaseType _easeType;
  12. [NormalizedAnimationCurve(false)]
  13. [EnabledIf(nameof(_curveType), (int) CurveType.AnimationCurve)]
  14. [SerializeField]
  15. private AnimationCurve _animationCurve;
  16. public float Evaluate(float progress)
  17. {
  18. Assert.IsTrue(progress >= 0.0f);
  19. Assert.IsTrue(progress <= 1.0f);
  20. switch (_curveType)
  21. {
  22. case CurveType.Easing:
  23. return Easings.Interpolate(progress, _easeType);
  24. case CurveType.AnimationCurve:
  25. return _animationCurve.Evaluate(progress);
  26. default:
  27. throw new ArgumentOutOfRangeException();
  28. }
  29. }
  30. internal static Curve CreateFromEasing(EaseType easeType)
  31. {
  32. return new Curve
  33. {
  34. _curveType = CurveType.Easing,
  35. _easeType = easeType
  36. };
  37. }
  38. internal static Curve CreateFromAnimationCurve(AnimationCurve animationCurve)
  39. {
  40. return new Curve
  41. {
  42. _curveType = CurveType.AnimationCurve,
  43. _animationCurve = animationCurve
  44. };
  45. }
  46. }
  47. }