using System; using System.Diagnostics; using UnityEngine; public abstract class TweenHelper : MonoBehaviour { //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event TweenHelper.FinishedTweeningEventHandler FinishedTweeningEvent; private void FireFinishedTweeningEvent() { if (this.FinishedTweeningEvent != null) { this.FinishedTweeningEvent(); } } private void Awake() { this.CurrentTime = this._tweenTime; this._finishTime = this._tweenTime; } private void Update() { if (!this.Running) { return; } this.CurrentTime += Time.deltaTime; this.UpdateValue(this.CurrentValue); if (this.CurrentTime >= this._finishTime) { this.FireFinishedTweeningEvent(); } } public float CurrentTime { get; private set; } public bool Running { get { return this.CurrentTime < this._finishTime; } } public decimal CurrentValue { get { if (Mathf.Approximately(this._finishTime, 0f)) { return this._goalValue; } float num = this._curve.Evaluate(this.CurrentTime / this._finishTime); if (float.IsNaN(num)) { UnityEngine.Debug.LogErrorFormat("TweenHelper CurveResult is NaN after evaluating {0} / {1}", new object[] { this.CurrentTime, this._finishTime }); return this._goalValue; } return this._startValue + (decimal)num * (this._goalValue - this._startValue); } } public void TweenTo(decimal goalValue) { this.TweenTo(goalValue, this._tweenTime); } public void TweenTo(decimal goalValue, float time) { this.TweenTo(this.CurrentValue, goalValue, time); } public void TweenTo(decimal startValue, decimal goalValue) { this.TweenTo(startValue, goalValue, this._tweenTime); } public void TweenTo(decimal startValue, decimal goalValue, float time) { this._startValue = startValue; this._goalValue = goalValue; this._finishTime = time; if (Mathf.Approximately(time, 0f)) { this.UpdateValue(this._goalValue); this.FireFinishedTweeningEvent(); } else { this.CurrentTime = 0f; } } protected abstract void UpdateValue(decimal value); [SerializeField] private AnimationCurve _curve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f); [SerializeField] private float _tweenTime = 1f; private decimal _startValue; private decimal _goalValue; private float _finishTime; public delegate void FinishedTweeningEventHandler(); }