U3DC.COM | 优三帝研究院

Menu

DoTween官方文档中文版(四)Creating a Tweener

Creating a Tweener(创建补间)

Tweeners are the working ants of DOTween. They take a property/field and animate it towards a given value.

Tweeners就像给DOTween工作的蚂蚁他们针对属性/区域一个给定进行动画处理

As of now DOTween can tween these types of values:
float, int, uint, Vector2/3/4, Quaternion, Rect, RectOffset, string
(some of these values can be tweened in special ways)

就目前,DOTween可以使用这些类型的值:

float, int, uint, Vector2/3/4, Quaternion, Rect, RectOffset, string

一些这些补间特别的方式

There are 3 ways to create a Tweener: the generic way, the shortcuts way and additional generic ways.

 

有三种创建Tweener的方式:泛型方式 快捷方式其他通用方式

A. The generic way(泛型方式

This is the most flexible way of tweening and allows you to tween almost any value, eitherpublic or private, static or dynamic (just so you know, the shortcuts way actually uses the generic way in the background).

补间灵活方式允许补间几乎任何 eitherpublic 私人静态动态 (只是知道快捷方式实际上使用泛型方法后台)。

As with shortcuts, the generic way has a FROM alternate version. Just chain a From to a Tweener to make the tween behave as a FROM tween instead of a TO tween.

随着快捷方式一般方法一个备用版本只是 Tweener 表现补间动画而不是补间动画补间

static DOTween.To(getter, setter, to, float duration)
Changes the given property from its current value to the given one.
给定一个当前更改给定属性
getter A delegate that returns the value of the property to tween. Can be written as a lambda like
获得返回补间属性委托可以比如 lambdathis: ()=> myValue
where myValue is the name of the property to tween.
setter A delegate that sets the value of the property to tween. Can be written as a lambda like this:
设置属性补间委托可以作为 lambda 这样x=> myValue = x
where myValue is the name of the property to tween.
其中 myValue 补间属性名称
to The end value to reach.
duration The duration of the tween.
达到最终
补间动画持续时间持续时间

Examples(示例)

// Tween a Vector3 called myVector to 3,4,8 in 1 second
DOTween.To(()=> myVector, x=> myVector = x, new Vector3(3,4,8), 1);
// Tween a float called myFloat to 52 in 1 second
DOTween.To(()=> myFloat, x=> myFloat = x, 52, 1);

B. The shortcuts way(快捷方式)

DOTween includes shortcuts for some known Unity objects, like Transform, Rigidbody and Material. You can start a tween directly from a reference to these objects (which will also automatically set the object itself as the tween target), like:

DOTween 包含一些已知的unity对象,如变换刚体材料快捷方式可以补间直接引用这些对象 会自动设置对象本身作为补间目标) 例如

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

Each of these shortcuts also has a FROM alternate version except where indicated. Just chain a From to a Tweener to make the tween behave as a FROM tween instead of a TO tween.
IMPORTANT: when you assign a FROM to a tween, the target will immediately jump to the FROM position (immediately as in "the moment you write that line of code", not "the moment the tween starts").

每个这些快捷方式也都有一个FROM备用版本除非有指定只是 Tweener 表现为FROM补间动画而不是TO补间动画补间
重要提示 分配补间目标立即跳转到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();

C. Additional generic ways

These are additional generic methods that allow to tween values in specific ways.

These too have FROM alternate versions except where indicated. Just chain a From to a Tweener to make the tween behave as a FROM tween instead of a TO tween.

static DOTween.Punch(getter, setter, Vector3 direction, float duration, int vibrato, floatelasticity)
No FROM version.
Punches a Vector3 towards the given direction and then back to the starting one as if it was connected to the starting position via an elastic.
getter A delegate that returns the value of the property to tween. Can be written as a lambda like this: ()=> myValue
where myValue is the name of the property to tween.
setter A delegate that sets the value of the property to tween. Can be written as a lambda like this: x=> myValue = x
where myValue is the name of the property to tween.
direction The direction and strength of the punch.
duration The duration of the tween.
vibrato Indicates how much will the punch vibrate,
elasticity Represents how much (0 to 1) the vector will go beyond the starting position when bouncing backwards. 1 creates a full oscillation between the direction and the opposite decaying direction, while 0 oscillates only between the starting position and the decaying direction.

// Punch upwards a Vector3 called myVector in 1 second
DOTween.Punch(()=> myVector, x=> myVector = x, Vector3.up, 1);
static DOTween.Shake(getter, setter, float duration, float/Vector3 strength, int vibrato,float randomness, bool ignoreZAxis)
No FROM version.
Shakes a Vector3 along its X Y axes with the given values.
getter A delegate that returns the value of the property to tween. Can be written as a lambda like this: ()=> myValue
where myValue is the name of the property to tween.
setter A delegate that sets the value of the property to tween. Can be written as a lambda like this: x=> myValue = x
where myValue is the name of the property to tween.
duration The duration of the tween.
strength The shake strength. Using a Vector3 instead of a float lets you choose the strength for each axis.
vibrato Indicates how much will the shake vibrate,
randomness Indicates how much the shake will be random (0 to 360 - values higher than 90 kind of suck, so beware). Setting it to 0 will shake along a single direction and behave like a random punch.
ignoreZAxis If TRUE shakes only along the X Y axis (not available if you use a Vector3 forstrength).

// Shake a Vector3 called myVector in 1 second
DOTween.Shake(()=> myVector, x=> myVector = x, 1, 5, 10, 45, false);
static DOTween.ToAlpha(getter, setter, float to, float duration)
Tweens the alpha of a Color from its current value to the given one.
getter A delegate that returns the value of the property to tween. Can be written as a lambda like this: ()=> myValue
where myValue is the name of the property to tween.
setter A delegate that sets the value of the property to tween. Can be written as a lambda like this: x=> myValue = x
where myValue is the name of the property to tween.
to The end value to reach.
duration The duration of the tween.

// Tween the alpha of a color called myColor to 0 in 1 second
DOTween.ToAlpha(()=> myColor, x=> myColor = x, 0, 1);
static DOTween.ToArray(getter, setter, float to, float duration)
No FROM version.
Tweens a Vector3 to the given end values. Ease is applied between each segment and not as a whole.
getter A delegate that returns the value of the property to tween. Can be written as a lambda like this: ()=> myValue
where myValue is the name of the property to tween.
setter A delegate that sets the value of the property to tween. Can be written as a lambda like this: x=> myValue = x
where myValue is the name of the property to tween.
endValues The end values to reach for each segment. This array must have the same length asdurations.
durations The duration of each segment. This array must have the same length as endValues.

Examples

// Tween a Vector3 between 3 values, for 1 second each
Vector3[] endValues = new[] { new Vector3(1,0,1), new Vector3(2,0,2), new Vector3(1,4,1) };
float[] durations = new[] { 1, 1, 1 };
DOTween.ToArray(()=> myVector, x=> myVector = x, endValues, durations);
static DOTween.ToAxis(getter, setter, float to, float duration, AxisConstraint axis)
Tweens a single axis of a Vector3 from its current value to the given one.
getter A delegate that returns the value of the property to tween. Can be written as a lambda like this: ()=> myValue
where myValue is the name of the property to tween.
setter A delegate that sets the value of the property to tween. Can be written as a lambda like this: x=> myValue = x
where myValue is the name of the property to tween.
to The end value to reach.
duration The duration of the tween.
axis The axis to tween.

// Tween the X of a Vector3 called myVector to 3 in 1 second
DOTween.ToAxis(()=> myVector, x=> myVector = x, 3, 1);
// Same as above, but tween the Y axis
DOTween.ToAxis(()=> myVector, x=> myVector = x, 3, 1, AxisConstraint.Y);
Virtual Tween
static DOTween.To(setter, float startValue, float endValue, float duration)
Tweens a virtual property from the given start to the given end value and implements a setter that allows to use that value with an external method or a lambda.
setter The action to perform with the tweened value.
startValue The value to start from.
endValue The value to reach.
duration The duration of the virtual tween.

DOTween.To(MyMethod, 0, 12, 0.5f);
// Where MyMethod is a function that accepts a float parameter
// (which will be the result of the virtual tween)

// Alternatively, with a lambda
DOTween.To(x => someProperty = x, 0, 12, 0.5f);

        
打赏
— 于 共写了7357个字
— 文内使用到的标签:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据