/// <summary> /// Resets the animation and updates it progress by the given time /// It's handy to use this function when an animation repeats or is in a sequence /// </summary> /// <param name="lateTime">Late time</param> public void Reset(float lateTime) { bool oldFinished = Finished; ProgressRaw = Mathf.Clamp(Duration == 0.0f ? 1.0f : Speed * lateTime / Duration, 0.0f, 1.0f); Progress = EasingExtensions.Apply(Easing, ProgressRaw); Finished = ProgressRaw == 1.0f; UpdateValue(); if (Finished && !oldFinished && (OnComplete != null)) { OnComplete(this, 0.0f); } }
/// <summary> /// Updates the animation /// </summary> /// <param name="time">Elapsed time</param> /// <param name="forceUpdate">When set to false, checks if the old and new progress are equal, so it avoids doing /// further calculations for nothing</param> public void UpdateProgress(float time, bool forceUpdate = false) { time *= Speed; // If the animation duration is 0 if (Duration == 0.0f) { // Ends it up to avoid zero division if (!Finished) { ProgressRaw = 1.0f; Progress = 1.0f; Finished = true; UpdateValue(); if (OnComplete != null) { OnComplete(this, time); } } } else { // Otherwise, calculates the new progress percentage float newProgress = ProgressRaw + time / Duration; float clampedProgress = Mathf.Clamp(newProgress, 0.0f, 1.0f); // Compare values to avoid further calculations if ((ProgressRaw == clampedProgress) && !forceUpdate) { return; } // Assigns the new progress and calculates the new values ProgressRaw = clampedProgress; Progress = EasingExtensions.Apply(Easing, ProgressRaw); Finished = ProgressRaw == 1.0f; UpdateValue(); if ((Finished) && (OnComplete != null)) { OnComplete(this, (newProgress - 1.0f) * Duration); } } }