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., 1);
给Tweener调⽤From⽅法,会让⽬标对象跳到你所设置的属性上,执⾏恢复到当前属性的动画
transform.DOMove(new Vector3(2,3,4), 1).From();
rigidbody.DOMove(new Vector3(2,3,4), 1).From();
material., 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)
unity3d入门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()));
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论