LeanTween callbacks

      No Comments on LeanTween callbacks

In this post we will review several LeanTween callbacks and some useful features. First things first letís talk about callbacks. Also, check the previous article about the basics of LeanTween.

callbacks top

Basic callbacks

LeanTween has basic callbacks as setOnStart, setOnUpdate, setOnComplete. Every method takes as a parameter basic C# void delegate – Action. It can come with object parameter or float parameter and object parameter.

OnComplete has addition – setOnCompleteOnRepeat, setOnCompleteOnStart.

setOnCompleteOnRepeat – calls method set OnComplete every loop iteration

setOnCompleteOnStart – calls method set OnComplete at the start of animation.

Full logic of the methods calls showed on diagram:

callbacks diagram

For example:

We need delay-based loop for loading animation like this:

callbacks load 2

We have 4 images (or sprites) that we need to move

public GameObject WhiteDot;
public GameObject BlackDot;
public GameObject RedDot;
public GameObject GreeDot;

And animate them by moving every dot to the next, just like this.

private void AnimationLoop()
{
if(!_isAnimate) return;
LeanTween.move(WhiteDot, BlackDot.transform.position, MoveTime).setEaseInOutCubic();
LeanTween.move(BlackDot, RedDot.transform.position, MoveTime).setEaseInOutCubic();
LeanTween.move(RedDot, GreeDot.transform.position, MoveTime).setEaseInOutCubic();
LeanTween.move(GreeDot, WhiteDot.transform.position, MoveTime)
.setEaseInOutCubic().setOnComplete(AnimationLoop);
}

Here we set OnComplete AnimationLoop method itself. It looks like recursion but it is not.

Another example illustrate the logic of setOnCompleteOnRepeat and setOnCompleteOnStart.

This is simple ping-pong animation created with setLoopPingPong for dot movement. We set OnComplete method for change color of dot at the end.

callbacks nope

LeanTween.move(WhiteDot, targetPosition, MoveTime).setOnComplete(ChangeColor).setLoopPingPong();

As we know setting loop without parameter makes infinite loop and it never calls OnComplete. To get every loop iteration end, we just write:

setOnCompleteOnRepeat(true)LeanTween.move(WhiteDot, targetPosition, MoveTime).setOnComplete(ChangeColor).setOnCompleteOnRepeat(true).setLoopPingPong();

Here is result:

callbacks onRepeat

But at the start dot was white and become green only after first iteration. To deal with it we need setOnCompleteOnStart.

LeanTween.move(WhiteDot, targetPosition, MoveTime).setOnComplete(ChangeColor).setOnCompleteOnStart(true).setOnCompleteOnRepeat(true).setLoopPingPong();

callbacks onStart

We can also destroy object after animation end. Just add setDestroyOnComplete(true).

I hope you like this post, leave your opinion in comments and share this article to your colleagues! Don’t forget to check our blog, we post each end every day from Monday to Saturday.

More interesting things about games development:

Our Twitter: https://twitter.com/TakeGamesTeam
We’re on Facebook: https://www.facebook.com/TakeGamesTeam/
Site: https://take-games.com/
Blog: https://take-games.com/blog/

Leave a Reply

Your email address will not be published. Required fields are marked *