一.简介
1.DOTween是Unity游戏开发中常用的动画插件,拓展了Unity原有的常用组件,如TransformCamera等,使用方式如下:
transform.DOMove(Vector3.one, .5f).SetDelay(1f).SetEase(Ease.Linear).OnComplete(() => { Debug.Log("doMove complete"); });
使用时直接使用组件调用相应的动画方法(DOMoveDOMoveX等),这些方法的返回值是一个Tweener对象,之后可以调用Tweener对象的各种方法为这个Tweener对象设置动画延迟动画曲线回调(设置方法设置完成后会将Tweener对象自身返回,所以可以连续使用"."对Tweener进行设置)等.DOTween的这些方法在DOTween的官方文档中有详细的说明.
2.DOTweenAnimation组件是DOTweenPro提供的一个动画组件,添加到游戏物体上进行可视化动画设置,当然也可以使用代码添加和设置这个脚本.
3.说明:本文使用的DOTween插件版本是DOTween Pro v0.9.290.
二.DOTween的拓展方法
导入的DOTween插件中DOMoveSetDelay等拓展方法在生成后的动态链接库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; } } }