使用DOTween動畫插件來實現物體的移動動畫
Learn
一、DOTween插件對變量的動畫
二、控制Cube和UI面板的動畫
三、動畫的快捷播放方式
四、動畫的前放和后放
五、From Tweens
六、動畫的屬性設置
七、對話框文字動畫
八、震動屏幕效果
九、文本顏色和透明度動畫
Unity項目中導入DoTween動畫插件

一、DOTween插件對變量的動畫
新建Gary場景,在MainCamera攝像機上綁定一個getStart.cs腳本對象,使用Lambda表達式對變量進行變化

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class getStart : MonoBehaviour { public Vector3 myValue = new Vector3(0,0,0); // Use this for initialization void Start () { DOTween.To(()=>myValue,x=> myValue = x,new Vector3(10,10,10),2); } // Update is called once per frame void Update () { } }
二、控制Cube和UI面板的動畫
控制Cube的動畫,Transform從(0,0,0)運動到(10,10,10)
getStart.cs添加Transform引用,綁定Cude對象

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class getStart : MonoBehaviour { public Vector3 myValue = new Vector3(0,0,0); public Transform cubeTransform; // Use this for initialization void Start () { DOTween.To(()=>myValue,x=> myValue = x,new Vector3(10,10,10),2); } // Update is called once per frame void Update () { cubeTransform.position = myValue; } }
控制UI面板動畫,Transform從(500,0,0)運動到(0,0,0)
getStart.cs添加RectTransform引用,綁定Image對象

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class getStart : MonoBehaviour { public Vector3 myValue = new Vector3(0,0,0); public Transform cubeTransform; public RectTransform taskPanelTransform; // Use this for initialization void Start () { DOTween.To(()=>myValue,x=> myValue = x,new Vector3(0,0,0),2); } // Update is called once per frame void Update () { //cubeTransform.position = myValue; //taskPanelTransform.position = myValue; taskPanelTransform.localPosition = myValue; } }
處理三維向量位置值變化
public Vector3 myValue = new Vector3(0,0,0); DOTween.To(()=>myValue,x=> myValue = x,new Vector3(0,0,0),2);
處理Float位置值變化
public float myValue2 = 0; DOTween.To(() =>myValue2, x => myValue2 = x, 10, 2);
三、動畫的快捷播放方式
新建Gary2場景,添加Image、Button組件
點擊Button控件,RectTransform組件從屏幕外(300,0,0)運動到(0,0,0)坐標點
Button控件上綁定MyButton腳本,綁定要移動的組件,添加OnClick()點擊事件

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class MyButton : MonoBehaviour { public RectTransform panelTransform; public void OnClick() { //讓panelTransform從當前位置動畫到(0,0,0)位置時間為1s panelTransform.DOMove(new Vector3(0,0,0),1); panelTransform.DOLocalMove(new Vector3(0,0,0),1); } }
四、動畫的前放和后放
點擊Button控件,RectTransform組件從屏幕外(300,0,0)運動到(0,0,0)坐標點
再次點擊Button控件,RectTransform組件從屏幕(0,0,0)運動到(300,0,0)坐標點

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class MyButton : MonoBehaviour { public RectTransform panelTransform; public bool isIn = false; private void Start() { //默認動畫播放完就會被銷毀 Tweener tweener = panelTransform.DOLocalMove(new Vector3(0, 0, 0), 1); //Tweener對象保存這個動畫的信息,每次調用do類型的方法都會創建一個tween對象,這個對象是dotween對象來管理的 tweener.SetAutoKill(false); //把autokill自動設置為false tweener.Pause(); } public void OnClick() { //點擊第一次時讓RectTransform進入屏幕當中 if (isIn == false) { panelTransform.DOPlayForward(); isIn = true; } else { //讓RectTransform離開屏幕 panelTransform.DOPlayBackwards(); isIn = false; } } }
動畫前放
panelTransform.DOPlayForward();
動畫后放
panelTransform.DOPlayBackwards();
五、From Tweens
新建Gary3場景,添加Cube組件添加MyCube.cs腳本,將Cube對象位置設置為(1,0,0)

Cube從當前位置移動到(5,1)位置后又運行回去(默認From(false))
transform.DOMoveX(5, 1).From();
Cube從當前位置移動到(5+1,1)位置后又運行回去
transform.DOMoveX(5, 1).From(true);
演示From(true)

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class MyCube : MonoBehaviour { // Use this for initialization void Start () { //從當前位置運行到X放 //transform.DOMoveX(5,1); //默認是從當前位置運行到目標位置,加上From()方法以后表示從目標位置移動到當前位置 //transform.DOMoveX(5, 1).From(); transform.DOMoveX(5, 2).From(true); } // Update is called once per frame void Update () { } }
六、動畫的屬性設置
新建Gary4場景,添加Image控件,Image控件下添加MyPanel.cs腳本
物體先向反方向移動一段距離后向前移動
tweener.SetEase(Ease.InBack);

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class MyPanel : MonoBehaviour { // Use this for initialization void Start () { Tweener tweener = transform.DOLocalMoveX(0,2); tweener.SetEase(Ease.InBack); } // Update is called once per frame void Update () { } }
物體彈跳幾次才到達目的點
tweener.SetEase(Ease.InBounce);

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class MyPanel : MonoBehaviour { // Use this for initialization void Start () { Tweener tweener = transform.DOLocalMoveX(0,2); tweener.SetEase(Ease.InBounce); } // Update is called once per frame void Update () { } }
物體到達目的點后進行跳動
weener.SetEase(Ease.OutBounce);

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class MyPanel : MonoBehaviour { // Use this for initialization void Start () { Tweener tweener = transform.DOLocalMoveX(0,2); tweener.SetEase(Ease.OutBounce); } // Update is called once per frame void Update () { } }
Ease屬性及Ease中默認屬性值
#region 程序集 DOTween, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null // F:\1891\DoTween\Gary\Gary_DoTween\Assets\Demigiant\DOTween\DOTween.dll #endregion namespace DG.Tweening { public enum Ease { Unset = 0, Linear = 1, InSine = 2, OutSine = 3, InOutSine = 4, InQuad = 5, OutQuad = 6, InOutQuad = 7, InCubic = 8, OutCubic = 9, InOutCubic = 10, InQuart = 11, OutQuart = 12, InOutQuart = 13, InQuint = 14, OutQuint = 15, InOutQuint = 16, InExpo = 17, OutExpo = 18, InOutExpo = 19, InCirc = 20, OutCirc = 21, InOutCirc = 22, InElastic = 23, OutElastic = 24, InOutElastic = 25, InBack = 26, OutBack = 27, InOutBack = 28, InBounce = 29, OutBounce = 30, InOutBounce = 31, Flash = 32, InFlash = 33, OutFlash = 34, InOutFlash = 35, // // 摘要: // Don't assign this! It's assigned automatically when creating 0 duration tweens INTERNAL_Zero = 36, // // 摘要: // Don't assign this! It's assigned automatically when setting the ease to an AnimationCurve // or to a custom ease function INTERNAL_Custom = 37 } }
其它一些動畫方法
設置動畫循環
tweener.SetLoops(0);
設置動畫監聽事件
tweener.OnComplete(OnTweenComplete);
void OnTweenComplete() { Debug.log("動畫播放完成!"); }
using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class MyPanel : MonoBehaviour { // Use this for initialization void Start () { Tweener tweener = transform.DOLocalMoveX(0,2); tweener.SetEase(Ease.OutBounce); tweener.SetLoops(0); tweener.OnComplete(OnTweenComplete); } // Update is called once per frame void Update () { } void OnTweenComplete() { Debug.log("動畫播放完成!"); } }
七、對話框文字動畫
新建一個場景Gary5,添加Text控件,綁定腳本MyText.cs,控件內容由MyText.cs動態生成

(Text控件中存在文字時,默認會進行覆蓋)
using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using UnityEngine.UI; public class MyText : MonoBehaviour { private Text text; // Use this for initialization void Start () { text = this.GetComponent<Text>(); text.DOText("大家好,我叫Gary!!!",2); } // Update is called once per frame void Update () { } }
八、震動屏幕效果
新建一個場景Gary6,給攝像機綁定MyShakeCamera.cs腳本

(震動完后攝像機還是會回到原位)
using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class MyShakeCamera : MonoBehaviour { // Use this for initialization void Start () { //隨機向走位移動3m的距離 transform.DOShakePosition(3); } // Update is called once per frame void Update () { } }
在x和y軸上震動
transform.DOShakePosition(3,new Vector3(1,1,0));
九、文本顏色和透明度動畫
新建Gary7場景,新建一個Text控件,給Text控件綁定TextColorTween.cs腳本

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; public class TextColorTween : MonoBehaviour { private Text text; // Use this for initialization void Start () { text = GetComponent<Text>(); //設置文字動畫 text.DOColor(Color.red,2); //設置文字透明度 text.DOFade(0,3); } // Update is called once per frame void Update () { } }
設置文字動畫,漸變時間2s
text.DOColor(Color.red,2);
設置文字透明度,漸變時間3s
text.DOFade(0,3);
