123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using System;
- using Fort23.Core;
- using UnityEngine;
- using Utility.CTween;
- public class CustomFloatTween : CustomTweener
- {
- public CustomGetter<float> Getter;
- public CustomSetter<float> Setter;
- public float EndValue;
- public float StartValue;
- public float CurValue;
- private GameObject _target;
- public CustomFloatTween CustomInit(CustomGetter<float> getter, CustomSetter<float> setter, float endValue, float duration, GameObject target)
- {
- _target = target;
- _tempTimer = 0;
- _curTime = 0;
- Duration = 0;
- _isPause = false;
- _isKilled = false;
- _isComplete = false;
- Duration = duration;
- Getter = getter;
- Setter = setter;
- EndValue = endValue;
- StartValue = Getter();
- Update();
- return this;
- }
- public override void Update()
- {
- if (_target == null)
- {
- Kill();
- return;
- }
- if (_isPause || _isKilled || _isComplete)
- {
- return;
- }
- _isPlaying = true;
- _curTime += Time.deltaTime;
- float rate = 1 / Duration; //相对速率
- _tempTimer += Time.deltaTime * rate;
- _tempTimer = Mathf.Clamp(_tempTimer, 0, 1);
- float v = AnimationCurve.Evaluate(_tempTimer);
- float pNewValue = Mathf.Lerp(StartValue, EndValue, v);
- CurValue = pNewValue;
- Setter?.Invoke(pNewValue);
- OnUpdateAction?.Invoke();
- OnUpdateAndValueAction?.Invoke(pNewValue);
- if (_curTime >= Duration)
- {
- OnCompleteAction?.Invoke();
- _isPlaying = false;
- _isComplete = true;
- if (AutoDispose)
- {
- Kill();
- }
- }
- }
- public void ReStart(float endValue = -1, float duration = -1)
- {
- _tempTimer = 0;
- _curTime = 0;
- // Duration = 0;
- _isPause = false;
- _isKilled = false;
- _isComplete = false;
- if (Math.Abs(endValue - (-1)) > 0.001f)
- {
- EndValue = endValue;
- }
- if (Math.Abs(duration - (-1)) > 0.001f)
- {
- Duration = duration;
- }
- CustomInit(Getter, Setter, EndValue, Duration, _target);
- }
- public override void Dispose()
- {
- if (_target != null)
- {
- Setter?.Invoke(EndValue);
- OnCompleteAction?.Invoke();
- }
- base.Dispose();
- _target = null;
- Getter = null;
- Setter = null;
- CustomTweenManager.FloatRecycle(this);
- }
- }
|