Unity插件---Dotween

1.什么是DOTween

DoTween 是由 Demigiant 开发的,被广泛应用于 Unity 游戏开发中。它是一个流行的动画插件,被许多开发者用于创建流畅、高效的动画效果,提升游戏体验。

2.DOTween的初始配置
①set up

首先找到DOTween Unity Panel 的面板 点击下面的SetUp DOTween… 按钮来定于全局的配置

在这里插入图片描述

②命名空间

首先引用命名空间才能使用相应的语法

using DG.Tweening; //命名空间
3. 代码解析
①物体

首先是基于物体的代码介绍以及是具体的使用方法。

///首先说明一下这些方法都是扩展方法下面 的具体扩展的哪一个方法就直接省略了。
// 移动到目标位置
public static TweenerCore<Vector3, Vector3, VectorOptions> DOMove(this Transform target, Vector3 endValue, float duration, bool snapping = false);
target:要移动的对象的 Transform 组件。
endValue:移动的目标位置。
duration:移动的持续时间。
snapping(可选):是否将位置的值进行取整,默认值为 false//再摸一个方向上的位置移动    
DOMoveX(float endValue,float duration, bool snapping = false);
DOMoveY();
DOMoveZ();
target:要移动的对象的 Transform 组件。
endValue:X 轴方向上移动的目标位置。
duration:移动的持续时间。
snapping(可选):是否将位置的值进行取整,默认值为 false// 物体的旋转
public static Tweener DORotate(this Transform target, Vector3 endValue, float duration, RotateMode mode = RotateMode.Fast);
target:要旋转的对象的 Transform 组件。
endValue:旋转的目标角度,使用 Euler 角(欧拉角)表示为 Vector3。
duration:旋转的持续时间。
mode(可选):旋转模式,默认为 RotateMode.Fast,可选的模式有 RotateMode.Fast 和 RotateMode.FastBeyond360
// 补充 RotateMode
RotateMode.Fast:快速旋转模式,对象将会直接根据目标旋转到指定角度,不考虑最短路径。

RotateMode.FastBeyond360:快速旋转模式,对象将会在超过360度后继续旋转,不考虑最短路径。

RotateMode.WorldAxisAdd:世界轴添加旋转模式,对象将会在世界坐标系下按照指定角度进行旋转。

RotateMode.LocalAxisAdd:本地轴添加旋转模式,对象将会在自身局部坐标系下按照指定角度进行旋转。

RotateMode.LocalAxisMinimum:本地轴最小旋转模式,对象将会绕着最短路径旋转到指定角度,保持局部坐标系和旋转方向一致
    
//物体的缩放
public static Tweener DOScale(this Transform target, Vector3 endValue, float duration);
target:要缩放的对象的 Transform 组件。
endValue:缩放的目标缩放值,使用 Vector3 表示。
duration:缩放的持续时间。

transform.DOScale(new Vector3(2, 2, 2), 1f);

// 冲击效果
public static Tweener DOPunchPosition(this Transform target, Vector3 punch, float duration, int vibrato = 10, float elasticity = 1f, bool snapping = false);
target:要应用该效果的对象的 Transform 组件。
punch:冲击的力度和方向。
duration:动画的持续时间。
vibrato(可选):冲击效果的震动次数,默认值为 10。
elasticity(可选):冲击效果的弹性,默认值为 1f,高于1f会引起超过原始位置的冲击。
snapping(可选):是否将位置的值进行取整,默认值为 false//运动的混合(可以多个连接在一起)
public static Tweener DOBlendableMoveBy(this Transform target, Vector3 byValue, float duration);
target:要移动的对象的 Transform 组件。
byValue:相对于当前位置的位移向量。
duration:移动的持续时间。

②材质

// 改变颜色的方法
public static Tweener DOColor(this Material target, Color endValue, string property, float duration);
target:要修改颜色属性的材质。
endValue:颜色变化的目标颜色。
property:要修改的颜色属性的名称。或者 propertyID
duration:颜色变化的持续时间。
// 改变透明度
public static TweenerCore<Color, Color, ColorOptions> DOFade(
      this Material target,
      float endValue,
      int propertyID,
      float duration)
endValue: 一个浮点数,表示透明度的目标值。动画将从对象的当前透明度逐渐过渡到这个目标值。
propertyID : 要修改的颜色属性的名称。或者 propertyID
duration: 一个浮点数,表示动画的持续时间(以秒为单位)。它定义了对象从当前透明度到目标透明度的平滑过渡所需的时间。

// 改变偏移值
public static TweenerCore<Vector2, Vector2, VectorOptions> DOOffset(
      this Material target,
      Vector2 endValue,
      float duration)
endValue:一个 Vector2 类型的参数,表示目标偏移值。动画会从当前的偏移值平滑地过渡到这个目标偏移值。
duration:一个浮点数类型的参数,表示动画的持续时间(以秒为单位)。它定义了对象从当前偏移值到目标偏移值的平滑过渡所需的时间。
// DOVector方法用于在一定的持续时间内从当前值平滑地过渡到目标值
public static TweenerCore<Vector4, Vector4, VectorOptions> DOVector(
      this Material target,
      Vector4 endValue,
      int propertyID,
      float duration)
target:一个Material类型的参数,表示目标材质。
endValue:一个Vector4类型的参数,表示目标属性值。动画将从当前属性值逐渐过渡到这个目标值。
propertyID:一个整数类型的参数,表示目标属性的标识符。您可以使用Shader.PropertyToID方法将属性名称转换为propertyID。使用propertyID而不是直接使用属性名称可以提高性能。
duration:一个浮点数类型的参数,表示动画的持续时间(以秒为单位)。  
_material.DOBlendableColor(Color.green, propertyID, 2f);

③运动序列

// 运动序列是一个空的序列 可以存储许多的DOTween动画 并进行管理
//使用前首先要声明一个 DOTween的序列
 Sequence sequence = DOTween.Sequence();

 Append 谁先加进去谁先执行 

// 将动画 一次加入到序列中并且依次执行 先添加先执行
public static Sequence Append(this Sequence s, Tween t)
动画Tween:可以是使用DOTween提供的Tween方法创建的动画,比如transform.DOMoveY(3, 2f)。
Tweener:表示一个已经创建好的Tween动画,可以是其他地方创建的Tween对象,比如DOTween.To(() => myVariable, x => myVariable = x, targetValue, duration)。
回调方法:可以是一个委托方法,用于在动画结束时执行的回调函数,比如sequence.AppendCallback(() => Debug.Log("Animation finished"));
// 用于并行播放动画 参数同上
public static Sequence Join(this Sequence s, Tween t)
Sequence sequence = DOTween.Sequence();

Tween tween1 = transform.DOMoveX(5, 2f);
Tween tween2 = transform.DOScale(Vector3.one * 2, 1f);
Tween tween3 = transform.DORotate(Vector3.up * 90, 1.5f);

sequence.Join(tween1);
sequence.Join(tween2);
sequence.Join(tween3);
// 插入一个动画
public static Sequence Insert(this Sequence s, Tween t)
//当插入到 补间和你原来所处时间的补间动画 控制的是同一个属性如position ,或者Scale,Rotate等 就会发生覆盖,但是如果控制的不是相同用的属性则发生的是附加,和join的效果差不多。
       
//插入一个延迟
sequence.AppendInterval(1f)

Prepend 谁先进去谁后执行 但是这个Prepend的执行还是在Append之前的
// 预添加
public static Sequence Prepend(this Sequence s, Tween t)

// 回调函数
// 在某个地方添加一个回调,当值执行到这个的时候会进行回调
sequence.AppendCallback(() => Debug.Log("Animation finished"));
// 在某个地方插入一个回调,当值执行到这个的时候会进行回调
sequence.InsertCallback(() => Debug.Log("Animation finished"));
// 在某个地方预添加一个回调,当值执行到这个的时候会进行回调
sequence.PrependCallback(() => Debug.Log("Animation finished"));

④循环

// Dotween是链式代码 可以一直点出来一些设置
public static T SetLoops<T>(this T t, int loops, LoopType loopType) where T : Tween
loops :循环次数 -1 代表无限
LoopType:循环的方式
+ Restart:每次循环重新启动动画,即每次循环从起始点重新开始。
+ Yoyo:循环播放动画,反复往返运动,即在正向播放完后反向播放,循环如此进行。
+ Incremental:循环播放动画时每次增量加上前一次循环的值。比如初始动画播放从12,第一次循环增加2,第二次循环增加4,以此类推。
如 : transform.DOMove(Vector3.one, 1f).SetLoops(1,LoopType.Restart);
// 第二种方法实现循环
public static T SetAs<T>(this T t, TweenParams tweenParams) where T : Tween
 TweenParams pTweenParams = new TweenParams();
 pTweenParams.SetLoops(-1);
 transform.DOMove(Vector3.one, 1).SetAs(pTweenParams);

⑤常用set的函数

//当动画执行完成之后自动的销毁。SetAutoKill()
transform.DOMove(Vector3.one, 1).SetAs(pTweenParams).SetAutoKill();
// 反向移动From() :就是从一个endvalue 向startvalue移动
// 默认值为false ,当为True的时候 此时就会加上当下的坐标在进行动画
 transform.DOMove(Vector3.one, 1).SetAs(pTweenParams).From(false);

// 设置延迟SetDelay( float DelayTime);
transform.DOMove(Vector3.one, 1).From(true).SetDelay(2f);

//设置速度 默认情况下 这个1表示的时间  SetSpeedBased(true) 此时这个1就变成了速度,false的时候 1代表的还是时间 
transform.DOMove(Vector3.one, 1).From(true).SetSpeedBased(true);

// 设置ID Tween的标识符可以是任意类型的对象,用于在后续操作中对Tween进行引用或检索
        TweenParams myParams = new TweenParams();
        // 设置标识符
        myParams.SetId("MyTween");
        // 使用TweenParams中的参数创建Tween对象
        transform.DOMoveX(10f, 1f).SetAs(myParams);
 	    Tweener myTween = DOTween.GetTween("MyTween");

// 设置相对移动 表示的是相对与该物体的位置在向某个方向移动某一个距离。
transform.DOMove(Vector3.one, 1).SetRelative();
// 设置更新 SetUpdate()方法用于设置Tween的更新类型。Tween的更新类型确定了Tween的更新操作是基于时间的(TimeBased)还是基于帧数的(FrameBased)

UpdateType.Normal:基于帧数的更新类型,按照Unity的帧数进行更新(默认)。当为True的时候不受TimeScale的影响
UpdateType.Fixed:基于固定时间间隔的更新类型,按照Unity的FixedUpdate进行更新。
UpdateType.Manual:手动更新类型,需要手动调用Tween的ManualUpdate()方法来更新。
 void Start()
        // 创建Tween对象并设置持续时间为1秒钟
        Tweener tween = transform.DOMoveX(10f, 1f);
        // 将Tween的更新类型设置为帧数更新(FrameBased)
        tween.SetUpdate(UpdateType.Normal, true);
        // 设置Tween启动时回调函数
        tween.OnStart(() => Debug.Log("Tween started!"));
        // 启动Tween
        tween.Play();
    void Update()
        if (Input.GetKeyDown(KeyCode.Space))
            // 暂停Tween的更新操作
            DOTween.PauseAll();
            // 在暂停时输出日志
            Debug.Log("Tween paused!");
        else if (Input.GetKeyDown(KeyCode.R))
            // 恢复Tween的更新操作
            DOTween.PlayAll();
            // 在恢复时输出日志
            Debug.Log("Tween resumed!");
  
//设置曲线 
public static T SetEase<T>(this T t, Ease ease) where T : Tween
public static T SetEase<T>(this T t, AnimationCurve animCurve) where T : Tween
public static T SetEase<T>(this T t, EaseFunction customEase) where T : Tween
customEase :自定义方法
AnimationCurve :动画曲线   
ease: 曲线的类型
transform.DOMove(new Vector3(2, 2, 2), 2f).SetEase(Ease.Linear);
Linear:线性缓动,无衰减或加速。
InSine:正弦缓动,起始缓慢,逐渐加速。
OutSine:正弦缓动,起始快速,逐渐减速。
InOutSine:正弦缓动,起始和结束时缓慢,中间时加速。
InQuad:二次方缓动,起始缓慢,逐渐加速。
OutQuad:二次方缓动,起始快速,逐渐减速。
InOutQuad:二次方缓动,起始和结束时缓慢,中间时加速。
InCubic:三次方缓动,起始缓慢,逐渐加速。
OutCubic:三次方缓动,起始快速,逐渐减速。
InOutCubic:三次方缓动,起始和结束时缓慢,中间时加速。
InQuart:四次方缓动,起始缓慢,逐渐加速。
OutQuart:四次方缓动,起始快速,逐渐减速。
InOutQuart:四次方缓动,起始和结束时缓慢,中间时加速。
InQuint:五次方缓动,起始缓慢,逐渐加速。
OutQuint:五次方缓动,起始快速,逐渐减速。
InOutQuint:五次方缓动,起始和结束时缓慢,中间时加速。
InExpo:指数缓动,起始缓慢,逐渐加速。
OutExpo:指数缓动,起始快速,逐渐减速。
InOutExpo:指数缓动,起始和结束时缓慢,中间时加速。
InCirc:圆形缓动,起始缓慢,逐渐加速。
OutCirc:圆形缓动,起始快速,逐渐减速。
InOutCirc:圆形缓动,起始和结束时缓慢,中间时加速。
InBack:回弹缓动,起始缓慢,逐渐加速。
OutBack:回弹缓动,起始快速,逐渐减速。
InOutBack:回弹缓动,起始和结束时缓慢,中间时加速。
InElastic:弹簧缓动,起始缓慢,逐渐加速。
OutElastic:弹簧缓动,起始快速,逐渐减速。
InOutElastic:弹簧缓动,起始和结束时缓慢,中间时加速。
InBounce:反弹缓动,起始缓慢,逐渐加速。
OutBounce:反弹缓动,起始快速,逐渐减速。
InOutBounce:反弹缓动,起始和结束时缓慢,中间时加速
// 回调函数 
//使用OnComplete通常需要为该事件指定一个函数或方法作为参数。当操作完成时,该函数将被调用,完成后续的逻辑
OnComplete
tween.OnComplete(() => {
    
     Debug.Log("Tween completed!"); });   // 使用Lambda表达式定义回调函数

//当Tween被中止时,你可以通过OnKill()方法指定一个回调函数,在回调函数中执行相应的操作。
   tween = transform.DOMoveX(10f, 1f);
        // 设置Tween被中止时的回调函数
        tween.OnKill(OnTweenKill);
        // 启动Tween
        tween.Play();

    // 中止Tween时的回调函数
    void OnTweenKill()
        // 在Tween被中止时执行自定义操作
        Debug.Log("Tween killed!");
    // 示例中的中止Tween的操作
    void StopTween()
        // 在适当的时机中止Tween
        tween.Kill();
// 反复的完成
OnstepComplete

// Onstart 开始播放的时候只能执行一次
Onstart

// 当暂停的时候 然后在开始
Onplay / Onpause

// OnRewind
动画重新播放的时候开始执行

本文是博主的总结,还会有后续的更新。想要查看官方文档的话可以访问https://dotween.demigiant.com/ Dotween官网

猜你喜欢

转载自blog.csdn.net/m0_52021450/article/details/132535522