A simple introduction to the Unity Dotween plugin and sample code

Source: Internet
Author: User

Unity inside do interpolation animation plug-ins have many, more common have itween, Hotween, Dotween. According to the feedback and actual experience, the Dotween plug-in is very prominent in flexibility, stability and ease of use. Here is a brief introduction of its usage, and in the following article to do some examples of effects, or good.

The so-called "interpolation animation", as the name implies, is to insert other values in two values to achieve animation. The principle is very simple, for example, to get an object from A to B, we only know the coordinates of a and B, interpolation animation can be based on "easing function" to determine the other values between A and B, to achieve the object from A to B "motion process." "Easing function" is a function to determine "interpolation", here are all the easing function, graphical representation, very convenient. At the end of the article, we will introduce several commonly used easing functions, and intuitively feel their differences according to the code effect.

Dotween official documents only this one page, the inside is already very detailed, here is a little narration.
Implement a Dotween animation that has a "common mode" (the generic Way) and a "Simple Mode" (the shortcuts means) in two ways.
The code format for common mode is as follows:

//让myVector向量在1秒内变为 Vector3(3,4,8)向量DOTween.To(()=> myVector, x=> myVector = x, new Vector3(3,4,8), 1);//让myFloat变量在1秒内变为52DOTween.To(()=> myFloat, x=> myFloat = x, 52, 1);

I've never used this pattern before ... Let's just look at the simple notation.

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

There is no need to explain the above, are so intuitive to write.
It is worth noting that the Dotween has a from () function, which indicates that the object has been written from the previous position/color/size change to the current state, this function is very useful.
In the Dotween official documentation, the detailed list of Dotween can support dynamic projects. More commonly used transform under Domove,doscale,dorotate,ui under Dofade,docolor.

Let's take a look at some practical applications.

Example one:


Source

Sphere1. Domovex (20,1). Setease (Ease. OutBack). Setrelative (); sphere2. Domovex (20,1). Setease (Ease. Inquad). Setrelative ();sphere320,1) ;sphere420,1) ;sphere520,1)            

The code above causes the sphere to move +20 in the x-axis relative to (setrelative ()) within 1 seconds. The Setease () function in the middle determines that the easing function can be used to show that different easing functions can achieve different animations over-effects.
5 small balls at the same time to reach the end, but the movement process is not the same, some first fast and slow, some first slow and fast, some will surpass the end to retrace.

Example two:

Source

void Show(){    //boxes引用了图片中的那些方块Transform    foreach(var j in boxes) j.DOScale(new Vector3(1,1,1),0.5f);}void Hide(){ foreach(var j in boxes) j.transform.DOScale(new Vector3(0,0,0),0.4f);}

Here is a small app I wrote recently, animation effect is OK, but the actual code on the above few lines. I will not explain, it is too simple.

Example three:

Source

void ShowAnimation(){     StartCoroutine(Show());}IEnumerator Show(){    //items数组引用了图片中的那几个长条Transform    foreach(var item in items) { item.DOLocalMoveX(-1000,1f,false).From().SetEase(Ease.OutBack); yield return new WaitForSeconds(0.02f); }}

Here in order to achieve the effect of the animation gradually began to use the coroutine, we have to say unity is really useful ah.

Said so much, in fact, the use of dotween is not difficult, the code is simple, easy to understand, the real difficulty is still in the ease of the full understanding of the function.
Just a few extra exercises.

A simple introduction to the Unity Dotween plugin and sample code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.