Leantween Unity



UnityLeantween Unity

DOTween is a fast, efficient, fully type-safe object-oriented animation engine for Unity, optimized for C# users, free and open-source, with tons of advanced features It is also the evolution of HOTween, my previous Unity tween engine.

  1. LeanTween.cancel(myButton.gameObject); Leantween.value(change the alpha here); Note: You can either change the alpha using the color property of your button/image/text OR you can change the alpha by adding a Canvas Group that changes the alpha of all child ui components. Either way the above solution works!
  2. Just make sure you download a HoloToolkit from June 16, 2017, or later. This includes this pull request by yours truly that we will need in this app. And while you are importing stuff, also import LeanTween from the Unity Asset Store (hit CTRL-9 to open it immediately without having to hunt it down in the menu).

Leantween Unity Ui

Leantween Unity

Even with Unity 4.3 and its latest 2D features, I am sure a lot of you still use the great 2D Toolkit on top of it. It has so much more great features that Unity does not offer (or only for Pro). For tweening, I use LeanTween, because it has great support and ― nomen est omen ― really operates very slick and lean. The one thing that I found a bit tedious is applying two of my most used tweens: alpha and scaling. Both can’t be applied in the regular fashion on the game object, but should rather be applied directly to the sprite component. So you need to fiddle around with LeanTween.value and write all the same code over and over to accomplish what it takes to do that properly. This also really messes up the code, so I created a nice helper that makes it really easy to do alpha and scale tweening directly on a sprite. All you need is the latest LeanTween (>= V2.031), since it offers some great new features that you’ll need to do that. If it’s not yet available on the Asset Store, you can always grab the latest version directly from Github.

Just create a new script named LeanTweenExtension.cs with the code below or download it ready-made. Then you can simply append setSprite() to your LeanTween alpha or scale statement, and the operation will be applied to the sprite component rather than to the gameobject. Everything else (looping, easing, …) works exactly the same as before. There are just two catches:

ValueLeantween unity script
  • You can’t use your own onUpdate function on that tween, since it is already needed internally to do the magic
  • You can’t scale separate x/y/z values at the same time using LeanTween.scale. While you can (and need to) provide the scale as a Vector3 parameter, only the x component is used and applied to x and y at the same time. This is probably the 90% use case anyway, but if you need to do it separately, you can just use separate and consecutive LeanTween.scaleX/Y/Z calls to achieve the same (The reason is that the LeanTween generic callback function that is used internally here just tweens on a single float, not a vector)

There are two signatures to call: setSprite() and setSprite(tk2dSprite sprite). The first will lookup the tk2dSprite component on the tweened gameobject, with the second you can provide it explicitly if you either tween on a different gameobject that does not own the sprite, or if you simply have it at hand and want to save the additional lookup time. Call it like this:

So as you can see, you can write very clean code even for complex tweens on your sprites. You can of course extend this helper extensions for other operations, but as far as I needed them up to now, these are the two that need special attention when used for sprite gameobjects.

Unity Leantween Vs Dotween

And here is the beef: