using System; using UnityEngine; namespace CommonUnity3D.UGUIAction { /// /// 缩放动作. /// eg: /// ScaleAction scale = new ScaleAction(); /// scale.ScaleX = 1.5f; /// scale.ScaleY = 1.5f; /// scale.Duration = 1; /// scale.ActionEaseType = CommonUnity3D.UGUIAction.EaseType.easeInOutBack; /// scale.ActionFinishCallBack = MoveActionCallBack; /// cvs.AddAction(scale); /// public class ScaleAction : ActionBase { public const string ACTIONTYPE = "ScaleAction"; private float mTargetX = 0.0f; private float mTargetY = 0.0f; private float mStartX = 0.0f; private float mStartY = 0.0f; private float mTotalTime = 0.0f; private float mCurrentTime = 0.0f; private int mCurrentCycle = 0; private bool mReverse = false; private Vector2 mV = Vector2.zero; public float ScaleX { get { return mTargetX; } set { mTargetX = value; } } public float ScaleY { get { return mTargetY; } set { mTargetY = value; } } public float Duration { get { return mTotalTime; } set { mTotalTime = value; } } public override void onUpdate(IActionCompment unit, float deltaTime) { if (deltaTime == 0 || (mCurrentTime == mTotalTime)) return; float previousTime = mCurrentTime; float restTime = mTotalTime - mCurrentTime; float carryOverTime = deltaTime > restTime ? deltaTime - restTime : 0.0f; mCurrentTime = Math.Min(mTotalTime, mCurrentTime + deltaTime); if (mCurrentCycle < 0 && previousTime <= 0 && mCurrentTime > 0) { mCurrentCycle++; } float ratio = mCurrentTime / mTotalTime; bool reversed = mReverse && (mCurrentCycle % 2 == 1); float deltaX = mTargetX - mStartX; float deltaY = mTargetY - mStartY; float transitionValue = 0.0f; if (reversed == true) { transitionValue = EaseManager.EasingFromType(0, 1, (float)(1.0 - ratio), mEaseType); } else { transitionValue = EaseManager.EasingFromType(0, 1, ratio, mEaseType); } float currentValueX = mStartX + transitionValue * deltaX; float currentValueY = mStartY + transitionValue * deltaY; mV.x = currentValueX; mV.y = currentValueY; unit.Scale = mV; if (previousTime < mTotalTime && mCurrentTime >= mTotalTime) { mIsEnd = true; } } public override void onStart(IActionCompment unit) { Vector2 OldScale = unit.Scale; mStartX = OldScale.x; mStartY = OldScale.y; } public override bool IsEnd() { return mIsEnd; } public override string GetActionType() { return ACTIONTYPE; } } }