private void SetupTween(TweenAction tween, int id, float from, float to, float duration, bool pingPong, float waitBetweenTime, TweenAction.TweenMode mode, Action <float> updateCallback, Action readyCallback, Action abortCallback) { tween.tweenId = id; tween.startTime = Time.timeSinceLevelLoad; tween.duration = duration; tween.endTime = tween.startTime + tween.duration; tween.from = from; tween.to = to; tween.pingPong = pingPong; tween.waitBetweenTime = waitBetweenTime; tween.mode = mode; tween.updateCallback = updateCallback ?? delegate { }; tween.readyCallback = readyCallback ?? delegate { }; tween.abortCallback = abortCallback ?? delegate { }; tween.UpdateValue(tween.from); if (gameObject.activeInHierarchy) { tween.coroutine = StartCoroutine(TweeningRoutine(tween)); } else { Debug.LogWarning(name + " is not active, aborting the tween."); tween.Abort(); } }
private IEnumerator TweeningRoutine(TweenAction tween) { tween.isRunning = true; TweenStarted(this, new EventArg <TweenAction>(tween)); while (true) { if (Time.timeSinceLevelLoad >= tween.endTime) { tween.UpdateValue(tween.to); if (tween.pingPong) { yield return(new WaitForSeconds(tween.waitBetweenTime)); float previousTarget = tween.to; tween.to = tween.from; tween.from = previousTarget; tween.startTime = Time.timeSinceLevelLoad; tween.endTime = tween.startTime + tween.duration; } else { break; } } else { tween.lerpTime = Mathf.InverseLerp(tween.startTime, tween.endTime, Time.timeSinceLevelLoad); float lerp = 0; switch (tween.mode) { case TweenAction.TweenMode.Linear: lerp = Mathf.Lerp(tween.from, tween.to, tween.lerpTime); break; case TweenAction.TweenMode.EaseIn: lerp = Mathf.Lerp(tween.from, tween.to, EaseIn(tween.lerpTime)); break; case TweenAction.TweenMode.EaseOut: lerp = Mathf.Lerp(tween.from, tween.to, EaseOut(tween.lerpTime)); break; case TweenAction.TweenMode.Exponential: lerp = Mathf.Lerp(tween.from, tween.to, Exponential(tween.lerpTime)); break; case TweenAction.TweenMode.Smooth: lerp = Mathf.Lerp(tween.from, tween.to, SmoothStep(tween.lerpTime)); break; case TweenAction.TweenMode.Smoother: lerp = Mathf.Lerp(tween.from, tween.to, SmootherStep(tween.lerpTime)); break; default: throw new NotImplementedException("TweenMode not implemented."); } tween.UpdateValue(lerp); } yield return(null); } tween.readyCallback(); tween.isRunning = false; TweenReady(this, new EventArg <TweenAction>(tween)); // Note: do not remove completed tweens, because we lose data (esp. currentValue) }