Unity3D之DOTween基礎

一, 情景

     雖然Unity提供了插值的算法,可是想來確實沒有Falsh, Egret, Laya, Cocos Creator等給出的Tween方案來的爽,主要是不夠方便.因此本人仍是強烈建議使用Tween插件, 如DOTween.算法

 

二, 先舉一個小栗子

     1, scene佈置以下:ide

             

    2, 需求以下:函數

         使Sphere(圓球) , 在2S的時間內勻速移動到(2,1,0)的位置oop

 

 

   實現方案:測試

   Ⅰ, DOTween的方案動畫

         代碼掛載到Scripts空物體上,以下:this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class DoTween3D : MonoBehaviour
{
    [SerializeField]
    private GameObject go;//個人小圓球啊


    private Tweener tweener;

    void Start()
    {
        this.tweener = this.go.transform.DOMove(new Vector3(2, 1, 0), 2.0f);//以秒爲單位
        tweener.SetAutoKill(false);
    }

    void Update()
    {

    }
}

 

而且, 圓球是能夠到(2,1,0)的位置的,以下圖lua

 

   Ⅱ, Unity的插值方案spa

        

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lerp3D : MonoBehaviour
{
    [SerializeField]
    private GameObject go;//圓球
    [SerializeField]
    private AnimationCurve curve;
    [SerializeField]
    private float duration = 2.0f;
    [SerializeField]
    private Vector3 targetV3 = new Vector3(2, 1, 0);
    private Vector3 initVe3;
    private float curveX;

    void Awake()
    {
        this.curveX = 0;
    }
    void Start()
    {
        this.initVe3 = this.go.transform.position;
    }

    // 能夠達到目標
    void Update()
    {
        this.curveX += Time.deltaTime / this.duration;
        this.go.transform.position = Vector3.Lerp(this.initVe3, this.targetV3, this.curve.Evaluate(this.curveX));
    }
}

 

感受Tween的想法簡單許多了. 這只是其中一部分插件

 

三, DoTween 經常使用API介紹

    Ⅰ , 注意

           1, 須要引用 using DG.Tweening;

           2, DoMove() 方法修改的是世界座標,要修改局部座標請使用DoLocalMove()方法

           3, DoMoveX()只是修改X軸方向上的移動,其餘的軸上的移動有DoMoveY(),DoMoveZ()

           4, DoPlayForward()正序播放 , DoPlayBackwards()倒序播放

好, 改一下個人scene, 作一個測試,以下

                   4-①, scene佈置

                 4-②, 腳本代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class DoTween3D : MonoBehaviour
{
    [SerializeField]
    private GameObject go;//個人小圓球啊
    [SerializeField]
    private Button button;
    [SerializeField]
    private bool isZhengXu = true;

    private Text buttonTxt;


    private Tweener tweener;

    void Start()
    {
        this.tweener = this.go.transform.DOMove(new Vector3(2, 1, 0), 2.0f);//以秒爲單位
        tweener.SetAutoKill(false);//禁止自動銷燬動畫
        tweener.Pause();//暫停

        this.buttonTxt = this.button.GetComponentInChildren<Text>();
        this.ResetBtnTxt();

        this.AddEvent();
    }

    private void AddEvent()
    {
        this.button.onClick.AddListener(this.OnClickHandler);
    }

    private void RemoveEvent()
    {
        this.button.onClick.RemoveListener(this.OnClickHandler);
    }

    private void OnClickHandler()
    {
        if (this.isZhengXu)
        {
            this.tweener.PlayForward();
        }
        else
        {
            this.tweener.PlayBackwards();
        }
        this.isZhengXu = !this.isZhengXu;
        this.ResetBtnTxt();
    }

    private void ResetBtnTxt()
    {
        if (this.isZhengXu)
        {
            this.buttonTxt.text = "正播";
        }
        else
        {
            this.buttonTxt.text = "倒播";
        }
    }

    void Update()
    {

    }

    void OnDestroy()
    {
        //銷燬緩動
        if (this.tweener != null)
        {
            this.tweener.Kill();
            this.tweener = null;
        }
    }
}

            5,Sequence

                   5-①,至關於一個Tweener的鏈表,能夠經過執行一個Sequence來執行一串Tweener,使用Sequence類能夠方便的組織Tweens來製做複雜的過渡動畫

                   5-②,提供的方法:

                           5-2-①,Append(Tweener tween),加添一個tween

                           5-2-②,AppendCallback(TweenCallback callback),添加一個回調

                           5-2-③,AppendInterval(float interval),添加一段時間間隔

                           5-2-④, Insert(float atPosition, Tweener tween),插入一段緩動(在給定的時間位置上)

                           5-2-⑤,InsertCallback(float atPosition, TweenCallback callback),插入一段回調(在給定的時間位置上)

                           5-2-⑥, Join(Tween tween), 在Sequence的最後一個tween的開始處放置一個tween

                           5-2-⑦,Prepend(Tween tween),在Sequence開始處插入一個tween

                           5-2-⑧,PrependCallback(TweenCallback callback),在Sequence開始處插入一個回調函數

                           5-2-⑨,PrependInterval(float interval),在Sequence開始處插入一段時間間隔

                   5-③,Demo以下

                           

using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

public class Sequence3D : MonoBehaviour
{
    [SerializeField]
    private GameObject go;

    private Sequence sequence;
    void Start()
    {
        this.sequence = DOTween.Sequence();
        this.sequence.Append(this.go.transform.DOMove(new Vector3(2, 1, 0), 2f));//添加一個補間動畫
        this.sequence.Append(this.go.transform.DORotate(new Vector3(90, 90, 0), 2f));//添加一個旋轉動畫(DOMove執行後之執行)
        this.sequence.Insert(0, this.go.transform.DOScale(new Vector3(1.5f, 2, 1), 2));//在0s吃添加一個縮放動畫(並行執行和DOMove,由於DOMove在0s執行)
        this.sequence.PrependInterval(5);//在開始處添加5秒等待,5S後仍是執行DOMove和DOScale
    }

    void Update()
    {

    }

    void OnDestroy()
    {
        if (this.sequence != null)
        {
            this.sequence.Kill();
            this.sequence = null;
        }
    }
}

              效果以下:

                        

    Ⅱ, 經常使用的方法總結:

           1.以DO開頭的方法:補間動畫的方法。例如:Transform.DOMoveX(10,1)

           2.以Set開頭的方法:設置補間動畫的屬性。例如:Tweener.SetLoops(4, LoopType.Yoyo)

           3.以On開頭的方法:補間動畫的回調函數。例如:Tweener.OnStart(callBackFunction)

相關文章
相關標籤/搜索