[Unity Study Notes] Basic Introduction to DOTween (1)

Insert image description here

Most of the content in this article is learned from DOTween official documents


What is DOTween?

DOTween is an animation plug-in, and Tween means tween. This plug-in is hereinafter referred to as DOT. DOT is very convenient to use. You only need to remember four classes and three methods:

four categories

  • Tweener
  • Sequence
  • Tween
  • Nested Tween

three methods

  • DO
  • Set
  • On

DO

The method names in DOT are very unified, so we can roughly know what this method does and how to use it from the prefix.

  • DO represents the method of implementing animation. It is the prefix of a shortcut method for calling tween animation. We can directly start from behind the object we want to apply animation (such as object transform, or material texture, text text, color color, etc.) Click on the DO method where you want to use tween animation:
transform.DOMoveX(100, 1);// 只需在物体的transform后面使用DOMoveX方法,就直接实现了物体在x轴上的移动
transform.DORestart();
DOTween.Play();

Set

  • The method prefixed with Set is used to change the settings of a tween. Usually the Set method can be set for Tween and its derived classes. Set can be appended infinitely after Tween.
var tweenerA = buttonA.DOMove(new Vector3(700, 200, 0), 5)
.SetLoops(3,LoopType.Yoyo).SetSpeedBased();
myTween.SetLoops(4, LoopType.Yoyo).SetSpeedBased();
// 展示一下无限set
myTween.SetSpeedBased().SetSpeedBased().SetSpeedBased().SetSpeedBased();

On

  • The methods prefixed with On represent the callbacks of the tween animation that can be called. These callbacks will be triggered at the corresponding moments of the tweening animation and their internal function delegates will be called.

Official example:

// 委托回调也可以无限调用
myTween.OnStart(myStartFunction).OnComplete(myCompleteFunction);

dispatch a single anonymous function

tweener.OnStart(()=> {
    
     Debug.Log("动画开始"); });
// 在Tweener开始时调用委托

Scheduling the official delegation types provided by DOTween:TweenCallback

TweenCallback tweenCallback = new TweenCallback(() => {
    
     Debug.Log("动画开始"); });
tweenCallback += () => {
    
     Debug.Log("我是TweenCallback委托"); };
tweener.OnStart(tweenCallback);
// tweener动画开始时调度委托
// 输出:动画开始
// 我是TweenCallback委托

Same Actionas, TweenCallbackis a no-return value delegate that accepts generic parameters,


Tweener

  • Tweener tweening, a class used to control tweening interpolation and animation. Most of the time what we need to manipulate is the Tweener class:
Tweener tweener;
private void Start()
{
    
    
    tweener = transform.DOMove(new Vector3(500, 500, 0), 5);
    var twe = transform.DOMove(new Vector3(500, 500, 0), 5);
    Debug.Log(tweener.GetType());
    Debug.Log(twe.GetType());
}

After checking the code and obtaining the type, we found that the type returned by the DO method is the TweenerCore type, which inherits from Tweener. Therefore, according to the Liskov substitution principle, Tweener can be converted to TweenerCore.


Sequence

  • Sequence sequence, a special kind of tweener, which does not control a certain value, but controls other tweens and animates them as a sequence.
private void Start()
{
    
    
    tweener = transform.DOMove(new Vector3(500, 500, 0), 5);
    var tweenerA = buttonA.DOMove(new Vector3(700, 200, 0), 5);
    var seq = DOTween.Sequence();
    seq.Append(tweener);
    seq.Append(tweenerA);
}

Similar to delegation, Sequence can execute tweened animations sequentially, and of course can also set scheduled execution at a certain time node.

var s = DOTween.Sequence();
var Cube1RunTime = 1.0f;
var Cube2RunTime = 1.0f;
s.Append(this.m_Trans.DOLocalMoveX(2.0f, Cube1RunTime));
s.Append(this.m_Trans.DOLocalMoveX(-3.42f, Cube1RunTime));
//在队列动画开始后的Cube1RunTime秒后播放
s.Insert(Cube1RunTime, this.m_Other.DOLocalMoveY(2.5f, Cube2RunTime));

Specific usage will also be introduced in the future.


Tween

  • Tween , the collective name of Tweener and Sequence. From the code point of view, it is the base class of Tweener and Sequence, and it is also the most basic class of the DOTween plug-in.

Nested tween

  • Nested Tween Nested tweens, tweens contained in Sequence. You can't see it in the code, it should be an alias of Tween inside Sequence.

initialization

Before using DOTween, you need to initialize it. The simplest way to initialize it is to do it after importing, as shown in the figure below:
Insert image description here
Of course, you can also use code to perform default initialization, and the official recommendation is that if you want to set the initialization yourself, you can use the following code

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

// recycleAllByDefault: true自动回收,如果Tween被OnKill时不是销毁而会被回收到池中,不允许GC对他们销毁,而有可能被再次调用重生
// useSafeMode: true安全模式,补间有可能变慢,但换来的是更安全的操作
// LogBehaviour: 日志行为,根据选择模式设置日志
public static IDOTweenInit Init(bool? recycleAllByDefault = null, bool? useSafeMode = null, LogBehaviour? logBehaviour = null);

Usage

DOThere are two calling methods when using Tween . One is the prefix method we have introduced . This method is a quick call to the complete method. It can be set directly after the calling object, and there is no need to specify a calling delegate:

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

Using DOmethods, in addition to tweening from the initial value to the set value, if we add From()a method, we can tween from the set value to the initial value:

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

In addition to the shortcut method, there is also a complete method to use:

// 使用静态方法定义一个Tween
// 四个参数分别是getter,setter,to,duration
// getter,setter是泛型委托,它们并不支持所有的泛型,定义里只定义了一部分的类型
// getter委托用于接受初始值的类型
// setter委托用于接受初始值并作为入参
// to参数代表了最终值
// duration参数代表了从初始值补间到最终值的时间
// 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);

The DOTween.TO() method does not apply tweening directly, but assigns tweening over time from the initial value to the final value to the data of the accepted type. For example:

Vector3 transform= Vector3.zero;
DOTween.To(()=> transform, x=> transform= x, new Vector3(3,4,8), 1);

Then within one second, the transform will (0,0,0)change from to (3,4,8), but because there is no set object's transform, the object itself does not move.


The above are some basic usages of DOTween. I believe you will know what they mean when you see the function name. DOTween provides many tweening methods. The advanced usage is very powerful. It can even be connected to AVpro: excerpted from Unity
DOTween . To
Insert image description here

Guess you like

Origin blog.csdn.net/milu_ELK/article/details/132474063