Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

LeanTween - A tweening engine that is up to 5x faster than competing engines!

Discussion in 'Assets and Asset Store' started by dentedpixel, Dec 3, 2012.

  1. bac9-flcl

    bac9-flcl

    Joined:
    Dec 5, 2012
    Posts:
    829
    You don't need any specialized methods to tween a completely arbitrary set of fields. Just create private fields storing values you're tweening from, and another set of fields you're tweening to, then assign whatever you want to them before tweening using the most simple value-based LeanTween mode.

    Code (csharp):
    1.  
    2.     ...
    3.     rectFrom = camera.rect;
    4.     rectTo = new Rect (...);
    5.     LeanTween.value (gameObject, 0f, 1f, time).setOnUpdate (AnimateTweening);
    6. }
    7.  
    8. private Rect rectFrom;
    9. private Rect rectTo;
    10.  
    11. private void AnimateTweening (float value)
    12. {
    13.     float x = Mathf.Lerp (rectFrom.x, rectTo.x, value);
    14.     float y = Mathf.Lerp (rectFrom.y, rectTo.y, value);
    15.     float z = Mathf.Lerp (rectFrom.z, rectTo.z, value);
    16.     float w = Mathf.Lerp (rectFrom.w, rectTo.w, value);
    17.     camera.rect = new Rect (x, y, z, w);
    18. }
    19.  
    With LeanTween.value, you don't really need any other tweening type. It is especially useful when you need to tween more than one field at once - for example, in UI, you might want to simultaneously shift, resize, clip and recolor dozens of elements in a single tween. Doing that with a single animation method lerping between prepared from-to values makes it a simple affair.

    P.S.: If you are unfamiliar with lambda syntax from larku's example, it's doing exact same thing, just expressed in a slightly more compact way. Here is my example with lambda syntax:

    Code (csharp):
    1.  
    2.     ...
    3.     rectFrom = camera.rect;
    4.     rectTo = new Rect (...);
    5.     LeanTween.value (gameObject, 0f, 1f, time).setOnUpdate ((float value) =>
    6.     {
    7.         float x = Mathf.Lerp (rectFrom.x, rectTo.x, value);
    8.         float y = Mathf.Lerp (rectFrom.y, rectTo.y, value);
    9.         float z = Mathf.Lerp (rectFrom.z, rectTo.z, value);
    10.         float w = Mathf.Lerp (rectFrom.w, rectTo.w, value);
    11.         camera.rect = new Rect (x, y, z, w);
    12.     });
    13. }
    14.  
    15. private Rect rectFrom;
    16. private Rect rectTo;
    17.  
    It basically allows you to define an inline method right in the argument instead of passing Action argument pointing to a traditional method. I tend to avoid it, since you might want to reuse the Animate method from multiple places.
     
    Last edited: Oct 27, 2017
    Powzone and dentedpixel like this.
  2. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,259
    Thanks.

    Is is possible to make alphaText work with text that includes rich text color tags? Alpha doesn't appear to change, words with color tags have a constant alpha of 0 or 1 (end and start values), depending on their color, but there's no tweening. Temporarily disabling rich text fixes the alpha problem, but then all the tags become visible as the text is fading out. Words with no color tag work correctly and change alpha as expected. I get the same result with colorText too.
     
    Last edited: Nov 4, 2017
  3. blindmen

    blindmen

    Joined:
    Sep 10, 2015
    Posts:
    34
    @dentedpixel
    Hi there,

    I'm having issue using LeanTween:

    IndexOutOfRangeException: Array index is out of range.
    LeanTween.cancel (UnityEngine.GameObject gameObject, Boolean callOnComplete) (at Assets/Plugins/LeanTween/LeanTween.cs:516)
    LeanTween.cancel (UnityEngine.GameObject gameObject) (at Assets/Plugins/LeanTween/LeanTween.cs:510)
    DentedPixel.LTEditor.LeanTweenVisual.buildAllTweens (Boolean generateCode, UnityEngine.GameObject overrideGameObject) (at Assets/LeanTweenEditor/LeanTweenVisual.cs:523)
    DentedPixel.LTEditor.LeanTweenVisual.Start () (at Assets/LeanTweenEditor/LeanTweenVisual.cs:52)

    Any ideas? Thanks
     
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Can you show the code?
     
  5. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi there, you can increase the available number of tweens available by using:
    LeanTween.init(800);

    The default is 400, but sometimes people have code that accidentally kicks off tweens more often that they realized, so that might also be a source of the problem...
     
  6. Ame

    Ame

    Joined:
    Jul 10, 2012
    Posts:
    8
    Hi @dentedpixel

    First of all, thank you for the asset.

    What i'm here to ask you is a simple question : how can i pause and resume a sequence?
    I'm asking you this, cause i've tried multiple things but none of them is working.

    //Sequence
    Code (CSharp):
    1.  
    2. tw = LeanTween.sequence();
    3. tw.append(5.0f);
    4. tw.append( () => {
    5.  
    6. Debug.Log("hi");
    7.        
    8. });
    9.  
    10. seqId = tw.id;
    11.  
    //1 Try :
    Code (CSharp):
    1.  
    2.  
    3. LeanTween.pause(seqId);//Array index is out of range
    4.  
    5.  
    //2 Try :
    Code (CSharp):
    1.  
    2.  
    3.  tw.tween.pause();
    4. //Half working. If i pause after 2 seconds , it pauses, but :
    5. //if i resume before the total 5 seconds, the log will be always fired at 5 seconds (not counting the seconds of pause)
    6. //if i resume after the total of 5 seconds, the log will be instantly fired as if the tween in the meantime was completed and in order to see the log i should have resumed the tween
    7.  
    8.  
    If i use a simple moveTo action or other actions, everything is ok.

    Thank you for the help.

    Amedeo
     
  7. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,259
    I see that when I do an alpha tween on a canvas image object, it also changes the alpha of that image's children. Is it possible to fade an image's alpha without affecting its children's alphas?
     
    Last edited: Dec 6, 2017
  8. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    I need to move an object at constant speed following a few hundred points. I can set up the points and the spline shows up on the editor but how can i make it move at always the same speed? so far it moves faster/slower at different locations if I use a % to set the position.

    So to be more clear. I want the object to do a full path movement in 3 minutes. Moving at constant speed without acceleration at different points. Any ideas?
     
  9. Carlixyz

    Carlixyz

    Joined:
    Oct 27, 2014
    Posts:
    1
    Hello People, I had been following this tween solution from last year and finally I decided to buy it and it's nice, fast and clean still wondering with the bezier feature.

    My doubt if is there some simple way to use a .setOnComplete() call from script when the tweening finished in the LeanTweenVisual Component ? or at least something to get some Completed event from editor components?
    as Ex.
    Code (CSharp):
    1.  
    2. public LeanTweenVisual   ltGroupTweens ;  // the Component previously configured
    3.  
    4. public void OnEnable()
    5. {
    6.       ltGroupTween = GetComponent<LeanTweenVisual>();
    7.       itGroupTween.setOnComplete(ShowTweenFinished);
    8. }
    9.  
    10. // and then...
    11.   public void ShowTweenFinished()
    12. {
    13. ...
    14. }
    15.  
     
  10. NotQuiteSmith

    NotQuiteSmith

    Joined:
    Oct 27, 2013
    Posts:
    92
    I got LeanTween from the Asset store and imported but when I go to add a line of code I don't get any recognition or intellisense when I type "LeanTween." - any ideas?
     
  11. NotQuiteSmith

    NotQuiteSmith

    Joined:
    Oct 27, 2013
    Posts:
    92
    Hmmm...works if I import it to a new project. What could cause it to be missing from my existing project do you think? Obviously something I've done - just not sure what :-(

    EDIT: I "excluded" and then "included" all the .cs files in Plugins\LeanTween in Visual Studio and that seemed to work. No idea why it missed them the first time.
     
    Last edited: Jan 16, 2018
  12. NotQuiteSmith

    NotQuiteSmith

    Joined:
    Oct 27, 2013
    Posts:
    92
    How do I tween an object to a colour FROM the colour it is now? If vary newColour and I do:

    LeanTween.color(gameObject, newColour, 0.5f);

    It fades from BLACK to the new colour each time - pulsating effect.

    I tried LeanTween.value(Colour, Colour, time) but it didn't seem to do anything. I tried using ".setOnComplete(c => gameObject.GetComponentInChildren<Renderer>().material.color = newColour) at the end and while it permanently updates the objects colour, I still get the black "pulsating" issue.

    EDIT: I'm in a bit of a rush today so posting things too quickly maybe! I tested on a clean scene and .color works as expected. Must be something to do with the material I'm using.
     
  13. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Sorry for the late reply, you can set it not to be recursive with the .setRecursive(false) add-on
     
  14. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Unfortunately that feature hasn't been added yet (pausing a sequence), but I am glad the sequence feature is proving to be popular, I will try and flush out more functionality soon...
     
  15. sathya

    sathya

    Joined:
    Jul 30, 2012
    Posts:
    297
    Hi @dentedpixel,
    Is there any way we can use viewport position(0,0 to 1,1) to tween the UI elements? Right now the only option I found is to use pixel positions. Thanks in advance.
     
  16. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    I think you should be able to hack something with some code like this:

    Code (CSharp):
    1. LeanTween.value(rect.gameObject,0f,500f,1f).setOnUpdate((float val)=>{
    2.                 rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, val, rect.GetSize().x);
    3.             });
    Canvas positioning code is confusing though, so every setup is a little different
     
  17. sathya

    sathya

    Joined:
    Jul 30, 2012
    Posts:
    297
    Hi, Yes canvas positioning system is very confusing. This guy managed to do it. Trying to achieve the same with Leantween as well. https://github.com/GabrielReisESilva/EasyUIAnimator
     
  18. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Not sure if this is useful for you, but I use something a bit like this:

    Code (CSharp):
    1. Vector3 centerPosition = new Vector3(0, 0, 0);
    2. float width = canvasRectTransform.sizeDelta.x;
    3. offScreenLeftPosition = new Vector3(-width, 0, 0);
    4. offScreenRightPosition = new Vector3(width, 0, 0);
    5.  
    6. dialogPanel.localPosition = offScreenLeftPosition;
    7. LeanTween.move(dialogPanel, centerPosition, panelAnimationTime).setEase(panelTweenType);
    8.  
    This could be refactored to normalise the values between 0 and 1.

    Not sure how transferable this is between different configurations, but sharing in case it's useful.
     
    sathya likes this.
  19. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Did a quick search but couldn't find the answer here.

    Getting a compile time error on a callback handler method

    The error I am getting on 'RemoveMe' is
    Cannot convert from Method Group to Action

    Any LeanTween experts able to help?

    Code (csharp):
    1.  
    2.   void someFunc(){
    3. int id = LeanTween.moveX(cubeToRemove, -5.5f, 1f).setOnComplete(RemoveMe).setOnCompleteParam(foo.id, cubeToRemove).id;
    4.  
    5.     }
    6.  
    7.  
    8.     void RemoveMe(int id, GameObject cube){
    9.         Destroy(cube);
    10.     }
    11.  
     
  20. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422


    The problem is the method signature does not match the method signature setOnComplete(...) expects.

    Replace

    .setOnComplete(RemoveMe)

    with

    .setOnComplete(()=>{RemoveMe();})

    And things should work.
     
  21. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,259
    Is is possible to use LeanTween with TextMesh Pro objects? Neither alphaText nor alpha work with TextMeshPro text, whether I pass a rectTransform or a gameObject. I can't find the code that actually sets the alpha, but it should be an easy fix, just changing a definition.
     
    Last edited: Mar 10, 2018
  22. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    You can use LeanTween to change any value on anything:

    LeanTween.value( myText.gameObject, a => myText.color = a, alphaStart, alphaEnd, speed );
     
  23. Smaselll

    Smaselll

    Joined:
    Jul 22, 2015
    Posts:
    9
    Hi!
    I wonder if leantween has function for moving to the given point by random gentle path within certain time? Best wishes!
     
  24. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    302
    Hey everyone, I have a question about moving in local space. I defined a few nodes in the Lean Tween Path component, but when I look at the exported Vector3[], the values are in world space. They change if I move the object's transform around. For some reason, this code still works when I call LeanTween.moveLocal in script.

    Is there a way to tween along a path in local space? Or to get the current world space Vector3[] out of the component? I'm trying to add a generic figure 8 path to a prefab and have it loop on Start(), so I need each instance of the prefab to move in local space.
     
    cyangamer likes this.
  25. cyangamer

    cyangamer

    Joined:
    Feb 17, 2010
    Posts:
    234
    In my case, I need the tweened objects also being translated at a constant rate for a shmup game.

    EDIT: It looks like clicking Refresh on the Tween Path script updates the values for local space and moves the path object to compensate. I'm realizing my issue is probably different from _eternal's though
     
    Last edited: Apr 24, 2018
  26. Justice0Juic3

    Justice0Juic3

    Joined:
    Apr 29, 2013
    Posts:
    188
    What is it with this asset? Is it outdated?

    This happened when I assigned everything correctly. Take a look at the lower left corner. It just skips my waypoints. :eek::(
     
  27. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    305
    Anybody knows it is possible to LeanTween in LateUpdate?
    I need the tween to work after a custom Joint has been resolved

    Thank you
     
  28. amasinton

    amasinton

    Joined:
    Aug 12, 2006
    Posts:
    137
    I've been using LeanTween for years and love it. But I'm stumped on this:

    I'm starting a moveLocal, rotateLocal, scale and a value tween on a list of gameobjects using a for loop. So, that’s four tweens per object at the same time and there are over a hundred objects.

    It runs very slowly. (What a surprise!)

    I’ve broken it up to run in batches of about 10 objects per frame, but I don’t notice any real improvement.

    Is there some way I could structure this more efficiently?

    I’d appreciate your thoughts.
     
  29. Temseii

    Temseii

    Joined:
    Aug 23, 2016
    Posts:
    14
    Hey, I recently bought the editor asset and it seems great! However, for some reason my gameobject is skipping the first bit of the path (visually jumping a little bit ahead instead of smoothly starting movement from the start).

    Gif of the issue:
    https://gyazo.com/79fdac3609521356276213c1bb529639

    Script:
    https://pastebin.com/qymDj3VE

    Any idea why this is happening?

    EDIT: I fixed the issue by adding a new value (0, 0, 0) to the start of the array. Still interested in what exactly happened there though and if there is another way of fixing this.
     
    Last edited: May 22, 2018
  30. Smaselll

    Smaselll

    Joined:
    Jul 22, 2015
    Posts:
    9
    Hi! I have a couple of questions:
    1. Is there any option to move an object from point A to point B with random path (arch shape maybe)?
    2. Is there any option to move an object from point A to point B with the given speed?
     
  31. silentneedle

    silentneedle

    Joined:
    Mar 14, 2013
    Posts:
    280
    @dentedpixel

    LeanTween.isTweening(0) throws the following nullref:

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. LeanTween.isTweening (Int32 uniqueId) (at Assets/Plugins/LeanTween/LeanTween.cs:811)
     
  32. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    How did you get an id of 0? That might not be a valid id that is why it is causing an error. To get the id use code like the following:

    Code (CSharp):
    1. int id = LeanTween.moveX(gameObject, 1f, 3f).id;
    2. if(LeanTween.isTweening( id ))
    3.      Debug.Log("I am tweening!");
     
  33. silentneedle

    silentneedle

    Joined:
    Mar 14, 2013
    Posts:
    280
    I only allow the tween if no tween is running with:

    Code (CSharp):
    1. if (!LeanTween.isTweening(tweenID))
    tweenID isn't set to anything, just a normal int which starts as 0.
     
  34. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    tweenId is not something you set, but something you get from the tweening engine. Like the example above you would set it like:

    int id = LeanTween.moveX(gameObject, 1f, 3f).id;

    and then on subsequent checks you can use this id to see if the tween is still running (and if not you can start a new one).
     
  35. silentneedle

    silentneedle

    Joined:
    Mar 14, 2013
    Posts:
    280
    I know, but imagine the following:

    Code (CSharp):
    1. private int tweenID;
    2.  
    3. private void Update () {
    4.     if (!LeanTween.isTweening(tweenID) && Input.GetKeyDown("space")) {
    5.         // only do something if no tween is running and space key was pressed
    6.         DoSomething();
    7.     }
    8. }
    As a workaround, I'm checking tweenID for zero, but imo isTweening shouldn't throw an exception if the key wasn't found, but it's up to you.
     
  36. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Yeah I see what you mean now. That definitely wouldn't be ideal behavior. I am not getting that error on the current version of the code: https://github.com/DentedPixel/LeanTween/ . It does a sanity check on the id to make sure it is within range... so hopefully when I push this new version it will be fixed?
     
  37. iga112

    iga112

    Joined:
    Jul 19, 2017
    Posts:
    1
    Hi there. I have bunch of tweens in game that move and rotate camera to show off island. But if i pause game (just changing timescale to 0) on when some tweens are running, after unpausing tweens won't resume. So for test case i tried using pauseAll and resume All, that also didn't helped. And after debugging some lean tween code i found out that tweenMaxSearch variable value is actually different from what it was before pausing and after unpausing. Is there any way how to resume my tweens after pauses?
     
  38. sathya

    sathya

    Joined:
    Jul 30, 2012
    Posts:
    297
    Did anyone try using tween system inside the Job system? I want to use LeanTween.Value inside a job system to tween UI elements.
     
  39. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    I would like to use LeanTween to change the velocity of a game object using LeanTween.value to change the velocity Vector3 but I can't figure out how to associate the new Vector with game object.
    Code (CSharp):
    1. LeanTween.value(go,
    2. (value) => {
    3. Debug.Log(go+ " " + value);
    4. go.GetComponent<Rigidbody>().velocity=value;
    5. },
    6. Vector3.zero, new Vector3(x,y,z), 5f);
    Edit: Actually it seems to be working now. I must have been passing in the wrong gameObject before.
     
    Last edited: Oct 9, 2018
  40. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    Is there any way to tween a quaternion in Lean Tween?
     
  41. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi there, so you are resuming by setting the Time.timeScale = 1f and the tweens don't resume? That is a new bug if that is the case. Any repro steps for that would be helpful as I have not seen that myself.
     
  42. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,259
    I just installed the most recent LeanTween update, and it broke all my scripts. I don't know how to tell the version, but reinstalling what I had before the update fixed the problem. The update doesn't show any errors in the console, but when I run I get lots of "referenced script is missing" errors. None of my scripts will load. Other than that, there are no compilers errors, so I'm not sure where the problem is. I don't know if it makes a difference, but the latest version didn't put any files in the Plugins folder. Also, these are JS scripts.
     
  43. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Hi John-B,
    Sorry about that! I have been shifting away from javascript support since as of Unity 2018.2 they no longer support it :(. It should still work if you renamed the Library folder to "Plugins".
     
  44. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,259
    Thanks, I'll try installing the update again and renaming the folder. Are you talking about the Library folder that's OUTSIDE the Assets folder?

    I'm not sure what you mean about Unity 2018.2 no longer supporting JS. I'm using 2018.2.11, and my JS scripts are still working just fine (except for this issue). I know Unity plans to drop JS support at some time in the future, but I didn't think that time is now. I sure hope when they do end JS support, they provide a reliable tool to translate the MANY JS scripts I have.
     
  45. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    Nope! Don't rename that one, rename the LeanTween/Framework one to LeanTween/Plugins .

    That's good to hear 2018.2 still supports it! I heard that they had dropped support for it, but I only see that from forum comments, so it seems they were wrong.

    Yeah I have used C# conversion utility, and I didn't have such great results. I have some old projects that are still in unityscript, but I will probably never resurrect them because I have gotten so used to using C#.
     
  46. habitoti

    habitoti

    Joined:
    Feb 28, 2013
    Posts:
    141
    Hi,

    I just had to update a game from 4 years back, and also updated LeanTween to the latest version. Works great now still with Unity 2018. Just one thing I noticed in LTDescr.setAlpha. The very last branch of walking through renderers doesn't check whether the material actually has a color property (which it doesn't in case one of the children is a ParticleSystem -- it then crashes). Easy fix in LTDescr.cs line 408 is to make sure that the color property exists:

    Code (CSharp):
    1.  
    2. public LTDescr setAlpha(){
    3.      
    4. ...
    5.   foreach (Transform child in trans)
    6.   {
    7.     if(child.gameObject.GetComponent<Renderer>()!=null
    8. ==>   && child.gameObject.GetComponent<Renderer>().material.HasProperty("_Color"))
    9.     {
    10.         Color col = child.gameObject.GetComponent<Renderer>().material.color;
    11.         this.fromInternal.x = col.a;
    12.         break;
    13.      }
    14.   }
    15.  
    16. ...
    17.  
    Regards, habitoti
     
  47. mbadawi-art

    mbadawi-art

    Joined:
    Aug 24, 2018
    Posts:
    3
    Hi!
    I'm trying to use LeanTween to follow a Bezier curve (or a Spline, I don't care really) but I want to be able to specify the orientation of my "follower" on each point of the curve. What I'm trying to do is keep the "forward" vector as it is right now, following the curve, but I want to also be able to rotate my follower around it to make it "twist" around the curve as needed.
    Is that even possible?
    Thanks!
     
  48. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    305
    Hello, Im getting LeanTween id collisions. When I cancel a tween with id in another GameObject, sometimes another tween is canceled in a different gameobject because the same id was assigned to both tweens.

    How can I prevent this?

    Thank you
     
  49. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    305
    Any help with this?
     
  50. GameBrosDevelopment

    GameBrosDevelopment

    Joined:
    Feb 18, 2019
    Posts:
    4
    Hi DentedPixel,

    first of all. LeanTween is awesome! Thanks for creating it. I've been using it for many projects now and I always stumbled into one weird problem on mobile/Android:

    After returning to a game from watching a rewarded ad the tweens seems to be frozen for about 1-2 seconds. After that they continue to their normal behaviour. Any idea what might cause this? Do I have to resume/pause LeanTween if the games focus changes?

    Any clue/help would be highly appreciated. Thanks and kind regards