Unity Dotween官方案例學習


  本文只涉及一些案例,具體查看 DoTween 官方文檔

 

一、 Basics

 1 public class Basics : MonoBehaviour
 2 {
 3     public Transform redCube, greenCube, blueCube, purpleCube;
 4 
 5     IEnumerator Start()
 6     {
 7         // Start after one second delay (to ignore Unity hiccups when activating Play mode in Editor)
 8         yield return new WaitForSeconds(1);
 9 
10         // 2秒時間移動到 0,4,0
11         redCube.DOMove(new Vector3(0,4,0), 2);
12 
13         // 2秒時間從 0,4,0 移動到原始位置
14         greenCube.DOMove(new Vector3(0,4,0), 2).From();
15         
16         // 2秒時間移動 0,4,0 相對位置
17         blueCube.DOMove(new Vector3(0,4,0), 2).SetRelative();
18 
19         // 2秒時間移動 6,0,0 相對位置
20         purpleCube.DOMove(new Vector3(6,0,0), 2).SetRelative();
21         // 2秒內將顏色變為黃色,並且循環往復一直執行
22         purpleCube.GetComponent<Renderer>().material.DOColor(Color.yellow, 2).SetLoops(-1, LoopType.Yoyo);
23     }
24 }

 

 

 

   該場景主要涉及對一些 Unity 組件(transform, material)屬性的變換,當然我們也可以對其他一些組件(Audio, Camera, Light, Rigidbody, ...)進行操作。

 

二、Follow

 1 public class Follow : MonoBehaviour
 2 {
 3     public Transform target; // Target to follow
 4     Vector3 targetLastPos;
 5     Tweener tween;
 6 
 7     void Start()
 8     {
 9         // 啟動后先移動到目標位置,保存 Tweener 並且設置不自動銷毀
10         tween = transform.DOMove(target.position, 2).SetAutoKill(false);
11         // 存儲上一目標位置
12         targetLastPos = target.position;
13     }
14 
15     void Update()
16     {
17         // 目標沒有移動
18         if (targetLastPos == target.position) return;
19         // 修改目標位置,重新開始動畫
20         tween.ChangeEndValue(target.position, true).Restart();
21         // 保存目標位置,用於下次比較
22         targetLastPos = target.position;
23     }
24 }

  該場景實現目標跟隨,當目標物移動的時候,跟隨物體會相應移動。其中涉及到 Tweener 的設置與控制。

 

三、Materials

 1 public class Materials : MonoBehaviour
 2 {
 3     public GameObject target;
 4     public Color toColor;
 5 
 6     Tween colorTween, emissionTween, offsetTween;
 7 
 8     void Start()
 9     {
10         // 獲取材質組件
11         Material mat = target.GetComponent<Renderer>().material;
12 
13         // 改變材質顏色,動畫默認狀態為暫停
14         colorTween = mat.DOColor(toColor, 1).SetLoops(-1, LoopType.Yoyo).Pause();
15 
16         // 修改材質光線發散顏色,注意屬性 _EmissionColor
17         emissionTween = mat.DOColor(new Color(0, 0, 0, 0), "_EmissionColor", 1).SetLoops(-1, LoopType.Yoyo).Pause();
18 
19         // 修改材質偏移,動畫變化為線性,持續增加
20         offsetTween = mat.DOOffset(new Vector2(1, 1), 1).SetEase(Ease.Linear).SetLoops(-1, LoopType.Incremental).Pause();
21     }
22 
23     // Toggle methods (called by UI events)
24 
25     public void ToggleColor()
26     {
27         // 切換動畫狀態,播放或者暫停
28         colorTween.TogglePause();
29     }
30 
31     public void ToggleEmission()
32     {
33         emissionTween.TogglePause();
34     }
35 
36     public void ToggleOffset()
37     {
38         offsetTween.TogglePause();
39     }
40 }

  該場景主要實現 material 的動畫效果。其中 SetEase 方法設置動畫的屬性變化方式(線性,拋物線等,就是變化速度)。

  

 四、Paths

 1 public class Paths : MonoBehaviour
 2 {
 3     public Transform target;
 4     public PathType pathType = PathType.CatmullRom;
 5     // 路徑
 6     public Vector3[] waypoints = new[] {
 7         new Vector3(4, 2, 6),
 8         new Vector3(8, 6, 14),
 9         new Vector3(4, 6, 14),
10         new Vector3(0, 6, 6),
11         new Vector3(-3, 0, 0)
12     };
13 
14     void Start()
15     {
16         // 按路徑運動
17         // 使用 SetOptions 函數使路徑封閉
18         // 使用 SetLookAt 函數使物體朝向路徑本身
19         Tween t = target.DOPath(waypoints, 4, pathType)
20             .SetOptions(true)
21             .SetLookAt(0.001f);
22         // 線性變化且無限循環
23         t.SetEase(Ease.Linear).SetLoops(-1);
24     }
25 }

  該場景實現了物體按路徑運動動畫。

  

 五、Sequences

 1 public class Sequences : MonoBehaviour
 2 {
 3     public Transform cube;
 4     public float duration = 4;
 5 
 6     IEnumerator Start()
 7     {
 8         yield return new WaitForSeconds(1);
 9 
10         // 新建一個 Sequence
11         Sequence s = DOTween.Sequence();
12         // 添加一個動畫,持續一個周期
13         s.Append(cube.DOMoveX(6, duration).SetRelative().SetEase(Ease.InOutQuad));
14         // 添加一個動畫,持續半個周期
15         s.Insert(0, cube.DORotate(new Vector3(0, 45, 0), duration / 2).SetEase(Ease.InQuad).SetLoops(2, LoopType.Yoyo));
16         // 添加一個動畫,半個周期時開始,切持續半個周期
17         s.Insert(duration / 2, cube.GetComponent<Renderer>().material.DOColor(Color.yellow, duration / 2));
18         s.SetLoops(-1, LoopType.Yoyo);
19     }
20 }

 

   該場景實現了一個簡單的 Sequences。其中 Append 方法是將動畫加在末尾,而 Insert 方法是可以加到任意位置。

 

 六、UGUI

 1 public class UGUI : MonoBehaviour
 2 {
 3     public Image dotweenLogo, circleOutline;
 4     public Text text, relativeText, scrambledText;
 5     public Slider slider;
 6 
 7     void Start()
 8     {
 9         // Logo 圖片漸漸消失動畫
10         dotweenLogo.DOFade(0, 1.5f).SetAutoKill(false).Pause();
11 
12         // 圖片顏色動畫
13         circleOutline.DOColor(RandomColor(), 1.5f).SetEase(Ease.Linear).Pause();
14         // 圖片部分顯示動畫,結束后按相反方向
15         circleOutline.DOFillAmount(0, 1.5f).SetEase(Ease.Linear).SetLoops(-1, LoopType.Yoyo)
16             .OnStepComplete(()=> {
17                 circleOutline.fillClockwise = !circleOutline.fillClockwise;
18                 circleOutline.DOColor(RandomColor(), 1.5f).SetEase(Ease.Linear);
19             })
20             .Pause();
21 
22         // 文字動畫
23         text.DOText("This text will replace the existing one", 2).SetEase(Ease.Linear).SetAutoKill(false).Pause();
24         relativeText.DOText(" - This text will be added to the existing one", 2).SetRelative().SetEase(Ease.Linear).SetAutoKill(false).Pause();
25         scrambledText.DOText("This text will appear from scrambled chars", 2, true, ScrambleMode.All).SetEase(Ease.Linear).SetAutoKill(false).Pause();
26 
27         // 滑動條動畫
28         slider.DOValue(1, 1.5f).SetEase(Ease.InOutQuad).SetLoops(-1, LoopType.Yoyo).Pause();
29     }
30 
31     // Called by PLAY button OnClick event. Starts all tweens
32     public void StartTweens()
33     {
34         DOTween.PlayAll();
35     }
36 
37     // Called by RESTART button OnClick event. Restarts all tweens
38     public void RestartTweens()
39     {
40         DOTween.RestartAll();
41     }
42 
43     // Returns a random color
44     Color RandomColor()
45     {
46         return new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1);
47     }
48 }

  該場景主要實現了 UGUI 組件的動畫。

 


免責聲明!

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



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