2 hours to get the use of DoTween in commercial projects

Install the DoTween plugin

First, we need to add the DoTween plugin to our Unity project. We can download the DoTween plugin from the Unity Asset Store and import it into our project.

right! There is a game development exchange group here, which gathers a group of zero-based beginners who love to learn games, and there are also some technical experts who are engaged in game development. You are welcome to exchange and learn.

After importing the DoTween plugin, we need to add it to our project. We can import the DoTween plugin by selecting "Assets" -> "Import Package" -> "Custom Package".

Create Tween animation

Now that we have installed the DoTween plugin, let's see how to create a Tween animation. In this example, we will create a Tween animation that moves an object from its current position to a specified position.

We will create an empty object and name it "TweenObject". Then, we'll add a Box Collider component and a Rigidbody component.

Next, we will create a script and add it to the "TweenObject" object. In the script, we will use the DoTween plugin to create the Tween animation.

In the script, we need to import the DoTween namespace and create an instance of the Tween class. Then, we can use the static methods of the Tween class to create Tween animations.

The following is the complete code implementation:

using UnityEngine;
using DG.Tweening;

public class TweenObject : MonoBehaviour
{
private Rigidbody rb;

void Start()
{
    rb = GetComponent<Rigidbody>();
    MoveObject();
}

void MoveObject()
{
    Vector3 targetPosition = new Vector3(5, 0, 0);
    float duration = 1f;

    rb.DOMove(targetPosition, duration);
}

}

In the above code, we first get the Rigidbody component on the object. Then, we call the MoveObject() method to create the Tween animation. In the MoveObject() method, we define the target position and animation duration, and use the DOMove() method of the DOTween class to create the Tween animation.

After running the script, we can see that the "TweenObject" moves from its current position to (5, 0, 0).

Create a Tween sequence

In addition to creating Tween animations, DoTween can also be used to create Tween sequences. A Tween sequence is a combination of multiple Tween animations that can be played sequentially or simultaneously.

In this example, we will create a Tween sequence that moves an object from its current position to a specified position and then back to its original position.

We will use the "TweenObject" created above and add a new script to it. In the script, we will use the Sequence() method of the DOTween class to create a Tween sequence.

The following is the complete code implementation:

using UnityEngine;
using DG.Tweening;

public class TweenSequence : MonoBehaviour
{
private Rigidbody rb;

void Start()
{
    rb = GetComponent<Rigidbody>();
    MoveObject();
}

void MoveObject()
{
    Vector3 targetPosition = new Vector3(5, 0, 0);
    float duration = 1f;

    Sequence sequence = DOTween.Sequence();

    sequence.Append(rb.DOMove(targetPosition, duration));
    sequence.Append(rb.DOMove(Vector3.zero, duration));
}

}

In the above code, we first get the Rigidbody component on the object. Then, we call the MoveObject() method to create the Tween sequence. In the MoveObject() method, we define the target position and animation duration, and use the Sequence() method of the DOTween class to create a Tween sequence.

We add two Tween animations to the Tween sequence using the Append() method of the Sequence class. The first Tween animation moves the object from the current position to the (5, 0, 0) position, and the second Tween animation moves the object from the (5, 0, 0) position back to the original position.

After running the script, we can see that the "TweenObject" moves from its current position to (5, 0, 0) and then back to its original position.

Guess you like

Origin blog.csdn.net/voidinit/article/details/130600114