Rotate and Fade GUI Elements in Unity3d with LeanTween

Rotate and Fade GUI Elements in Unity3d with LeanTween

Apr 25

LeanTween now supports the ability to rotate and fade gui elements. This can be accomplished with remarkably little code compared to other solutions out there.

Below is an example of all of the GUI animation effects that LeanTween now supports. :


Click on the buttons to see each feature in action.
 

Javascript example of fading a button on click:

if(GUI.Button(buttonRect.rect, “Alpha”))
 LeanTween.alpha( buttonRect, 0.0, 1.0, [] );
GUI.color.a = 1.0; // Reset to normal alpha

The buttonRect variable is a LTRect object that is also used in the move and scale features for LeanTween.  All of the usual LeanTween features are as usual supported for these new methods.

Download these Examples in both C# and Javascript here.

15 comments

  1. All i saw in this project is Errors, its not working in Unity 4.0.

    • Thank you for bringing this to my attention. I updated the link with the latest code (you can also download it from the Asset Store to know you are getting the latest).

  2. David

    Hi. I am using leantween in my project. I can only find the LeanTween.alpha(ltRect….) for GUI elements in the .js version and not in the .cs version. Is this correct or do it really exist in the .cs too?

    • Hi David,

      That is correct, the .CS version is about half a year old and doesn’t have the latest improvements. However you should be able to use the .JS version with your C# code, it is fully compatible as long as you place it in your Plugins folder.

    • Disregard my earlier comment the C# version is now up to date with the JS version.

  3. Vitor Souza

    The rotation button of your demo is not working properly, it breaks when you double click it!

  4. Oscar

    Hi. I’m using the LeanTween plugin in my project, and I have a question when working with GUI elements, is it possible to cancel tweens of a GUI element with LeanTween.cancel() or other function?
    Thank you.

    • You are right Oscar there is no method to cancel GUI tweens (thanks for pointing this out!). I have since added one though, so you can cancel a tween like: LeanTween.cancel( ltRect, rectId ); . Grab the latest up on Github to get this new feature.

  5. Thomas Bildsøe

    Hi Russell

    Thanks for a great tweening library it performs really nice, and it works almost perfect for our purposes. Which brings me to the almost part…

    I can’t the onComplete event to work. I’m using LeanTween for animating Gui-elements in and out of the screen. I have a Gui script which I have put on an empty gameObject, and in this I have defined a button which starts a tween. The tween has an onComplete event, and no matter if I set an onCompleteTarget or not, I get the following error message:
    “BroadcastMessage hideMenu has no receiver”

    I’m not sure if this is a bug or if it has to do with my implementation.

    Thanks

    • Hi Thomas,

      I am glad LeanTween is mostly serving your purposes 🙂 .

      I just tested out the “onComplete” event and it looks to be working correctly for GUI elements, like you said you will have to set the “onCompleteTarget” to the gameObject that the script resides on (this is the case for C# users at least). If you can’t get it to work correctly, you can always use the method delayedCall and have the function fire after the amount of time the animation is set to take…

      I hope that helps!

  6. I had problems in use C# version of examples.
    The scale example has the first parameter as a Rect in example, but the argument expected is a GameObject.
    if (GUI.Button(buttonRect1.rect, “Scale Centered”))
    {
    LeanTween.scale(buttonRect1, new Vector2(buttonRect1.rect.width, buttonRect1.rect.height) * 1.2f, 0.25f, new object[] { “ease”, LeanTweenType.easeOutQuad });
    }
    Probably in JS works, but how I fix it in C# version, because I cannot simply convert a LRect to GameObject.

    Congratulations for the work !

    • Hi Mauricio,

      I know this works in C# in the most up-to-date version/examples. I will have to update that link, so as not to mislead people.

      Thanks for the tip, and let me know if you still have issues… (and thanks for the support!)

  7. Thomas

    Hi ! I’m using the LeanTween library and it rocks as far as I can tell. However I’m getting an issue: my game menu is completely animated with LeanTween, I can start a game but when I come back to the menu (Application.LoadLevel) the animations are not working anymore and my GUI elements are not it the right place anymore. Any help ?

    • Thomas

      Never mind, I found the combination of two bugs in my code that caused the issue. Works really nicely now ! When the game will be published I will send you a link if you would like to see it. This library is really nice, thanks a lot !

Leave a Reply