天天看点

Unity3D:DoTween动画

一、控制变量

  1. DOTween.To()

    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)

DOMove(Vector3 to, float duration, bool snapping) // 全局坐标移动

DOMoveX/DOMoveY/DOMoveZ(float to, float duration, bool snapping)

DOLocalMove(Vector3 to, float duration, bool snapping) // 本地坐标移动

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)

DORotate(Vector3 to, float duration, RotateMode mode)

DORotateQuaternion(Quaternion to, float duration)

DOLocalRotate(Vector3 to, float duration, RotateMode mode)

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:持续时间

DOScale(float/Vector3 to, float duration)

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)

DOJump(Vector3 endValue, float jumpPower, int numJumps, float jumpPower, bool snapping)

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)

DOPunchPosition(Vector3 punch, float duration, int vibrato, float elasticity, bool snapping)

DOPunchRotation(Vector3 punch, float duration, int vibrato, float elasticity)

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)

DOShakePosition(float duration, float/Vector3 strength, int vibrato, float randomness, bool snapping, bool fadeOut)

DOShakeRotation(float duration, float/Vector3 strength, int vibrato, float randomness, bool fadeOut)

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:持续时长

DOColor(Color to, float duration)

DOFade(float to, float duration)

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)

sequence.Append(Tween tween) // 添加一个动画到序列末尾。

sequence.AppendCallback(TweenCallback callback) // 添加回调函数到序列末尾。

sequence.AppendInterval(float interval) // 添加一段空时间到序列末尾。

sequence.Insert(float atPosition, Tween tween) // 插入一段动画到指定时间。

sequence.InsertCallback(float atPosition, TweenCallback callback) // 插入回调函数到序列指定时间。

sequence.Join(Tween tween) // 插入动画与序列最后一个动画(这里的最后是指最后加入序列而非序列末尾)同时播放。

sequence.Prepend(Tween tween) // 添加一个动画到序列头部。

sequence.PrependCallback(TweenCallback callback) // 添加回调函数到序列头部。

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、全局

DOTween.defaultAutoKill // (全局) 当该值为真时,之后创建的动画当其播放完毕之后会自动被销毁。

DOTween.defaultAutoPlay // (全局) 当该值为真时,之后创建的动画会自动播放。

DOTween.defaultEaseType // (全局) 该值为创建动画时候默认的动画曲线。

DOTween.defaultLoopType // (全局) 该值为创建动画时候默认的循环模式。

2、局部

tweener.SetAs(Tween tween \ TweenParams tweenParams) // 设置该动画相关属性。

tweener.SetAutoKill(bool autoKillOnCompletion = true) // 设置该动画是否自动销毁。

tweener.SetEase(Ease easeType \ AnimationCurve animCurve \ EaseFunction customEase) // 设置缓动参数,实现不同的动画效果。

tweener.SetId(object id) // 设置该动画的id。

tweener.SetLoops(int loops, LoopType loopType = LoopType.Restart) // 设置该动画循环次数和循环类型,次数为-1表示无限循环。

tweener.SetRecyclable(bool recyclable) // 设置动画为可回收,可循环使用的。

tweener.SetRelative(bool isRelative = true) // 设置动画为相对的。

tweener.SetUpdate(UpdateType updateType, bool isIndependentUpdate = false) // 设置动画是否忽略TimeScale。

六、动画操作

tweener.From() // 参数为true或者false。表示运动为相对运动还是绝对运动。并且动画效果为tweener的逆动画

tweener.Pause() // 动画播放暂停。

tweener.Play() // 动画继续播放。

tweener.Flip() // 动画播放中执行,动画原轨迹运动到起始点,当动画回到起始状态时动画结束。

tweener.Complete() // 动画播放中执行,物体立即运动到动画末尾状态,动画结束。

tweener.Goto() // 参数为float,表示动画立即进入到时间为t时候的状态。

tweener.PlayForward() // 动画顺序播放。

tweener.PlayBackwards() // 动画倒序播放。

tweener.TogglePause() // 顺序/倒序播放。该方法会自动识别物体当前状态,如果在起始点就顺序,否则就逆序

tweener.ReStart() // 动画重新开始播放。

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();
    }
}
           

}

七、回调函数

tweener.OnComplete() // 动画结束时触发。

tweener.OnKill() // 动画被销毁时触发。

tweener.OnPlay() // 动画开始播放时触发。

tweener.OnPause() // 动画暂停播放时触发。

tweener.OnRewind() // 动画倒转播放时触发。

tweener.OnStart() // 动画被创建时触发。

tweener.OnStepComplete() // 单个动画循环结束时触发。

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);
}
           

}

转载自文弱书生陈皮皮:https://blog.csdn.net/iFasWind/article/details/81452516``

继续阅读