示例#1
0
 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();
     }
 }
示例#2
0
 private void StopTween(TweenAction tween)
 {
     if (tween.coroutine != null)
     {
         StopCoroutine(tween.coroutine);
     }
     tween.abortCallback();
     // Note: do not remove completed tweens, because we lose data (esp. currentValue)
 }
示例#3
0
        /// <summary>
        /// Id can be any integer that is not yet used by this Tweener. If a used id is provided, the old tween will be aborted and replaced with the new tween.
        /// From value is optional. If it is not provided, the latest value of the current tween is used as the start value.
        /// </summary>
        /// <param name="tweenId"></param>
        /// <param name="to"></param>
        /// <param name="from"></param>
        /// <param name="duration"></param>
        /// <param name="pingPong"></param>
        /// <param name="waitBetweenTime"></param>
        /// <param name="mode"></param>
        /// <param name="updateCallback"></param>
        /// <param name="readyCallback"></param>
        /// <param name="abortCallback"></param>
        public void TweenTo(int tweenId, float to, float?from = null, float duration = 1, bool pingPong = false, float waitBetweenTime = 1, TweenAction.TweenMode mode = TweenAction.TweenMode.Linear, Action <float> updateCallback = null, Action readyCallback = null, Action abortCallback = null)
        {
            TweenAction tween;

            if (tweens.TryGetValue(tweenId, out tween))
            {
                StopTween(tween);
            }
            else
            {
                tween = new TweenAction();
                tweens.Add(tweenId, tween);
                //Debug.LogFormat("[Tweener] tween added with the id {0}", tweenId);
            }
            from = from ?? tween.currentValue;   // If the start value is not defined, we will use the current value. If the id matches another tween, the value is inherited from it.
            SetupTween(tween, tweenId, from.Value, to, duration, pingPong, waitBetweenTime, mode, updateCallback, readyCallback, abortCallback);
        }
示例#4
0
        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)
        }