[DOTween]使用過程中的一些注意事項記錄


代碼環境:

  unity:version5.3.4.f1

       DOTween:v1.0.312

  IDE:Microsoft visual studio Community 2015

 

DOTween中幾個需要注意的變量和函數功能說明:

1 tweens對象完成后會自動銷毀,如此我們基本不用關心DOTween的內存銷毀問題。

        //
        // Summary:
        //     Default autoKillOnComplete behaviour for new tweens.
        //     Default: TRUE
        public static bool defaultAutoKill;

2   構建新的tweens后,會自動play。保持該值,我們則無需要手動調用play

        //
        // Summary:
        //     Default autoPlay behaviour for new tweens.
        //     Default: AutoPlay.All
        public static AutoPlay defaultAutoPlay;

3 默認的運動方式,主要表現是開始執行時,快速,在后期會逐步減速。該算法在執行時間長度比較短時看着比較合理舒適,但是如果出現類似距離較長,時間也相對較長時,就容易發現在后期有點很不好接受的緩慢移動。此時就需要考慮更改運動方式。

        //
        // Summary:
        //     Default ease applied to all new Tweeners (not to Sequences which always have
        //     Ease.Linear as default).
        //     Default: Ease.InOutQuad
        public static Ease defaultEaseType;

        //
        // Summary:
        //     Sets the ease of the tween.
        //     If applied to Sequences eases the whole sequence animation
        public static T SetEase<T>(this T t, Ease ease) where T : Tween;
4 DOTween中有兩套不同的調用方式=》
shortcuts way,但是需要針對不同對象,需要調用不一樣對象:
        //
        // Summary:
        //     Tweens a Transform's localPosition to the given value. Also stores the transform
        //     as the tween's target so it can be used for filtered operations
        //
        // Parameters:
        //   endValue:
        //     The end value to reach
        //
        //   duration:
        //     The duration of the tween
        //
        //   snapping:
        //     If TRUE the tween will smoothly snap all values to integers
        public static Tweener DOLocalMove(this Transform target, Vector3 endValue, float duration, bool snapping = false);

        //
        // Summary:
        //     Tweens a Transform's localRotation to the given value. Also stores the transform
        //     as the tween's target so it can be used for filtered operations
        //
        // Parameters:
        //   endValue:
        //     The end value to reach
        //
        //   duration:
        //     The duration of the tween
        //
        //   mode:
        //     Rotation mode
        public static Tweener DOLocalRotate(this Transform target, Vector3 endValue, float duration, RotateMode mode = RotateMode.Fast);

        //
        // Summary:
        //     Tweens a Transform's localScale to the given value. Also stores the transform
        //     as the tween's target so it can be used for filtered operations
        //
        // Parameters:
        //   endValue:
        //     The end value to reach
        //
        //   duration:
        //     The duration of the tween
        public static Tweener DOScale(this Transform target, Vector3 endValue, float duration);
generic way,大部分的執行都可以用同類型的接口。
如下函數,只要是float類型數值,都可以符合調用要求。我個人使用最多的就是應用在alpha上,處理漸隱漸現效果很好。
        //
        // Summary:
        //     Tweens a virtual property from the given start to the given end value and implements
        //     a setter that allows to use that value with an external method or a lambda
        //     Example:
        //     To(MyMethod, 0, 12, 0.5f);
        //     Where MyMethod is a function that accepts a float parameter (which will be the
        //     result of the virtual tween)
        //
        // Parameters:
        //   setter:
        //     The action to perform with the tweened value
        //
        //   startValue:
        //     The value to start from
        //
        //   endValue:
        //     The end value to reach
        //
        //   duration:
        //     The duration of the virtual tween
        public static Tweener To(DOSetter<float> setter, float startValue, float endValue, float duration);
5 關於sequence
Sequences are like Tweeners, but instead of animating a property or value they animate other Tweeners or Sequences as a group.
比較簡單的Sequence,可以直接用如下函數增加Append / Join:
 //
        // Summary:
        //     Adds the given tween to the end of the Sequence. Has no effect if the Sequence
        //     has already started
        //
        // Parameters:
        //   t:
        //     The tween to append
        public static Sequence Append(this Sequence s, Tween t);

        //
        // Summary:
        //     Inserts the given tween at the same time position of the last tween added to
        //     the Sequence. Has no effect if the Sequence has already started
        public static Sequence Join(this Sequence s, Tween t);

如果是比較復雜的,建議直接使用insert,如此可以隨意的控制加入tween的時間節點

        //
        // Summary:
        //     Inserts the given tween at the given time position in the Sequence, automatically
        //     adding an interval if needed. Has no effect if the Sequence has already started
        //
        // Parameters:
        //   atPosition:
        //     The time position where the tween will be placed
        //
        //   t:
        //     The tween to insert
        public static Sequence Insert(this Sequence s, float atPosition, Tween t);
        //
        // Summary:
        //     Inserts the given callback at the given time position in the Sequence, automatically
        //     adding an interval if needed. Has no effect if the Sequence has already started
        //
        // Parameters:
        //   atPosition:
        //     The time position where the callback will be placed
        //
        //   callback:
        //     The callback to insert
        public static Sequence InsertCallback(this Sequence s, float atPosition, TweenCallback callback);

6 關於Kill函數:特意提出該接口,是因為針對於Sequence,調用DOTween.Kill("Sequence", true),並不能在kill之前complete,不確定是其本身的bug還是我對接口的理解不對。

        //
        // Summary:
        //     Kills all tweens with the given ID or target and returns the number of actual
        //     tweens killed
        //
        // Parameters:
        //   complete:
        //     If TRUE completes the tweens before killing them
        public static int Kill(object targetOrId, bool complete = false);

以下是一些測試例子和執行后的效果圖。

    [ContextMenu("DoTweenAlpha")]
    void DoTweenAlpha()
    {
        Debug.Log("DoTweenAlpha");

        UIRect uiRect = m_uiRectAni;
        if (uiRect != null)
        {
            DOTween.To(x => uiRect.alpha = x, 1.0f, 0.0f, 5.0f).SetId("Tween");
        }
    }

    [ContextMenu("DoTweenKillCompleteAlpha")]
    void DoTweenKillCompleteAlpha()
    {
        Debug.Log("DoTweenKillCompleteAlpha");

        UIRect uiRect = m_uiRectAni;
        if (uiRect != null)
        {
            DOTween.Kill("Tween", true);
        }
    }

    [ContextMenu("DoTweenKillAlpha")]
    void DoTweenKillAlpha()
    {
        Debug.Log("DoTweenKillAlpha");

        UIRect uiRect = m_uiRectAni;
        if (uiRect != null)
        {
            DOTween.Kill("Tween", false);
        }
    }

    [ContextMenu("DoSequenceAlpha")]
    void DoSequenceAlpha()
    {
        Debug.Log("DoSequenceAlpha");

        UIRect uiRect = m_uiRectAni;
        if (uiRect != null)
        {
            Sequence sequence = DOTween.Sequence();
            sequence.Append(DOTween.To(x => uiRect.alpha = x, 1.0f, 0.0f, 5.0f));
            sequence.SetId("Sequence");
        }
    }

    [ContextMenu("DoSequenceKillCompleteAlpha")]
    void DoSequenceKillCompleteAlpha()
    {
        Debug.Log("DoSequenceKillCompleteAlpha");

        UIRect uiRect = m_uiRectAni;
        if (uiRect != null)
        {
            DOTween.Kill("Sequence", true);
        }
    }

    [ContextMenu("DoSequenceKillAlpha")]
    void DoSequenceKillAlpha()
    {
        Debug.Log("DoSequenceKillAlpha");

        UIRect uiRect = m_uiRectAni;
        if (uiRect != null)
        {
            DOTween.Kill("Sequence", false);
        }
    }
執行順序:
DoTweenAlpha->DoTweenKillAlpha(正確kill,並沒有complete);DoTweenAlpha->DoTweenKillCompleteAlpha(正確kill,並成功complete)
DoSequenceAlpha->DoSequenceKillAlpha(正確kill,並沒有complete);DoSequenceAlpha->DoSequenceKillCompleteAlpha(正確kill,並沒有成功complete)
附上一張DoSequenceAlpha->DoSequenceKillCompleteAlpha執行后的效果圖:
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM