Unity - Easy tweens 🥔
Tags: unity3d, tween, animation, opensource
Published: 2020-01-17 23:03:13


Hello 🤓

This is something I wanted to bring to the world. I am talking about **PotaTween**. 

It is a Tween maker for Unity, it's a simple C# Component that helps you create simple tweens and use it either from the Editor or from Code.

## Introduction 🥔

Actually PotaTween was written by someone I don't know. In one of the last companies I worked, I was introduced to this simple tool, that was made by a friend of another developer.

We decided to improve that to fit our needs and the Unity updates such as the TextMeshPro and other ones. Then, by creating a GitHub repo I think we're giving to the world the opportunity to use it, explore, learn, and collaborate. 

> Oh yes, it's a good repo to start collaborating with open-source projects. 😀

## How we use it

Now we already have this repo, and you can check it here https://github.com/GabrielCapeletti/PotaTween

> It's so simple, I don't even know how to explain that.

### Setting from Editor

Basically what we'll need to do is **add the PotaTween component** to the GameObject we want to animate. And then just check the parameters **Play On Enable**, or **Play On Start** from the Inspector. 
Or start the animation from code:

```csharp
tween.SetScale(Vector3.zero, Vector3.one);
tween.Play();
```
As the `tween` variable is a PotaTween type, you can simply attach it in the inspector or say `GetComponent<PotaTween>()`.

### Setting from Code

You just have to `Create` the PotaTween from code. What it'll do is *AddComponent* in the given gameObject.

```csharp
PotaTween tween = PotaTween.Create(gameObject, 0);
tween.SetScale(Vector3.zero, Vector3.one);    // Animates from scale 0 to 1
tween.Play();
```

There is an important thing about the `Create` method. The second parameter, which is the id of the animation; so if you set two animations with the same id, they're going to be overwritten.

---

## A little deeper 🍟

### Can I use callbacks?

This Tween Component allows you to easily set callbacks.
- onStart
- onComplete

These names above are `PotaTweenEvent` which is actually the same of `UnityEvent<PotaTween>`, and you can set them like this: 
```csharp
tween.onStart = new PotaTween.PotaTweenEvent();
tween.onStart.AddListener((PotaTween t) =>
{
    Debug.Log("PotatooO");
});
```

#### What about the easy way? 
The easier way to do something when the animation ends is by adding a callback function as a parameter of the `Play` method.
```csharp
tween.Play(() => {
    Debug.Log("Animation has finished.");
});
```

### Can I reuse my animations? 🥔🍟

Yes. This is one of the changes we did from the original project.
This is called **PotaTweenPreset**. It's an asset that you can create by accessing the Unity menu `Create > PotaTween > Preset`

The Preset works pretty much the same way the PotaTween Component. To use it you will need to reference your Preset asset, so create a variable like this:
```csharp
public PotaTweenPreset blinkTween;
```

To make the Preset work, just initialize it wherever you need
```csharp
PotaTween tween = blinkTween.Initialize(gameObject, 0);
```
This way your animation can be reused everywhere because it is an asset, not a component configuration. You just need to reference it from the inspector.


## That's it. 
> A good thing of PotaTween is simplicity, I hope it can help you

I would like to read your comment about what did you think. 
Do you think it's useful?

# Thanks! 😄