[Unity 3D] DOTween 常用函数

DOTween官方文档:http://dotween.demigiant.com/documentation.php

一、控制变量

1. DOTween.To()

  1. static DOTween.To(getter, setter, to, float duration)

这个函数使用了Lambda表达式,主要功能为:给变量a添加一个动画,让它从默认值1在2秒内变化到5。

变量类型可以为int、Vector3、Color等等,但是要确保与目标类型相同。

using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Test01 : MonoBehaviour {

    public float a = 1;

    void Start () {
        DOTween.To(() => a, x => a = x, 5, 2);
    }
}

 

二、 控制物体(Transform)

1. 位置动画(Move)

to:目标位置/值,duration:持续时间,snapping:只取整数值(默认为false)

  1. DOMove(Vector3 to, float duration, bool snapping)    // 全局坐标移动
  2. DOMoveX/DOMoveY/DOMoveZ(float to, float duration, bool snapping)
  3. DOLocalMove(Vector3 to, float duration, bool snapping)    // 本地坐标移动
  4. DOLocalMoveX/DOLocalMoveY/DOLocalMoveZ(float to, float duration, bool snapping)
using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Move : MonoBehaviour {
    void Start () {
        transform.DOMove(new Vector3(10, 10, 10), 5, true);
    }
}

2.旋转动画(Rotate)

to:目标值,duration:持续时间,mode:旋转模式(默认为RotateMode.Fast)

  1. DORotate(Vector3 to, float duration, RotateMode mode)
  2. DORotateQuaternion(Quaternion to, float duration)
  3. DOLocalRotate(Vector3 to, float duration, RotateMode mode)
  4. DOLocalRotateQuaternion(Quaternion to, float duration)
using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Rotate : MonoBehaviour {
    void Start() {
        transform.DORotate(new Vector3(0, 0, 360), 5);
    }
}

3.缩放动画(Scale)

to:目标值,duration:持续时间

  1. DOScale(float/Vector3 to, float duration)
  2. DOScaleX/DOScaleY/DOScaleZ(float to, float duration)
using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Scale: MonoBehaviour {
    void Start() {
        transform.DOScale(2, 5);
    }
}

4.跳跃动画(Jump)

endValue:结束位置,jumpPower:跳跃的最大高度,numJumps:跳跃次数,jumpPower:持续时间,snapping:只取整数值(默认为false)

  1. DOJump(Vector3 endValue, float jumpPower, int numJumps, float jumpPower, bool snapping)
  2. DOLocalJump(Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping)
using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Rotate : MonoBehaviour {
    void Start() {
        transform.DOJump(new Vector3(2, 2, 2), 4, 4, 5);
    }
}

5.击打动画(Punch)

punch:晃动幅度,duration:持续时间,vibrato:晃动的次数(默认为10),elasticity:弹性系数(默认为1),snapping:只取整数值(默认为false)

  1. DOPunchPosition(Vector3 punch, float duration, int vibrato, float elasticity, bool snapping)
  2. DOPunchRotation(Vector3 punch, float duration, int vibrato, float elasticity)
  3. DOPunchScale(Vector3 punch, float duration, int vibrato, float elasticity)
using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Rotate : MonoBehaviour {
    void Start() {
        transform.DOPunchPosition(new Vector3(2, 2, 2), 5);
    }
}

6. 摇晃动画(Shake)

duration:持续时间,strength:晃动幅度(默认为float:1),vibrato:晃动的次数(默认为10),randomness:随机晃动角度(0-180,默认为90),snapping:只取整数值(默认为false),fadeOut:渐出效果(默认为true)

  1. DOShakePosition(float duration, float/Vector3 strength, int vibrato, float randomness, bool snapping, bool fadeOut)
  2. DOShakeRotation(float duration, float/Vector3 strength, int vibrato, float randomness, bool fadeOut)
  3. DOShakeScale(float duration, float/Vector3 strength, int vibrato, float randomness, bool fadeOut)
using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Rotate : MonoBehaviour {
    void Start() {
        transform.DOShakePosition(5);
    }
}

 

三、文字动画(Text)

to:目标颜色/值/文本,duration:持续时长

  1. DOColor(Color to, float duration)
  2. DOFade(float to, float duration)
  3. DOText(string to, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null)
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;  // 引入命名空间

public class Test : MonoBehaviour {

    private Text _text;

    void Start()
    {
        _text = GetComponent<Text>();
        _text.DOText("0123456789", 10);
    }
}

 

四、动画序列(Sequence)

  1. sequence.Append(Tween tween)    // 添加一个动画到序列末尾。
  2. sequence.AppendCallback(TweenCallback callback)    // 添加回调函数到序列末尾。
  3. sequence.AppendInterval(float interval)    //  添加一段空时间到序列末尾。
  4. sequence.Insert(float atPosition, Tween tween)    // 插入一段动画到指定时间。
  5. sequence.InsertCallback(float atPosition, TweenCallback callback)    //  插入回调函数到序列指定时间。
  6. sequence.Join(Tween tween)    // 插入动画与序列最后一个动画(这里的最后是指最后加入序列而非序列末尾)同时播放。
  7. sequence.Prepend(Tween tween)    // 添加一个动画到序列头部。
  8. sequence.PrependCallback(TweenCallback callback)    // 添加回调函数到序列头部。
  9. sequence.PrependInterval(float interval)    //  添加一段空时间到序列头部。
using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Shake : MonoBehaviour
{

    private Sequence sequence;

    void Start()
    {
        sequence = DOTween.Sequence();
        Tween tween1 = transform.DOMove(new Vector3(2, 2, 2), 2);
        Tween tween2 = transform.DORotate(new Vector3(45, 45, 45),2);
        Tween tween3 = transform.DOShakePosition(2);

        sequence.Append(tween1);
        sequence.Join(tween2);
        sequence.AppendInterval(1);
        sequence.Append(tween3);
    }
}

 

五、动画设置

1、全局

  1. DOTween.defaultAutoKill    //  (全局) 当该值为真时,之后创建的动画当其播放完毕之后会自动被销毁。
  2. DOTween.defaultAutoPlay    //  (全局) 当该值为真时,之后创建的动画会自动播放。
  3. DOTween.defaultEaseType    //  (全局) 该值为创建动画时候默认的动画曲线。
  4. DOTween.defaultLoopType    //  (全局) 该值为创建动画时候默认的循环模式。

2、局部

  1. tweener.SetAs(Tween tween \ TweenParams tweenParams)    // 设置该动画相关属性。
  2. tweener.SetAutoKill(bool autoKillOnCompletion = true)    // 设置该动画是否自动销毁。
  3. tweener.SetEase(Ease easeType \ AnimationCurve animCurve \ EaseFunction customEase)    // 设置缓动参数,实现不同的动画效果。
  4. tweener.SetId(object id)    // 设置该动画的id。
  5. tweener.SetLoops(int loops, LoopType loopType = LoopType.Restart)    // 设置该动画循环次数和循环类型,次数为-1表示无限循环。
  6. tweener.SetRecyclable(bool recyclable)    // 设置动画为可回收,可循环使用的。
  7. tweener.SetRelative(bool isRelative = true)    // 设置动画为相对的。
  8. tweener.SetUpdate(UpdateType updateType, bool isIndependentUpdate = false)    // 设置动画是否忽略TimeScale。

 

六、动画操作

  1. tweener.From()    // 参数为true或者false。表示运动为相对运动还是绝对运动。并且动画效果为tweener的逆动画
  2. tweener.Pause()    // 动画播放暂停。
  3. tweener.Play()    // 动画继续播放。
  4. tweener.Flip()    // 动画播放中执行,动画原轨迹运动到起始点,当动画回到起始状态时动画结束。
  5. tweener.Complete()    // 动画播放中执行,物体立即运动到动画末尾状态,动画结束。
  6. tweener.Goto()    // 参数为float,表示动画立即进入到时间为t时候的状态。
  7. tweener.PlayForward()    // 动画顺序播放。
  8. tweener.PlayBackwards()    // 动画倒序播放。
  9. tweener.TogglePause()    // 顺序/倒序播放。该方法会自动识别物体当前状态,如果在起始点就顺序,否则就逆序
  10. tweener.ReStart()    // 动画重新开始播放。
  11. tweener.Kill()    // 立即销毁该动画。
using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Shake : MonoBehaviour {

    private Tweener tweener;

    void Start() {
        tweener = transform.DOShakePosition(2);
        tweener.Pause();
        tweener.SetAutoKill(false);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            tweener.Restart();
        }
    }
}

 

七、回调函数

  1. tweener.OnComplete()    // 动画结束时触发。
  2. tweener.OnKill()    // 动画被销毁时触发。
  3. tweener.OnPlay()    // 动画开始播放时触发。
  4. tweener.OnPause()    // 动画暂停播放时触发。
  5. tweener.OnRewind()    // 动画倒转播放时触发。
  6. tweener.OnStart()    // 动画被创建时触发。
  7. tweener.OnStepComplete()    // 单个动画循环结束时触发。
  8. tweener.OnUpdate()    // 动画更新时触发。
using UnityEngine;
using DG.Tweening;  // 引入命名空间

public class Shake : MonoBehaviour
{

    private Tweener tweener;

    void Start()
    {
        tweener = transform.DOShakePosition(2);
        tweener.OnStart(OnStart);    // 使用回调函数
        tweener.OnUpdate(() => Debug.Log("OnUpdate"));    // 使用回调函数+匿名函数
        tweener.onComplete += OnComplete;    // 使用内部的委托
        tweener.OnKill(() => OnKill("OnKill"));    // 传递参数需要使用Lambda表达式
    }

    void OnStart()
    {
        Debug.Log("OnStart");
    }

    void OnComplete()
    {
        Debug.Log("OnComplete");
    }

    void OnKill(string param)
    {
        Debug.Log(param);
    }
}
  • 20
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值