一.簡介
1.DOTween是Unity游戲開發中常用的動畫插件,拓展了Unity原有的常用組件,如Transform\Camera等,使用方式如下:
transform.DOMove(Vector3.one, .5f).SetDelay(1f).SetEase(Ease.Linear).OnComplete(() => { Debug.Log("doMove complete"); });
使用時直接使用組件調用相應的動畫方法(DOMove\DOMoveX等),這些方法的返回值是一個Tweener對象,之后可以調用Tweener對象的各種方法為這個Tweener對象設置動畫延遲\動畫曲線\回調(設置方法設置完成后會將Tweener對象自身返回,所以可以連續使用"."對Tweener進行設置)等.DOTween的這些方法在DOTween的官方文檔中有詳細的說明.
2.DOTweenAnimation組件是DOTweenPro提供的一個動畫組件,添加到游戲物體上進行可視化動畫設置,當然也可以使用代碼添加和設置這個腳本.
3.說明:本文使用的DOTween插件版本是DOTween Pro v0.9.290.
二.DOTween的拓展方法
導入的DOTween插件中DOMove\SetDelay等拓展方法在生成后的動態鏈接庫dll文件中,所以我沒有看到這些方法的具體實現.在DOTween官方文檔中對所有拓展方法根據組件類型進行了分類列舉,包括Unity組件的拓展方法(用於設置動畫類型,返回值為Tweener對象)\Tweener類的拓展方法(用於設置動畫的曲線\延遲\是否循環\各種回調函數等)等,DOTween - Documentation (demigiant.com).
下面總結一下在官方文檔中沒有找到的一些組件拓展方法(也可能在某處有說明,至少我沒有在文檔中搜索到),這些方法可以對動畫播放進行控制.
這些方法都是Component的拓展方法,也就是說所有Unity的組件都能調用,它們主要用於控制調用組件上正在播放或已經播放的DOTween動畫,其中返回值都是int值,代表動畫個數.方法的作用詳見摘要部分.
三.DOTweenAnimation組件:
在讀取腳本源碼的過程中,添加了一些注釋,因此將添加了注釋的腳本粘貼到這里
// Author: Daniele Giardini - http://www.demigiant.com // Created: 2015/03/12 15:55 using System; using System.Collections.Generic; using DG.Tweening.Core; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; #if DOTWEEN_TMP using TMPro; #endif #pragma warning disable 1591 namespace DG.Tweening { /// <summary> /// Attach this to a GameObject to create a tween /// 在游戲物體上添加此腳本從而創建一個動畫(相當於某個游戲物體的DOTween動畫管理器) /// </summary> [AddComponentMenu("DOTween/DOTween Animation")] public class DOTweenAnimation : ABSAnimationComponent { public float delay;//延遲 public float duration = 1;//持續時間,默認1s public Ease easeType = Ease.OutQuad;//曲線類型 public AnimationCurve easeCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));//曲線類型對應的曲線對象 public LoopType loopType = LoopType.Restart;//循環方式 public int loops = 1;//循環次數,默認1次(不循環) public string id = "";//id字符串 public bool isRelative; public bool isFrom;//tween是否是FROM tween標識 public bool isIndependentUpdate = false; public bool autoKill = true;//動畫播放完成后是否自動銷毀標記,默認自動銷毀 public bool isActive = true;//是否激活標記 public bool isValid;//是否有效標記 public Component target;//動畫的目標組件 public DOTweenAnimationType animationType;//動畫類型(顏色淡入淡出動畫\移動動畫\旋轉動畫等) public bool autoPlay = true;//是否自動播放(默認true) public float endValueFloat;//float類型的動畫終值(不同的動畫類型會使用不同類型的動畫終值) public Vector3 endValueV3;//vector3類型的動畫終值 public Color endValueColor = new Color(1, 1, 1, 1);//color類型的動畫終值 public string endValueString = "";//string類型的動畫終值 public Rect endValueRect = new Rect(0, 0, 0, 0);//Rect類型的動畫終值 public bool optionalBool0;//bool類型的模式選擇(不同動畫類型會使用不同類型的模式選擇) public float optionalFloat0;//float類型的模式選擇 public int optionalInt0;//int類型的模式選擇 public RotateMode optionalRotationMode = RotateMode.Fast;//旋轉模式選擇 public ScrambleMode optionalScrambleMode = ScrambleMode.None;// public string optionalString;//string類型的模式選擇 int _playCount = -1; // Used when calling DOPlayNext 播放下一條動畫時使用,用於播放計數(當游戲物體上掛載了多個腳本時可以播放多個動畫,這個變量可以作為當前播放的動畫的指針) #region Unity Methods void Awake() { if (!isActive || !isValid) return;//校驗組件是否激活或有效 //awake時就調用了CreateTween方法,這個方法中創建tween對象時就直接播放了動畫 CreateTween(); } void OnDestroy() { if (tween != null && tween.IsActive()) tween.Kill();//銷毀組件時如果動畫還在播放中,會強行結束這個動畫 tween = null;//置空tween } // Used also by DOTweenAnimationInspector when applying runtime changes and restarting 在DOTweenAnimationInspector(此腳本的編輯器腳本)中申請運行時間改變和重啟時會使用此方法. /// <summary> /// 創建tween,會根據目標組件target的類型和動畫類型直接調用相應的動畫方法並播放動畫 /// </summary> public void CreateTween() { if (target == null) { Debug.LogWarning(string.Format("{0} :: This tween's target is NULL, because the animation was created with a DOTween Pro version older than 0.9.255. To fix this, exit Play mode then simply select this object, and it will update automatically", this.gameObject.name), this.gameObject); return; } Type t = target.GetType(); // Component c; switch (animationType) { case DOTweenAnimationType.None: break; case DOTweenAnimationType.Move: if (t.IsSameOrSubclassOf(typeof(RectTransform))) tween = ((RectTransform)target).DOAnchorPos3D(endValueV3, duration, optionalBool0); else if (t.IsSameOrSubclassOf(typeof(Transform))) tween = ((Transform)target).DOMove(endValueV3, duration, optionalBool0); else if (t.IsSameOrSubclassOf(typeof(Rigidbody2D))) tween = ((Rigidbody2D)target).DOMove(endValueV3, duration, optionalBool0); else if (t.IsSameOrSubclassOf(typeof(Rigidbody))) tween = ((Rigidbody)target).DOMove(endValueV3, duration, optionalBool0); // c = this.GetComponent<Rigidbody2D>(); // if (c != null) { // tween = ((Rigidbody2D)c).DOMove(endValueV3, duration, optionalBool0); // goto SetupTween; // } // c = this.GetComponent<Rigidbody>(); // if (c != null) { // tween = ((Rigidbody)c).DOMove(endValueV3, duration, optionalBool0); // goto SetupTween; // } // c = this.GetComponent<RectTransform>(); // if (c != null) { // tween = ((RectTransform)c).DOAnchorPos3D(endValueV3, duration, optionalBool0); // goto SetupTween; // } // tween = transform.DOMove(endValueV3, duration, optionalBool0); break; case DOTweenAnimationType.LocalMove: tween = transform.DOLocalMove(endValueV3, duration, optionalBool0); break; case DOTweenAnimationType.Rotate: if (t.IsSameOrSubclassOf(typeof(Transform))) tween = ((Transform)target).DORotate(endValueV3, duration, optionalRotationMode); else if (t.IsSameOrSubclassOf(typeof(Rigidbody2D))) tween = ((Rigidbody2D)target).DORotate(endValueFloat, duration); else if (t.IsSameOrSubclassOf(typeof(Rigidbody))) tween = ((Rigidbody)target).DORotate(endValueV3, duration, optionalRotationMode); // c = this.GetComponent<Rigidbody2D>(); // if (c != null) { // tween = ((Rigidbody2D)c).DORotate(endValueFloat, duration); // goto SetupTween; // } // c = this.GetComponent<Rigidbody>(); // if (c != null) { // tween = ((Rigidbody)c).DORotate(endValueV3, duration, optionalRotationMode); // goto SetupTween; // } // tween = transform.DORotate(endValueV3, duration, optionalRotationMode); break; case DOTweenAnimationType.LocalRotate: tween = transform.DOLocalRotate(endValueV3, duration, optionalRotationMode); break; case DOTweenAnimationType.Scale: tween = transform.DOScale(optionalBool0 ? new Vector3(endValueFloat, endValueFloat, endValueFloat) : endValueV3, duration); break; case DOTweenAnimationType.Color: isRelative = false; if (t.IsSameOrSubclassOf(typeof(SpriteRenderer))) tween = ((SpriteRenderer)target).DOColor(endValueColor, duration); else if (t.IsSameOrSubclassOf(typeof(Renderer))) tween = ((Renderer)target).material.DOColor(endValueColor, duration); else if (t.IsSameOrSubclassOf(typeof(Image))) tween = ((Image)target).DOColor(endValueColor, duration); else if (t.IsSameOrSubclassOf(typeof(Text))) tween = ((Text)target).DOColor(endValueColor, duration); #if DOTWEEN_TK2D else if (t.IsSameOrSubclassOf(typeof(tk2dTextMesh))) tween = ((tk2dTextMesh)target).DOColor(endValueColor, duration); else if (t.IsSameOrSubclassOf(typeof(tk2dBaseSprite))) tween = ((tk2dBaseSprite)target).DOColor(endValueColor, duration); // c = this.GetComponent<tk2dBaseSprite>(); // if (c != null) { // tween = ((tk2dBaseSprite)c).DOColor(endValueColor, duration); // goto SetupTween; // } #endif #if DOTWEEN_TMP else if (t.IsSameOrSubclassOf(typeof(TextMeshProUGUI))) tween = ((TextMeshProUGUI)target).DOColor(endValueColor, duration); else if (t.IsSameOrSubclassOf(typeof(TextMeshPro))) tween = ((TextMeshPro)target).DOColor(endValueColor, duration); // c = this.GetComponent<TextMeshPro>(); // if (c != null) { // tween = ((TextMeshPro)c).DOColor(endValueColor, duration); // goto SetupTween; // } // c = this.GetComponent<TextMeshProUGUI>(); // if (c != null) { // tween = ((TextMeshProUGUI)c).DOColor(endValueColor, duration); // goto SetupTween; // } #endif // c = this.GetComponent<SpriteRenderer>(); // if (c != null) { // tween = ((SpriteRenderer)c).DOColor(endValueColor, duration); // goto SetupTween; // } // c = this.GetComponent<Renderer>(); // if (c != null) { // tween = ((Renderer)c).material.DOColor(endValueColor, duration); // goto SetupTween; // } // c = this.GetComponent<Image>(); // if (c != null) { // tween = ((Image)c).DOColor(endValueColor, duration); // goto SetupTween; // } // c = this.GetComponent<Text>(); // if (c != null) { // tween = ((Text)c).DOColor(endValueColor, duration); // goto SetupTween; // } break; case DOTweenAnimationType.Fade: isRelative = false; if (t.IsSameOrSubclassOf(typeof(SpriteRenderer))) tween = ((SpriteRenderer)target).DOFade(endValueFloat, duration); else if (t.IsSameOrSubclassOf(typeof(Renderer))) tween = ((Renderer)target).material.DOFade(endValueFloat, duration); else if (t.IsSameOrSubclassOf(typeof(Image))) tween = ((Image)target).DOFade(endValueFloat, duration); else if (t.IsSameOrSubclassOf(typeof(Text))) tween = ((Text)target).DOFade(endValueFloat, duration); #if DOTWEEN_TK2D else if (t.IsSameOrSubclassOf(typeof(tk2dTextMesh))) tween = ((tk2dTextMesh)target).DOFade(endValueFloat, duration); else if (t.IsSameOrSubclassOf(typeof(tk2dBaseSprite))) tween = ((tk2dBaseSprite)target).DOFade(endValueFloat, duration); // c = this.GetComponent<tk2dBaseSprite>(); // if (c != null) { // tween = ((tk2dBaseSprite)c).DOFade(endValueFloat, duration); // goto SetupTween; // } #endif #if DOTWEEN_TMP else if (t.IsSameOrSubclassOf(typeof(TextMeshProUGUI))) tween = ((TextMeshProUGUI)target).DOFade(endValueFloat, duration); else if (t.IsSameOrSubclassOf(typeof(TextMeshPro))) tween = ((TextMeshPro)target).DOFade(endValueFloat, duration); // c = this.GetComponent<TextMeshPro>(); // if (c != null) { // tween = ((TextMeshPro)c).DOFade(endValueFloat, duration); // goto SetupTween; // } // c = this.GetComponent<TextMeshProUGUI>(); // if (c != null) { // tween = ((TextMeshProUGUI)c).DOFade(endValueFloat, duration); // goto SetupTween; // } #endif // c = this.GetComponent<SpriteRenderer>(); // if (c != null) { // tween = ((SpriteRenderer)c).DOFade(endValueFloat, duration); // goto SetupTween; // } // c = this.GetComponent<Renderer>(); // if (c != null) { // tween = ((Renderer)c).material.DOFade(endValueFloat, duration); // goto SetupTween; // } // c = this.GetComponent<Image>(); // if (c != null) { // tween = ((Image)c).DOFade(endValueFloat, duration); // goto SetupTween; // } // c = this.GetComponent<Text>(); // if (c != null) { // tween = ((Text)c).DOFade(endValueFloat, duration); // goto SetupTween; // } break; case DOTweenAnimationType.Text: if (t.IsSameOrSubclassOf(typeof(Text))) tween = ((Text)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString); // c = this.GetComponent<Text>(); // if (c != null) { // tween = ((Text)c).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString); // goto SetupTween; // } #if DOTWEEN_TK2D else if (t.IsSameOrSubclassOf(typeof(tk2dTextMesh))) tween = ((tk2dTextMesh)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString); // c = this.GetComponent<tk2dTextMesh>(); // if (c != null) { // tween = ((tk2dTextMesh)c).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString); // goto SetupTween; // } #endif #if DOTWEEN_TMP else if (t.IsSameOrSubclassOf(typeof(TextMeshProUGUI))) tween = ((TextMeshProUGUI)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString); else if (t.IsSameOrSubclassOf(typeof(TextMeshPro))) tween = ((TextMeshPro)target).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString); // c = this.GetComponent<TextMeshPro>(); // if (c != null) { // tween = ((TextMeshPro)c).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString); // goto SetupTween; // } // c = this.GetComponent<TextMeshProUGUI>(); // if (c != null) { // tween = ((TextMeshProUGUI)c).DOText(endValueString, duration, optionalBool0, optionalScrambleMode, optionalString); // goto SetupTween; // } #endif break; case DOTweenAnimationType.PunchPosition: if (t.IsSameOrSubclassOf(typeof(RectTransform))) tween = ((RectTransform)target).DOPunchAnchorPos(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0); else if (t.IsSameOrSubclassOf(typeof(Transform))) tween = ((Transform)target).DOPunchPosition(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0); // tween = transform.DOPunchPosition(endValueV3, duration, optionalInt0, optionalFloat0, optionalBool0); break; case DOTweenAnimationType.PunchScale: tween = transform.DOPunchScale(endValueV3, duration, optionalInt0, optionalFloat0); break; case DOTweenAnimationType.PunchRotation: tween = transform.DOPunchRotation(endValueV3, duration, optionalInt0, optionalFloat0); break; case DOTweenAnimationType.ShakePosition: if (t.IsSameOrSubclassOf(typeof(RectTransform))) tween = ((RectTransform)target).DOShakeAnchorPos(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0); if (t.IsSameOrSubclassOf(typeof(Transform))) tween = ((Transform)target).DOShakePosition(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0); // tween = transform.DOShakePosition(duration, endValueV3, optionalInt0, optionalFloat0, optionalBool0); break; case DOTweenAnimationType.ShakeScale: tween = transform.DOShakeScale(duration, endValueV3, optionalInt0, optionalFloat0); break; case DOTweenAnimationType.ShakeRotation: tween = transform.DOShakeRotation(duration, endValueV3, optionalInt0, optionalFloat0); break; case DOTweenAnimationType.CameraAspect: tween = ((Camera)target).DOAspect(endValueFloat, duration); break; case DOTweenAnimationType.CameraBackgroundColor: tween = ((Camera)target).DOColor(endValueColor, duration); break; case DOTweenAnimationType.CameraFieldOfView: tween = ((Camera)target).DOFieldOfView(endValueFloat, duration); break; case DOTweenAnimationType.CameraOrthoSize: tween = ((Camera)target).DOOrthoSize(endValueFloat, duration); break; case DOTweenAnimationType.CameraPixelRect: tween = ((Camera)target).DOPixelRect(endValueRect, duration); break; case DOTweenAnimationType.CameraRect: tween = ((Camera)target).DORect(endValueRect, duration); break; } // SetupTween: if (tween == null) return; if (isFrom) { ((Tweener)tween).From(isRelative); } else { tween.SetRelative(isRelative); } tween.SetTarget(this.gameObject).SetDelay(delay).SetLoops(loops, loopType).SetAutoKill(autoKill) .OnKill(()=> tween = null); if (easeType == Ease.INTERNAL_Custom) tween.SetEase(easeCurve); else tween.SetEase(easeType); if (!string.IsNullOrEmpty(id)) tween.SetId(id); tween.SetUpdate(isIndependentUpdate); //校驗是否有各種回調 if (hasOnStart) { if (onStart != null) tween.OnStart(onStart.Invoke); } else onStart = null; if (hasOnPlay) { if (onPlay != null) tween.OnPlay(onPlay.Invoke); } else onPlay = null; if (hasOnUpdate) { if (onUpdate != null) tween.OnUpdate(onUpdate.Invoke); } else onUpdate = null; if (hasOnStepComplete) { if (onStepComplete != null) tween.OnStepComplete(onStepComplete.Invoke); } else onStepComplete = null; if (hasOnComplete) { if (onComplete != null) tween.OnComplete(onComplete.Invoke); } else onComplete = null; if (autoPlay) tween.Play(); else tween.Pause(); } #endregion #region Public Methods // These methods are here so they can be called directly via Unity's UGUI event system 在這里的這些方法能直接通過Unity的UGUI事件系統調用 /// <summary> /// 播放 /// </summary> public override void DOPlay() { DOTween.Play(this.gameObject); } /// <summary> /// 回播 /// </summary> public override void DOPlayBackwards() { DOTween.PlayBackwards(this.gameObject); } /// <summary> /// 繼續播放 /// </summary> public override void DOPlayForward() { DOTween.PlayForward(this.gameObject); } /// <summary> /// 暫停 /// </summary> public override void DOPause() { DOTween.Pause(this.gameObject); } /// <summary> /// 暫停或繼續 /// </summary> public override void DOTogglePause() { DOTween.TogglePause(this.gameObject); } /// <summary> /// 倒回(動畫播放位置指針重新指向第一個動畫播放前) /// </summary> public override void DORewind() { _playCount = -1; // Rewind using Components order (in case there are multiple animations on the same property) 倒回正在使用的組件列表(以免同一個物體上有復合動畫) DOTweenAnimation[] anims = this.gameObject.GetComponents<DOTweenAnimation>(); for (int i = anims.Length - 1; i > -1; --i) { Tween t = anims[i].tween; if (t != null && t.IsInitialized()) anims[i].tween.Rewind(); } // DOTween.Rewind(this.gameObject); } /// <summary> /// Restarts the tween 重啟tween動畫 /// </summary> /// <param name="fromHere">If TRUE, re-evaluates the tween's start and end values from its current position. 如果為true,需要重新計算動畫的起始位置 /// Set it to TRUE when spawning the same DOTweenAnimation in different positions (like when using a pooling system) 當要在不同的位置大量播放相同的動畫時置為true(如使用一個動畫池)</param> public override void DORestart(bool fromHere = false) { _playCount = -1; if (tween == null) { if (Debugger.logPriority > 1) Debugger.LogNullTween(tween); return; } if (fromHere && isRelative) ReEvaluateRelativeTween(); DOTween.Restart(this.gameObject); } /// <summary> /// 完成動畫 /// </summary> public override void DOComplete() { DOTween.Complete(this.gameObject); } /// <summary> /// 強行結束動畫 /// </summary> public override void DOKill() { DOTween.Kill(this.gameObject); tween = null; } #region Specifics /// <summary> /// 通過id播放當前物體上的動畫 /// </summary> /// <param name="id"></param> public void DOPlayById(string id) { DOTween.Play(this.gameObject, id); } /// <summary> /// 播放所有相同id的動畫(不限於當前游戲物體) /// </summary> /// <param name="id"></param> public void DOPlayAllById(string id) { DOTween.Play(id); } /// <summary> /// 播放下一個動畫(游戲物體上掛載了多個此組件時) /// </summary> public void DOPlayNext() { DOTweenAnimation[] anims = this.GetComponents<DOTweenAnimation>(); while (_playCount < anims.Length - 1) { _playCount++; DOTweenAnimation anim = anims[_playCount]; if (anim != null && anim.tween != null && !anim.tween.IsPlaying() && !anim.tween.IsComplete()) { anim.tween.Play(); break; } } } /// <summary> /// 倒回動畫並重新開始播放動畫 /// </summary> public void DORewindAndPlayNext() { _playCount = -1; DOTween.Rewind(this.gameObject); DOPlayNext(); } /// <summary> /// 重播此物體上指定id的動畫 /// </summary> /// <param name="id"></param> public void DORestartById(string id) { _playCount = -1; DOTween.Restart(this.gameObject, id); } /// <summary> /// 重播所有指定id的動畫(不限於當前物體) /// </summary> /// <param name="id"></param> public void DORestartAllById(string id) { _playCount = -1; DOTween.Restart(id); } /// <summary> /// 獲取當前的tween /// </summary> /// <returns></returns> public List<Tween> GetTweens() { return DOTween.TweensByTarget(this.gameObject); } #endregion #endregion #region Private // Re-evaluate relative position of path 重估路徑的關聯點(計算下一個tween動畫的路徑點並設置此路徑點為move的終點) void ReEvaluateRelativeTween() { if (animationType == DOTweenAnimationType.Move) { ((Tweener)tween).ChangeEndValue(transform.position + endValueV3, true); } else if (animationType == DOTweenAnimationType.LocalMove) { ((Tweener)tween).ChangeEndValue(transform.localPosition + endValueV3, true); } } #endregion } public static class DOTweenAnimationExtensions { /// <summary> /// 計算組件是否是目標組件的子類或就是目標組件(拓展Type方法) /// </summary> /// <param name="t"></param> /// <param name="tBase"></param> /// <returns></returns> public static bool IsSameOrSubclassOf(this Type t, Type tBase) { return t.IsSubclassOf(tBase) || t == tBase; } } }