using Thousandto.Code.Center; using Thousandto.Core.Base; using UnityEngine; namespace Thousandto.Plugins.Common { //动画基类 public abstract class UIAnimationBase { #region//保护变量 protected UIAnimationType _animtionType; //动画计时器 protected float _animationTimer = 0f; //动画总时间 protected float _duration = 0f; //动画曲线 protected AnimationCurve _animationCurve = null; //是否已经结束 protected bool _isFinish = false; //等待帧数 protected int _waitFrame = 1; #endregion #region//属性 public bool IsFinish { get { return _isFinish; } } public float Duration { get { return _duration; } set { _duration = value; } } //动画曲线 public AnimationCurve AnimationCurve { get { return _animationCurve; } set { _animationCurve = value; } } //操作的对象 public virtual Transform HandleTrans { get { return null; } set { } } #endregion #region//构造函数 public UIAnimationBase(UIAnimationType type) { _animtionType = type; } #endregion #region//公有函数 public void Update() { if (_duration <= 0f) { Stop(); return; } if (_isFinish) { return; } if (HandleTrans == null || HandleTrans.Equals(null)) { Stop(); return; } if(!HandleTrans.gameObject.activeInHierarchy) { //操作对象已经隐藏了,直接结束动画 _animationTimer = _duration; } if (_waitFrame > 0) { --_waitFrame; return; } //忽略减速效果,防止动画穿帮 _animationTimer += Time.deltaTime / (Time.timeScale > 0f ? Time.timeScale : 1f); float lerpValue = Mathf.Clamp01(_animationTimer / _duration); if (_animationCurve != null) { lerpValue = _animationCurve.Evaluate(Mathf.Lerp(0f, _animationCurve.keys[_animationCurve.keys.Length - 1].time, lerpValue)); } Evaluate(lerpValue); if (lerpValue >= 1f) { //动画结束 Stop(); } } #endregion #region//私有函数 public void Start() { if(GameCenter.GameSetting.IsEnabled(Code.Logic.GameSettingKeyCode.EnableUIAnimation)) { _animationTimer = 0f; } else { _animationTimer = Duration; } _isFinish = false; _waitFrame = 1; OnStart(); } public void Stop() { _isFinish = true; OnStop(); } #endregion #region//子类继承 //处理动画 public abstract void Evaluate(float value); //开始 protected abstract void OnStart(); //结束 protected abstract void OnStop(); #endregion } }