You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

117 regels
2.4 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. public abstract class TweenHelper : MonoBehaviour
  5. {
  6. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  7. public event TweenHelper.FinishedTweeningEventHandler FinishedTweeningEvent;
  8. private void FireFinishedTweeningEvent()
  9. {
  10. if (this.FinishedTweeningEvent != null)
  11. {
  12. this.FinishedTweeningEvent();
  13. }
  14. }
  15. private void Awake()
  16. {
  17. this.CurrentTime = this._tweenTime;
  18. this._finishTime = this._tweenTime;
  19. }
  20. private void Update()
  21. {
  22. if (!this.Running)
  23. {
  24. return;
  25. }
  26. this.CurrentTime += Time.deltaTime;
  27. this.UpdateValue(this.CurrentValue);
  28. if (this.CurrentTime >= this._finishTime)
  29. {
  30. this.FireFinishedTweeningEvent();
  31. }
  32. }
  33. public float CurrentTime { get; private set; }
  34. public bool Running
  35. {
  36. get
  37. {
  38. return this.CurrentTime < this._finishTime;
  39. }
  40. }
  41. public decimal CurrentValue
  42. {
  43. get
  44. {
  45. if (Mathf.Approximately(this._finishTime, 0f))
  46. {
  47. return this._goalValue;
  48. }
  49. float num = this._curve.Evaluate(this.CurrentTime / this._finishTime);
  50. if (float.IsNaN(num))
  51. {
  52. UnityEngine.Debug.LogErrorFormat("TweenHelper CurveResult is NaN after evaluating {0} / {1}", new object[]
  53. {
  54. this.CurrentTime,
  55. this._finishTime
  56. });
  57. return this._goalValue;
  58. }
  59. return this._startValue + (decimal)num * (this._goalValue - this._startValue);
  60. }
  61. }
  62. public void TweenTo(decimal goalValue)
  63. {
  64. this.TweenTo(goalValue, this._tweenTime);
  65. }
  66. public void TweenTo(decimal goalValue, float time)
  67. {
  68. this.TweenTo(this.CurrentValue, goalValue, time);
  69. }
  70. public void TweenTo(decimal startValue, decimal goalValue)
  71. {
  72. this.TweenTo(startValue, goalValue, this._tweenTime);
  73. }
  74. public void TweenTo(decimal startValue, decimal goalValue, float time)
  75. {
  76. this._startValue = startValue;
  77. this._goalValue = goalValue;
  78. this._finishTime = time;
  79. if (Mathf.Approximately(time, 0f))
  80. {
  81. this.UpdateValue(this._goalValue);
  82. this.FireFinishedTweeningEvent();
  83. }
  84. else
  85. {
  86. this.CurrentTime = 0f;
  87. }
  88. }
  89. protected abstract void UpdateValue(decimal value);
  90. [SerializeField]
  91. private AnimationCurve _curve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
  92. [SerializeField]
  93. private float _tweenTime = 1f;
  94. private decimal _startValue;
  95. private decimal _goalValue;
  96. private float _finishTime;
  97. public delegate void FinishedTweeningEventHandler();
  98. }