DoTween資料
官方網站:http://dotween.demigiant.com/
下載下傳位址:http://dotween.demigiant.com/download.php
快速開始:http://dotween.demigiant.com/getstarted.php
官方文檔:http://dotween.demigiant.com/documentation.php
開源位址:https://github.com/Demigiant/dotween
一、準備工作
1、下載下傳 DOTween,解壓至Unity的Asset檔案夾,如果你使用的Unity版本低于Unity4.3,請删除所有的 DOTween43檔案
2、在你們建立一個新的腳本時,需要在頂部導入指令空間:
using DG.Tweening;
3、初始化全局選項(可選)
DOTween.Init(autoKillMode, useSafeMode, logBehaviour);
如果你不這樣做(或在你建立第一個Tween之後)DOTween将自動初始化為預設值,但你仍然可以改變它們
二、使用方法
DOTween可以使用 完全通用的方法,像這樣
你也可以使用 便捷的方法,像這樣
無論選擇那種方式,當你建立 tween 都會傳回一個Tweener 或一個序列(兩者的差異),如果你需要儲存,你可以儲存兩者為Tween而無須關心它們的差別。
你可以以不同的方式 控制多個tween,你可以使用static的DOTween方法(有過濾的附加選項)……
// Rewind all tweens 撤消所有的tween
DOTween.Rewind();
// Rewind all tweens with a given id 根據指定id撤消tween
DOTween.Rewind(myId);
直接的tween引用
// Rewind a referenced tween 撤消引用的tween
myTween.Rewind();p>
或更多方式
// Rewind all tweens connected to a specific transform 撤消所有的tween到特定的transform
transform.DORewind();
三、Global或特定設定
你可以設定 global settings 将應用于所有新建立的補間動畫,或通過 具體設定 為每一個單獨的補間動畫。
全局設定
全局設定 允許你設定預設 autoPlay和autoKill 行為,ease type,global timeScale,以及類似的東西。
具體設定
具體設定 是通過鍊配置設定,他們都開始以“Set”開始(除了Callbacks,是以"On"開始”),是以智能感覺幫助你找到他們,這兒有些例子
// Create a transform tween and set its ease, loops and OnComplete callback(建立一個transform的tween,設定type為ease,循環,OnComplete回調)
transform.DOMove(new Vector3(2,2,2), 2).SetEase(Ease.OutQuint).SetLoops(4).OnComplete(myFunction);
// Same as above but using line breaks to make it more readable(同上,但使用換行符,使其更具可讀性)
transform.DOMove(new Vector3(2,2,2), 2)
.SetEase(Ease.OutQuint)
.SetLoops(4)
.OnComplete(myFunction);
// Same as above but storing the tween and applying settings without chaining(同上,但沒有使用鍊的方式)
Tween myTween = transform.DOMove(new Vector3(2,2,2), 2);
myTween.SetEase(Ease.OutQuint);
myTween.SetLoops(4);
myTween.OnComplete(myFunction);
此外一類 tween 類型都有 特殊的附加功能,取決取決于值補間的取決于值補間的類型,它可以通過設定 SetOptions() 隻要記住, SetOptions()是特殊的,當主要建立補間動畫的方法後它需要立即連結:
// Same as the previous examples, but force the transform to
// snap on integer values (very useful for pixel perfect stuff)
//和上面的例子類似,但需要強制transform臨時調整數值(對于像素完美是非常有用的)
transform.DOMove(new Vector3(2,2,2), 2)
.SetOptions(true)
.SetEase(Ease.OutQuint)
.SetLoops(4)
.OnComplete(myFunction);
你甚至可以Copy 一個tween的設定到另一個tween ,使用SetAs() :
// Create a tween with some settings and store it as a Tween
Tween myTween = transform.DOMove(new Vector3(2,2,2), 2)
.SetEase(Ease.OutQuint)
.SetLoops(4)
.OnComplete(myFunction);
// Create another tween and apply the same settings as the previous one(建立另一個tween,并應用上一個tween的設定)
material.DOColor(Color.red, 2).SetAs(myTween);
( 這兩個tween都會循環4次,easeType為OutQuint,當tween完成時都會調用myFunction)
四、Tween的Life(生命周期)
當你建立tween時它會自動播放(除非你設定了全局的 defaultAutoPlay 行為)直到完成循環
當tween完成後它會自動終止(除非你設定了做全局的 defaultAutoKill 行為),這意味着你不能再使用它。
如果你們要重複使用同一個tween,僅僅需要設定它的 autoKill 為 False(可以通過 global autoKill 設定所有的tweens 或為你自己的tween 設定 SetAutoKill(false) ).
如果你的tween的target為null,在tween播放時會報異常,你需要注意或激活 安全模式
五、Cache和tween最大值
如果你有 緩存池 緩存所有的你建立的tween,它們就可以重複使用,不需要建立新的。
此外也可以避免使用更多的資源,它會限制最大200個Tweeners并且50FPS在同一時間,如果你需要更多,DOTween會自動增加它的大小,當然你可以直接設定 來避免自動調整可能會發生卡頓的情況
// Set max Tweeners to 3000 and max Sequences to 200(設定最大Tweeners為300,最大FPS為200)
DOTween.SetTweensCapacity(3000, 200);
在任何時候你想 在tween完成時清除DOTween的cache,你可以調用 DOTween.Clear(),它會kill所有的tweens并釋放所有的緩存.
六、Recycling tween(回收Tween)
你不需要手動回收 tween,相反你可以選擇自動 回收所有的 或 回收特定的
不過,在任何時間你都可以 改變 Tweener的 start和end values