ProgressCurve.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using UnityEngine;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. /// <summary>
  6. /// ProgressCurve provides a helper for creating curves for easing.
  7. /// In some respects it works like an AnimationCurve except that ProgressCurve
  8. /// always takes in a normalized AnimationCurve and a second parameter
  9. /// defines the length of the animation.
  10. ///
  11. /// A few helper methods are provided to track progress through the animation.
  12. /// </summary>
  13. [Serializable]
  14. public class ProgressCurve
  15. {
  16. [SerializeField]
  17. private AnimationCurve _animationCurve;
  18. [SerializeField]
  19. private float _animationLength;
  20. private float _animationStartTime;
  21. public float AnimationLength => _animationLength;
  22. public ProgressCurve()
  23. {
  24. _animationCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
  25. _animationLength = 1.0f;
  26. }
  27. public ProgressCurve(AnimationCurve animationCurve, float animationLength)
  28. {
  29. _animationCurve = animationCurve;
  30. _animationLength = animationLength;
  31. }
  32. public ProgressCurve(ProgressCurve other)
  33. {
  34. Copy(other);
  35. }
  36. public void Copy(ProgressCurve other)
  37. {
  38. _animationCurve = other._animationCurve;
  39. _animationLength = other._animationLength;
  40. _animationStartTime = other._animationStartTime;
  41. }
  42. public void Start()
  43. {
  44. _animationStartTime = Time.time;
  45. }
  46. public float Progress()
  47. {
  48. if (_animationLength <= 0f)
  49. {
  50. return _animationCurve.Evaluate(1.0f);
  51. }
  52. float normalizedTimeProgress = Mathf.Clamp01(ProgressTime() / _animationLength);
  53. return _animationCurve.Evaluate(normalizedTimeProgress);
  54. }
  55. public float ProgressIn(float time)
  56. {
  57. if (_animationLength <= 0f)
  58. {
  59. return _animationCurve.Evaluate(1.0f);
  60. }
  61. float normalizedTimeProgress = Mathf.Clamp01((ProgressTime() + time) / _animationLength);
  62. return _animationCurve.Evaluate(normalizedTimeProgress);
  63. }
  64. public float ProgressTime()
  65. {
  66. return Mathf.Clamp(Time.time - _animationStartTime, 0f, _animationLength);
  67. }
  68. public void End()
  69. {
  70. _animationStartTime = Time.time - _animationLength;
  71. }
  72. }
  73. }