CustomFloatTween.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using Fort23.Core;
  3. using UnityEngine;
  4. using Utility.CTween;
  5. public class CustomFloatTween : CustomTweener
  6. {
  7. public CustomGetter<float> Getter;
  8. public CustomSetter<float> Setter;
  9. public float EndValue;
  10. public float StartValue;
  11. public float CurValue;
  12. private GameObject _target;
  13. public CustomFloatTween CustomInit(CustomGetter<float> getter, CustomSetter<float> setter, float endValue, float duration, GameObject target)
  14. {
  15. _target = target;
  16. _tempTimer = 0;
  17. _curTime = 0;
  18. Duration = 0;
  19. _isPause = false;
  20. _isKilled = false;
  21. _isComplete = false;
  22. Duration = duration;
  23. Getter = getter;
  24. Setter = setter;
  25. EndValue = endValue;
  26. StartValue = Getter();
  27. Update();
  28. return this;
  29. }
  30. public override void Update()
  31. {
  32. if (_target == null)
  33. {
  34. Kill();
  35. return;
  36. }
  37. if (_isPause || _isKilled || _isComplete)
  38. {
  39. return;
  40. }
  41. _isPlaying = true;
  42. _curTime += Time.deltaTime;
  43. float rate = 1 / Duration; //相对速率
  44. _tempTimer += Time.deltaTime * rate;
  45. _tempTimer = Mathf.Clamp(_tempTimer, 0, 1);
  46. float v = AnimationCurve.Evaluate(_tempTimer);
  47. float pNewValue = Mathf.Lerp(StartValue, EndValue, v);
  48. CurValue = pNewValue;
  49. Setter?.Invoke(pNewValue);
  50. OnUpdateAction?.Invoke();
  51. OnUpdateAndValueAction?.Invoke(pNewValue);
  52. if (_curTime >= Duration)
  53. {
  54. OnCompleteAction?.Invoke();
  55. _isPlaying = false;
  56. _isComplete = true;
  57. if (AutoDispose)
  58. {
  59. Kill();
  60. }
  61. }
  62. }
  63. public void ReStart(float endValue = -1, float duration = -1)
  64. {
  65. _tempTimer = 0;
  66. _curTime = 0;
  67. // Duration = 0;
  68. _isPause = false;
  69. _isKilled = false;
  70. _isComplete = false;
  71. if (Math.Abs(endValue - (-1)) > 0.001f)
  72. {
  73. EndValue = endValue;
  74. }
  75. if (Math.Abs(duration - (-1)) > 0.001f)
  76. {
  77. Duration = duration;
  78. }
  79. CustomInit(Getter, Setter, EndValue, Duration, _target);
  80. }
  81. public override void Dispose()
  82. {
  83. if (_target != null)
  84. {
  85. Setter?.Invoke(EndValue);
  86. OnCompleteAction?.Invoke();
  87. }
  88. base.Dispose();
  89. _target = null;
  90. Getter = null;
  91. Setter = null;
  92. CustomTweenManager.FloatRecycle(this);
  93. }
  94. }