Unity DoTween 動畫使用案例


這邊我就直接放一個標准的Dotween動畫的使用demo吧。

這個案例滿足應該可以完成你所想實現的幾乎所有復雜動畫。

 

void PlayTween()
    {
        //set tween data
        float testOneTime = 1.0f;
        float testTwoTime = 1.0f;
        float delayTime = 1.0f;
        Vector3 firstTargetPos = Vector3.one;
        Vector3 secondTargetPos = Vector3.forward;
        TweenCallback completeCallBack = DoEndThing;
        TweenCallback oneCallBack = DoOneThing;
        // play 
        Sequence se = DOTween.Sequence();
        se.Append(mThing.transform.DOMove(Vector3.zero, testOneTime).SetEase(Ease.InOutBounce)); //增加一段動畫
        se.Append(mThing.transform.DOMove(Vector3.one, testTwoTime));
        se.Join(mThing.transform.DOMove(Vector3.forward, testOneTime));//插入一段動畫
        se.AppendInterval(delayTime);//等待一段時間
        se.AppendCallback(oneCallBack);//執行回調 I
        se.Append(mThing.transform.DOMove(Vector3.one, testTwoTime));//再增加一段動畫
        //II :se.AppendCallback(()=> Debug.Log("this is tween callback")); 這是另外一種回調的寫法,匿名方法的形式,就是所謂的lamdon表達式寫法,具體自己去看看吧
        //適用於一句話的回調這樣寫
        //III:se.AppendCallback(()=> { Debug.Log("this is tween callback"); });這也是另外一種寫法,適用於多段回調寫法,
        //總的來說我都不建議這樣寫,我建議我上述沒有注釋的寫法。當然如果是一段話的回調,則采取II寫法,不建議使用III的寫法
        se.OnComplete(completeCallBack); //整個動畫完成之后的回調
        se.Play();
    }

    void DoOneThing()
    {
        Debug.Log("this is one thing to need do");
    }
    void DoEndThing()
    {
        Debug.Log("Do end call backthing");
    }

我們會看到  se.Append(mThing.transform.DOMove(Vector3.zero, testOneTime).SetEase(Ease.InOutBounce)); //增加一段動畫

這句是很經典的一段動畫的設置。

這邊額外解釋一下SetEase方法,設置不同的動畫播放曲線。(是先特別快,然后特別慢,還是先特別慢,然后特別快的設置)

有些同學可能不了解這個動畫曲線啥意思,看英文也不太方便。直接看圖,直觀。

該圖鏈接: http://robertpenner.com/easing/easing_demo.html

 

當然,有這個圖,但是如果這些曲線還是滿足不了我的話,怎么辦。

DoTween 有自定義曲線。

 

[SerializeField]
AnimationCurve selfSetTweenLine;

 

點擊圖中線條,即可進入自定義編輯。

 

 

Append/Join 的區別

 Append 是增加,就是之前的動畫都播放完之后再播放。

 Join 是加入,即現在當然雖然有播放的動畫,但是這個動畫不用等,直接也開始大家一起播放。

 

 

 

Play/PlayForward/PlayBackwards      SetAutoKill 理解

play 默認是SetAutoKill (true)

而如果你用playforward 則必須使用SetAutoKill (false),因為你需要再次開啟,而Dotween默認SetAutoKill (true),所以必須記得設置。

如下:

 Sequence se;
    // Use this for initialization
    void Start () {
        SetTween();
    }

    void SetTween()
    {
        float testOneTime = 1.0f;
        se = DOTween.Sequence();
        se.Append(this.transform.DOMove(Vector3.zero * 100, testOneTime).SetEase(Ease.InOutBounce)); //增加一段動畫
        se.SetAutoKill(false);
        se.Pause(); 
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Q))
        {
            se.PlayForward();
        }
        else if(Input.GetKeyDown(KeyCode.W))
        {
            se.PlayBackwards();
        }
    }
View Code

圖中,你可以嘗試改變SetAutoKill為true,則只會執行一遍動畫,之后的PlayBackwards不會再執行,直接被銷毀了

 

Restart使用  循環重復播放某種動畫實現

  [SerializeField]
    Image image;
    Sequence imageSequence;
	// Use this for initialization
    void Start()
    {
        imageSequence = DOTween.Sequence();
        imageSequence.Append(image.transform.DOScale(1.2f,5f));
        imageSequence.AppendCallback(() => image.transform.localScale = Vector2.one);
        imageSequence.SetAutoKill(false);
        imageSequence.Pause();
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.Q))
        {
            imageSequence.Restart();
        }
  
	}

  這是一個讓一個圖片縮放動畫,受控制,只用一個Play實現。一定要 用Restart。

 

 

補充:

DotweenPath 使用方式。參考鏈接:http://www.bubuko.com/infodetail-2245478.html

注意:編輯 增加一個path的方法,還需要在Scene中鼠標點擊+SHITFT+CTRL。 還有哈,在編譯模式下,用代碼指定tweenPath好像是不行的。所以,推出下面 DoPath的用法。

 

Tween tween = ShortcutExtension.DoPath(this.tranform,wayPoints,3,PathType.CatmulRoom)

tween.Play();

 

最后:DoTween基本的主要知識點就是這些了,沒啥東西了,主要核心東西都講完了。Over

 


免責聲明!

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



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