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

HOTween: a fast and powerful Unity tween engine

Discussion in 'Assets and Asset Store' started by Demigiant, Jan 7, 2012.

  1. SilentWarrior

    SilentWarrior

    Joined:
    Aug 3, 2010
    Posts:
    107
    The yoyo is quite easy, as shown. The incremental could be done with :
    float closestIncrement = findClosestIncrementValue(time);
    if( time>= closestIncrement time < closestIncrement+incrementOne )
    Update(closestIncrement);
    The yoyoinverse I dont know, however if it is exactly the same as yoyo but with inverse time, then it should also be easy.

    However its up to you if you want to support it. Personally I would use it if it was available, if it wasnt available then I would probably hack my way around it or something.

    The good thing about implementing all the nice features across the board is that the API stays consistent and people with less "algorithm" skills can still get a good (possibly more optimized) result than they would on their own.

    Anyways, you dont need to implement everything in one go. You can just support the Yoyo loop and implement the others when you feel comfortable with it. They should all be possible, some may require some extra code because of the combinations available and the way the easing types are calculated, but they shouldnt be too much of a problem.

    You can even turn the default way of using HOTween into relying in TweenVars, that way, if you update something in TweenVars that was needed for the HOTween standard, then everyone would benefit from it. Possibly (I cant say for sure) it could be cleaner and faster, because you wouldnt think about "hacks" and "cheats" to implement something if you already have a good working TweenVar for it.

    And since, lets face it, TweenVars are discrete, what ever time slice/time jump/forward or backwards, it all returns the same value everytime, and since they are just equations and conditions they are also fast. But I havent seen your source code, so, you are already probably using optimized code anyways.
     
  2. Robota

    Robota

    Joined:
    Feb 7, 2012
    Posts:
    82
    I would like to know if there is someone who is using HOTween for a 2D game ?
    And what he/she is thinking of it in a 2D context.
     
  3. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    @SilentWarrior: aha, ok, you convinced me. Will add all the loops. :D I thought about that, and shouldn't be complicated (and I'll implement them all: don't want to implement only some loop, because it would become a mess for users, not knowing which LoopType works with what).
    About relying wholly on TweenVars, I don't even want to think about it, after all the time I spent to build the main engine :D Plus, simple tweens practically already use a kind of evolved version of TweenVars.
    By the way, if you want to check the code, it's here.

    @Robota: I am using HOTween for 2D games right now, and I love it - though obviously I'm probably partial about it :D I did a couple of 1-day games (PassiveAggressiveDefender and Journeyballs) and am now developing a full and polished version of Journeyballs for mobile platforms.
     
    Last edited: Feb 29, 2012
  4. Robota

    Robota

    Joined:
    Feb 7, 2012
    Posts:
    82
    Thank you Izitmee for the response.
    Also, I would like to know, what are the main advantages to use HOTween instead of using an AnimationCurve ?
     
  5. SilentWarrior

    SilentWarrior

    Joined:
    Aug 3, 2010
    Posts:
    107
    I like your coding style, I wish I could code like that :D
    Your comment per line of code ratio is off the charts :p , in opposite, mine is very poor, I often do 5k/6k lines of code without a single comment.

    Also you do a lot of spacing, in my code, i usually avoid much spacing at all, because when I am writing it, I feel like having the whole code in front of me instead of scrolling, but then when I have to re-read it later i always regret it
     
  6. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Ahaha I know, I'm quite ridiculous with my comment per line of code ratio :D I usually do comment quite much, because my memory is VERY bad, and I tend to have lots of classes to avoid having to scroll too many lines in a single one. But at least I make short comments, just for me. With HOTween instead I really went off the chart, because I thought that, with it being open-source, it would be more understandable by everybody (and also because I'm using Doxygen to generate the docs from the comments). Though I really hate things like having to comment each member of an enum (though I do it anyway) :p
     
  7. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Mainly, that creating an AnimationCurve at runtime is a pain in the ass (I actually never even dared doing that) :D
    With HOTween (and also with iTween) you can create a tween in one line of code. I have no idea how complicated it might be with AnimationCurves (but if you have any insight I'd be curious). Also, with a tween engine you get a lot more options (like callbacks, different types of loops, and animation controls), you can animate more types of properties (like Rects, paths, etc.), and probably more (I don't know much of the full limitations of using AnimationCurves).
     
  8. Robota

    Robota

    Joined:
    Feb 7, 2012
    Posts:
    82
    @Izitmee
    Ok, thank you. In fact I'am currently playing with HOTween. And it is obvious, now, that HOTween is very easy to understand and very powerfull! Do you plan to make a paid version?
     
  9. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Glad you're finding it easy and powerful. And no plans for a paid version: it will definitely stay free and open-source.
     
  10. Robota

    Robota

    Joined:
    Feb 7, 2012
    Posts:
    82
    Amazing!
     
  11. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    [teal'c]
    Indeed
    [/teal'c]
     
  12. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    I demand you make a subscribe payment so we can donate monthly without having to click every month!
    Or get yourself a flattr : http://flattr.com/
     
  13. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    @ZJP: now that was a cool reference, so cool I had to look up the internet about teal'c, and now I'll laugh and coolishly fake my complete understanding of the joke - so I can look cool too :D

    @himatako: thanks a lot for the encouragement and the suggestions pal :) Flattr looks really interesting, will definitely look into it (though as of now you're in an elite composed of only two donators :D)
     
  14. yuewah

    yuewah

    Joined:
    Sep 21, 2009
    Posts:
    98
    I am using v.0.8.121.

    Code (csharp):
    1. public int score1;
    2.  
    3. void Start () {
    4.      HOTween.To( this, 1f, new TweenParms().Prop("score1", 100 ) );
    5. }
    It throws error on the iphone device.

    ArgumentException: Object type System.Single cannot be converted to target type: System.Int32
    Parameter name: val
    at System.Reflection.MonoField.SetValue (System.Object obj, System.Object val, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
    at System.Reflection.FieldInfo.SetValue (System.Object obj, System.Object value) [0x00000] in <filename unknown>:0
    at Holoville.HOTween.Plugins.Core.ABSTweenPlugin.SetValue (System.Object p_value) [0x00000] in <filename unknown>:0
    at Holoville.HOTween.Plugins.Core.PlugFloat.DoUpdate (Single p_totElapsed) [0x00000] in <filename unknown>:0
    at Holoville.HOTween.Plugins.Core.ABSTweenPlugin.Update (Single p_totElapsed) [0x00000] in <filename unknown>:0
    at Holoville.HOTween.Tweener.Update (Single p_shortElapsed, Boolean p_forceUpdate, Boolean p_isStartupIteration) [0x00000] in <filename unknown>:0
    at Holoville.HOTween.Core.ABSTweenComponent.Update (Single p_elapsed) [0x00000] in <filename unknown>:0
    at Holoville.HOTween.HOTween.DoUpdate (UpdateType p_updateType, Single p_elapsed) [0x00000] in <filename unknown>:0
    at Holoville.HOTween.HOTween.Update () [0x00000] in <filename unknown>:0
     
  15. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    @yuewah: uhm, wonderful, this means that Convert.ToInt32 doesn't work on iOS. I wonder why. Anyway, I posted on Google Code a beta release 0.8.122, which should solve this issue. Can you check it and tell me if now it works (I have no iOS to check it on)?
     
  16. MillieMoo

    MillieMoo

    Joined:
    May 30, 2011
    Posts:
    2
    Just came across HOTween and I'm really impressed, had useable tweens setup in my app within minutes. I'm particularly interested in the pathing functionality and was pleased to see a visual path editor is in the works which would be a big help.

    I've not delved too deeply in there yet but can't quite see it yet so just thought I would ask, is there a way to ask for the current position along the path, similar to the iTween method PointOnPath()? I'm trying to do something similar to the path-constrained characters example on the iTween site.

    Anyway, awesome job, certainly expect to be using HOTween for future work.
     
  17. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Thanks MillieMoo :)

    About the visual path editor, it's in the works, but I have to say that I don't think it'll be ready very soon. I'm working on other enhancements first (while also developing a game and making some payed work :p). Anyway, if you need it, just write me again in a week, and I'll push it onward :)

    About the PointOnPath, actually it can be done, but it's an internal method: didn't think it could be useful for someone else. Will implement it publicly in one of the next updates (possibly this weekend).
     
  18. MillieMoo

    MillieMoo

    Joined:
    May 30, 2011
    Posts:
    2
    Yeah I'm pretty busy with other things so there's no real urgency for the path editor, probably wouldn't be till the end of the month before I'd really benefit from it, just pleased to know it's coming soon:)

    Great news about the PointOnPath method, really appreciate it and I'll look out for it. And again, thanks for all your time and effort on this.
     
  19. iisjreg

    iisjreg

    Joined:
    Mar 25, 2011
    Posts:
    101
    I've just started using the Visual Editor to get a feel for HOTween. I really like it, but would like the ability to convert what I've done to code, so that I could put it into my scripts (or is there something I'm missing?)
     
  20. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Hi iisjreg,

    nope, you're not missing anything: as of now there's no way to convert Visual Editor stuff to code automatically. Actually, that's a thing I never thought of doing: I consider the Visual Editor like a nice toy (though it's built to work well), while scripting directly with HOTween is recommended (mostly because the Editor doesn't support all HOTween features, like Sequences and path animations with PlugVector3Path).

    Personally, I'm using HOTween right now in an Android/iOS game, and I'm doing like this:
    - I do all the main tweens via code (once you learn, it's super-quick: I'm extremely happy about my workflow with it)
    - I use the Visual Editor to add some background animations on the fly, and let those animations be managed by it
     
  21. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    New Update: HOTween v0.8.123:

    Added GetPointOnPath method to Tweeners.
    Usage: after creating a Tweener with a path animation in the usual way (with HOTween.To/From and PlugVector3Path), you can call
    Code (csharp):
    1.  
    2. myTweener.GetPointOnPath( 0.5f ); // where 0.5f is a percentage from 0 to 1.
    3.  
    to get a Vector3 point on the path, which is correctly calculated considering constant speed on path :)

    [EDIT]
    By the way, some of you might've noticed that HOTween's size has dramatically increased by 309kb. Don't worry, it's just because I added a pdb file to the package. It's only used inside the Editor, and allows a better debug.
     
    Last edited: Mar 4, 2012
  22. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Very cool option. What about the rotation?

    JP
     
  23. yuewah

    yuewah

    Joined:
    Sep 21, 2009
    Posts:
    98
    sorry for late to test, HOTween v0.8.122 (beta) still have error on iOS device
     
  24. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    hi there,

    I tried the incremental loop in a sequence with a finite loop count this time and noticed a small glitch , I know you said this was experimental but perhaps this is a obvious fix that can be sorted without to much problems on your side..

    what is happening is this : it does as should until the last step has completed , then seems to apply the incremental value one more time without any interpolation to the target as it completes. I attached some sample code


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Holoville.HOTween;
    4. public class NewBehaviourScript : MonoBehaviour {
    5. private TweenParms parms = new TweenParms();
    6.     private SequenceParms seqParams = new SequenceParms();
    7.     private Sequence seq;
    8.     private int stepCount = 0; 
    9.    
    10.     void Start () {
    11.        
    12.         parms.Prop("rotation", new Vector3(45,0,0),true);
    13.         parms.Ease(EaseType.EaseOutElastic);           
    14.         seqParams.Loops(3, LoopType.Incremental );
    15.         seqParams.OnComplete(onSequenceComplete);
    16.         seqParams.OnStepComplete(onSequenceStep);
    17.         seq = new Sequence(seqParams);
    18.         seq.AppendInterval( 1f );
    19.         seq.Append( HOTween.To( transform, 2 , parms) );       
    20.         seq.Play();
    21.        
    22.        
    23.     }
    24.    
    25.     private void onSequenceComplete(){
    26.         Debug.Log("sequence complete");
    27.     }
    28.    
    29.     private void onSequenceStep(){
    30.         stepCount++;
    31.         Debug.Log("on sequence step "+stepCount);          
    32.        
    33.     }
    34.    
    35. }

    I did download the latest zip from your site ( presuming the download link always returns the latest build ) today

    Thanks
     
  25. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    @yuewah: No problem, and thanks for testing it :) I uploaded a new version 0.8.124, which should definitely solve this issue. Instead of checking if iOS is being used, I went to the base of it and now PlugInt is always used when tweening ints (while before PlugFloat was used and then rounded). Let me know.

    @ZJP: rotation will have to wait :p I need to implement a different rotation calculation when moving on the path: the one which is in use now works ok, but doesn't allow me to implement things like LockAxis. As soon as I'm done with that, I'll be able to implement also a GetRotationOnPath method. As of now, it's a huge coding hell, due to the fact that HOTween works as a timeline which allows to jump to any point in time, and thus I can't rely on previous positions to help on rotation calculations. Thus it might take some time :p

    @tictox: going to look at it right now :)

    Also, I'm sorry but SequenceVar will have to wait for the end of next week. I'm kind of in a mess right now.
     
  26. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    New update: HOTween v0.8.125
    Sorry for posting another update so soon, but tictox, this solves the LoopType.Incremental issue (it was quite dumb of me not to have noticed it before :p) :)

    (oh, and yup, the website download link always returns the latest version)
     
  27. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Sorry, the latest version 0.8.125 had a Debug.Log left in there. Redownload it to clean it (I'm in a real mess today, running to do tons of things) :p
     
  28. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    Thanks Izitmee

    Works perfect now ;)

    I made a small donation today, my currency is very bad when converting up to European money. I normally would prefer not to make a public announcement about donating , but do it now as an example to others using your fantastic library - PLEASE SUPPORT THIS DEVELOPER! hehe
     
  29. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Ehe, thanks a lot tictox, I bow to that with gratefulness, and my internal European currency is bad too, so I find your donation awesome! :)
     
  30. yuewah

    yuewah

    Joined:
    Sep 21, 2009
    Posts:
    98
    HOTween v0.8.125 solved. greatly thank.
     
  31. yuewah

    yuewah

    Joined:
    Sep 21, 2009
    Posts:
    98
    is it possible to Play the HOTween.Sequence / Tweener in x2 speed. Similar to the playback of video recorder.
     
  32. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Sure, you can do it using timeScale. You can either set it during the tween creation, chaining the TimeScale method to TweenParms or SequenceParms, or directly set it from your Tweener/Sequence, like:
    Code (csharp):
    1.  
    2. myTweenerOrMySequence.timeScale = 2;
    3.  
    Note that you can change the timeScale in any moment, even while the tween/sequence is running.
     
  33. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    New Update: HOTween v0.8.130

    - Added WaitForCompletion coroutine to Tweener and Sequence. It can be used inside a coroutine to pause it until the tween is complete.
    C# example:
    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.     // Start coroutine that animates a transform and waits until it completes
    5.     StartCoroutine( MyCoroutine() );
    6. }
    7. IEnumerator MyCoroutine()
    8. {
    9.     // Create the tween and wait for it to complete
    10.     yield return StartCoroutine( HOTween.To( transform, 1, "position", new Vector3(1,1,1) ).WaitForCompletion() );
    11.     // The coroutine will wait until the tween is complete, and then execute the next line of code
    12.     ...
    13. }
    14.  
    Obviously, if you stored a Tweener or Sequence you can also use it from there, like:
    Code (csharp):
    1.  
    2. yield return StartCoroutine( myTweenerOrSequence.WaitForCompletion() );
    3.  
    Don't underestimate this method: it's very practical, and it's making my gaming coding much more neat (yes, I'm happy about it, as you can see) :)
     
  34. SteveB

    SteveB

    Joined:
    Jan 17, 2009
    Posts:
    1,451
    Haha, I keep getting thread updates emailed to me, so I'm forced to think about this and thus forced to ask again...

    ...any news on my runtime parameter adjustments (or whatever we were calling them :D )?

    Thanks bud and good job man!

    -Steve
     
  35. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Haha, uhm, sorry Steve, I'm quite ashamed, but I tried starting it many times with different approaches, but it's a very complicated thing to keep the end value to change smoothly.
    I implemented all that was around it (meaning you can change almost all HOTween parameters, except the dreaded end values of the tween), but this one option will need me to work on it continuously for quite a long time (both of coding than of testing, since it risks to break a lot of stuff), so I postponed it right now. It's still in my todo list, but I have no idea when I'll be able to really get at it. Sorry :p
     
  36. SteveB

    SteveB

    Joined:
    Jan 17, 2009
    Posts:
    1,451
    Fair enough, I promise never to ask of it again! :D

    ...but yea don't worry, I'm getting on without it. How I see it, if and when it arrives, it will be a wonderful surprise!!

    Keep it going bud

    -Steve
     
  37. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Aha, no you actually should ask. I will need some pushing to get to do it, and who knows, maybe one day I will answer "oh, sure, it's already done, I was just waiting for you to ask" :D
     
  38. shoyuken

    shoyuken

    Joined:
    Mar 9, 2012
    Posts:
    2
    Thanks for the update.
     
    Last edited: Mar 9, 2012
  39. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Whew, this was complicated, but it's fixed :)
    Get the new update HOTween v0.8.131.
     
  40. SilentWarrior

    SilentWarrior

    Joined:
    Aug 3, 2010
    Posts:
    107
    Omg, that made my tank cannon recoil sequence so sweet, good stuff.
     
    Last edited: Mar 9, 2012
  41. hima

    hima

    Joined:
    Oct 1, 2010
    Posts:
    183
    YES! This is going to make the code much more leaner and easier to read. :D Thank you for the new awesome feature!
     
    Last edited: Mar 10, 2012
  42. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Ehe, I'm glad you're liking it. It was actually very simple to implement, and I'm loving it. All the pieces of code where I needed to just wait for an animation to complete are so more neat now, which makes me feel both happy than very nerdy :D

    Also, new Update: HOTween 0.8.132
    - Fixed PlugVector3Path OrientToPath bug introduced by Tweener.GetPointOnPath (sorry for that).
     
  43. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    New Update: HOTween v0.8.140

    - Added permanent HOTween mode, via HOTween.Init(true). This will set HOTween not to be destroyed when all tweens are killed, thus reducing memory allocation and garbage collection.
    - Now HOTween gameObject is renamed to show total number of tweens only while inside the Editor.
     
  44. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    Just applied this now for a character using an escalator - Fantastic stuff ;)
     
  45. Mikie

    Mikie

    Joined:
    Dec 27, 2011
    Posts:
    367
    Great work. What are you going to charge when it is finished? Is there any chance I could get the source for speed demo?
     
  46. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Thanks :) I have no intention to charge anything. HOTween will stay free and open-source forever (that's kind of a tradition for tween engines :D).

    You can get the speed demo source in the speed demo page (there's a link in the "HOTween vs iTween" description). Just remember to update it with the latest HOTween DLL, since I don't touch it since a while.
     
  47. mydingefnysen

    mydingefnysen

    Joined:
    Dec 15, 2011
    Posts:
    51
    Hey there! Trying out HOTween and liking it a lot so far! I have a question: I want to toggle some objects alpha (materials alpha) and make sure that they are never stuck in between somewhere. What is the best way to do this? Currently I do like this but feel there is a better way to do it than to use kill, with play and rewind or something like that? If I dont use Kill in this code, if OnInvisible is fired during the tweening OnVisible started things get weird. Any suggestions are appreciated!

    Code (csharp):
    1.  
    2. public GameObject targetGO;
    3.  
    4. float tweenTime = 0.4f;
    5. float valueOn;
    6. float valueOff;
    7. UIWidget[] mWidgets;
    8. public float alphaValue = 0;
    9. TweenParms parms = new TweenParms();
    10.  
    11. void Start()
    12. {
    13.         targetGO = gameObject;
    14.     mWidgets = targetGO.GetComponentsInChildren<UIWidget>();
    15.     parms.OnUpdate(UpdateAlpha);
    16.     parms.Ease(EaseType.EaseOutExpo);
    17.     parms.IntId(this.GetInstanceID());
    18.     HOTween.Init( true );
    19. }
    20.  
    21. override public void OnVisible()
    22. {
    23.     targetGO.SetActiveRecursively(true);
    24.     parms.NewProp("alphaValue", valueOn);
    25.     parms.OnComplete(OnVisibleComplete);
    26.     if (HOTween.IsTweening(this)) HOTween.Kill(this.GetInstanceID());
    27.     HOTween.To(this, tweenTime, parms);
    28. }
    29.     override public void OnInvisible()
    30. {
    31.     parms.NewProp("alphaValue", valueOff);
    32.     parms.OnComplete(OnInvisibleComplete);
    33.     if (HOTween.IsTweening(this)) HOTween.Kill(this.GetInstanceID());
    34.     HOTween.To(this, tweenTime, parms);
    35. }
    36.  
    37. void UpdateAlpha()
    38. {
    39.     foreach (UIWidget w in mWidgets)
    40.        {
    41.            Color c = w.color;
    42.            c.a = alphaValue;
    43.            w.color = c;
    44.        }
    45. }
    46.  
    47. void OnVisibleComplete()
    48. {
    49.     //Do stuff when faded up
    50. }
    51.  
    52. void OnInvisibleComplete()
    53. {
    54.         //Do stuff when faded down
    55.     targetGO.SetActiveRecursively(false);
    56. }
    57.  
    58.  
     
  48. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Hey mydingefnysen

    Got quite a fever here, so my IQ is at the same level of a donkey. Luckily, I don't believe in IQ, though I'll answer anyway, but I hope I won't write something stupid.

    Mainly, seen that you're always animating the same field, it's useless to create/destroy a tween (and thus a Tweener) each time. It would be better and more efficient to simply store it and control it with Restart/Complete (and in this case there would be no need to store TweenParms):

    Code (csharp):
    1.  
    2. public GameObject targetGO;
    3. float tweenTime = 0.4f;
    4. float valueOn;
    5. float valueOff;
    6. UIWidget[] mWidgets;
    7. public float alphaValue = 0;
    8. Tweener tween; // Stored Tweener
    9.  
    10. void Start()
    11. {
    12.     targetGO = gameObject;
    13.     mWidgets = targetGO.GetComponentsInChildren<UIWidget>();
    14.     // Initialize HOTween.
    15.     HOTween.Init( true );
    16.     TweenParms parms = new TweenParms();
    17.     // Here I create the parms inline but with line breaks (avoids retyping "parms." each time)
    18.     parms.Ease(EaseType.EaseOutExpo)
    19.         .IntId(this.GetInstanceID())
    20.         .Prop("alphaValue", valueOn)
    21.         .AutoKill(false) // Prevents the Tweener to be killed automatically when it completes
    22.         .Pause(true) // Set it as paused, so that it can be started when needed.
    23.         .OnUpdate(UpdateAlpha)
    24.         .OnComplete(OnVisibleComplete);
    25.     // Create and store the Tweener
    26.     tween = HOTween.To(this, tweenTime, parms);
    27. }
    28.  
    29. override public void OnVisible()
    30. {
    31.     targetGO.SetActiveRecursively(true);
    32.     // Just restart the tween.
    33.     tween.Restart();
    34. }
    35. override public void OnInvisible()
    36. {
    37.     // Just complete the tween (so it will start from the end)
    38.     tween.Complete();
    39.     // and then play it backwards.
    40.     tween.Reverse();
    41. }
    42.  
    43. void UpdateAlpha()
    44. {
    45.     foreach (UIWidget w in mWidgets)
    46.     {
    47.            Color c = w.color;
    48.            c.a = alphaValue;
    49.            w.color = c;
    50.     }
    51.     if ( alphaValue == 0 ) {
    52.           // Happens only when the tween was playing backwards and it has fully rewinded
    53.           OnInvisibleComplete();
    54.     }
    55. }
    56.  
    57. void OnVisibleComplete()
    58. {
    59.     //Do stuff when faded up
    60. }
    61.  
    62. void OnInvisibleComplete()
    63. {
    64.     //Do stuff when faded down
    65.     targetGO.SetActiveRecursively(false);
    66. }
    67.  
    I really hope I didn't write something silly, cause I'm seeing everything double here :D
     
  49. mydingefnysen

    mydingefnysen

    Joined:
    Dec 15, 2011
    Posts:
    51
    Thanks for your help even though you see double! :D

    I didnt really manage to get it to work that well though, tween.Reverse() fired sometimes but the alpha didnt update and things like that. And wouldnt tween.Complete() force it to become fully visible before animating back to invisible? I tried pausing and reversing but that didnt work.

    Ill try some more with a stored tweener and reverse/restart and see what I can come up with. Hope you get better soon!
     
  50. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Oh yes, tween.Complete() forces it to become fully visible. I thought that was your intention, before reverting to invisible.

    Anyway, I suppose I wrote something silly :p Tomorrow (or maybe later today, if I feel better) I'll try and build an example. Just to be sure I understood correctly: you just want a series of elements to animate to visible/invisible when something happens, and to be automatically deactivated when they go invisible, right?