Unity3D的Dotween动画库入门

前两年在做VR launcher的时候,同事们已经使用Dotween实现一些界面动画了,当时没花时间去学习,今天捡回来看一下。

Dotween动画库在Unity的Assetstore里面可以找到,有免费的版本。目前就是使用免费的版本。

DOTween库的命名方式

学习DOTween,先了解他的命名方式。DOTween的命名前缀有助于开发者记忆或者快速调用需要的接口,可以看到Dotween在接口设计上比较重视这方面。

导入Dotween库之后,我们就可以直接在transform或者material这些对象上直接调用Dotween的接口了,下面是DOTween库的三种命名前缀:

  • DO前缀,在transform或者material对象上快捷调用动画控制接口,也是DOTween类的前缀。
transform.DOMoveX(100, 1);
transform.DORestart();

DOTween.Play();
  • Set前缀,给tween对象设置属性
myTween.SetLoops(4, LoopType.Yoyo).SetSpeedBased();
  • On 前缀,给tween对象设置回调方法
myTween.OnStart(myStartFunction).OnComplete(myCompleteFunction);

一、DOTween初始化

在调用动画接口前,需要先初始化Dotween,通过调用初始化方法

// static DOTween.Init(bool recycleAllByDefault = false, bool useSafeMode = true, LogBehaviour logBehaviour = LogBehaviour.ErrorsOnly)

// EXAMPLE A: initialize with the preferences set in DOTween's Utility Panel
DOTween.Init();
// EXAMPLE B: initialize with custom settings, and set capacities immediately
DOTween.Init(true, true, LogBehaviour.Verbose).SetCapacity(200, 10);

二、创建Tweener

Tweener是实现DoTween动画的执行者,携带着动画的属性和对象,并且实现属性值的改变

DOTween目前支持改变这些类型:float, double, int, uint, long, ulong, Vector2/3/4, Quaternion, Rect, RectOffset, Color, string

有三种方式创建Tweener:

A. 传统方式

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

B. 快捷方式
通过直接给Unity的对象调用快捷方法执行DOTween动画,Unity对象包括Transform, Rigidbody and Material

transform.DOMove(new Vector3(2,3,4), 1);
rigidbody.DOMove(new Vector3(2,3,4), 1);
material.DOColor(Color.green, 1);

给Tweener调用From方法,会让目标对象跳到你所设置的属性上,执行恢复到当前属性的动画

transform.DOMove(new Vector3(2,3,4), 1).From();
rigidbody.DOMove(new Vector3(2,3,4), 1).From();
material.DOColor(Color.green, 1).From();

Unity基本组件的方法

AudioSource

DOFade(float to, float duration)
DOPitch(float to, float duration)

Camera

DOAspect(float to, float duration)
DOColor(Color to, float duration)
DOFarClipPlane(float to, float duration)
DOShakePosition(float duration, float/Vector3 strength, int vibrato, bool fadeOut)
.
.

Material

DOColor(Color to, float duration)
DOColor(Color to, string property, float duration)
DOFade(float to, float duration)
.
.

Rigidbody

DOMove(Vector3 to, float duration, bool snapping)
DOJump(Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping)
DORotate(Vector3 to, float duration, RotateMode mode)
.
.

Transform

DOMove(Vector3 to, float duration, bool snapping)
DORotate(Vector3 to, float duration, RotateMode mode)
DOScale(float/Vector3 to, float duration)
.
.

Image

DOColor(Color to, float duration)
DOFade(float to, float duration)

还有很多很多,最好还是查看官方文档

C. 特殊方式

static DOTween.Punch(getter, setter, Vector3 direction, float duration, int vibrato, float elasticity)
static DOTween.Shake(getter, setter, float duration, float/Vector3 strength, int vibrato, float randomness, bool ignoreZAxis)
static DOTween.ToAlpha(getter, setter, float to, float duration)
static DOTween.ToArray(getter, setter, float to, float duration)
static DOTween.ToAxis(getter, setter, float to, float duration, AxisConstraint axis)
static DOTween.To(setter, float startValue, float endValue, float duration)

三、创建动画序列

创建序列,通过这两步:
1. 创建Sequence用于存储tweener引用

static DOTween.Sequence()
  1. 添加tweens、intervals、callbacks到Sequence里
Append(Tween tween)
Insert(float atPosition, Tween tween)
Join(Tween tween)

例子:

// Grab a free Sequence to use
Sequence mySequence = DOTween.Sequence();
// Add a movement tween at the beginning
mySequence.Append(transform.DOMoveX(45, 1));
// Add a rotation tween as soon as the previous one is finished
                    mySequence.Append(transform.DORotate(new Vector3(0,180,0), 1));
// Delay the whole Sequence by 1 second
mySequence.PrependInterval(1);
// Insert a scale tween for the whole duration of the Sequence
mySequence.Insert(0, transform.DOScale(new Vector3(3,3,3), mySequence.Duration()));

DOTween还有更多的用法,还是直接去官网看吧
官网文档地址:http://dotween.demigiant.com/documentation.php
推荐网址:http://hidavid.cn

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DvLee1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值