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
wheremyValue
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
wheremyValue
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").
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 aVector3
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
wheremyValue
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
wheremyValue
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 aVector3
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
wheremyValue
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
wheremyValue
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
wheremyValue
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
wheremyValue
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
wheremyValue
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
wheremyValue
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 asendValues
.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
wheremyValue
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
wheremyValue
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);