Author Topic: Tweens  (Read 45418 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Tweens
« on: November 23, 2013, 05:03:46 AM »
Overview

NGUI comes with a small but powerful Tween library.



To add a tween to a widget, you can right click on it and choose the appropriate selection from the Tween menu. Alternatively, or if you are not selecting a widget, you can go through the NGUI menu (NGUI -> Tween). Last but not least, you can also simply add the appropriate script by name, if you prefer.

Regardless of the route you take, once you add a tween to an object, that tween will be active by default, and as soon as you hit Play you will notice that the tween will be playing. If you don't want it to play right away, simply disable the component by unchecking the checkbox next to the script name.

Although there are a variety of different tweens, they all inherit from the same base class (UITweener) and have the same common functionality. They all have a From and a To fields representing the starting and final values. They also all have an Animation Curve that lets you fine-tune how the values will interpolate from one to the other.

The Duration field lets you specify how long it takes for the tween to fully animate, in seconds. You can also add a Start Delay in case you don't want the tween to play right away. This value is also in seconds.

Tween Group lets you have several similar tweens on the same object, and trigger them selectively by matching the group ID.

Now speaking of triggering tweens, you can do so manually by calling their Play() function by specifying the function in the OnClick section on the button, for example.



You can also take a more advanced route by attaching a UIPlayTween script (Attach -> Play Tween Script). It gives a fair bit more options for you to play with.



First you will want to specify a Tween Target. This will be the game object that will have the tween, or several tweens, that you will want to trigger. You can also optionally make it activate tweens on all child objects by marking the Include Children option.

Like in the actual tween above, the Tween Group field lets you be selective about which tweens you will trigger.

Trigger condition lets you specify what kind of action will trigger the tween to play. Play direction lets you choose which way it will play. If the target is disabled, you can choose to activate it or let it remain disabled (This includes tweens that were marked disabled on purpose so they wouldn't play right away, in case you're wondering!)

If the tween is already playing you can choose to restart it from the beginning, have it continue from where it is, or have it restart only if it already finished playing. You can also have the tween disable the target When finished -- a useful thing to do when you are using tweens to transition from one menu to another.

Finally, if you want to execute some remote function once the tween finishes playing, you can do so by dragging the targeted game object into the Notify field and choosing the appropriate function from the list. Like all NGUI notifications it must be of "public void FuncName (void)" type, like this:
  1. public void MyNotification ()
  2. {
  3.     Debug.Log(UITweener.current.name + " has finished playing!");
  4. }

Pro-Tips
  • You can create a tween that goes from the beginning to the end and then returns to the beginning by adjusting the Animation Curve, making it be shaped like an inverted "U".
  • You can have tweens trigger each other in a daisy chain by having one tween's OnFinished notification trigger another tween's Play() function.
  • To create a tween via code, use its Begin() function like so:
  1. TweenPosition.Begin(tweenGameObject, duration, targetPosition);

    Class Documentation

    http://tasharen.com/ngui/docs/class_u_i_tweener.html

    If you have a question regarding this component or would like me to clarify something, just post a reply here.
    « Last Edit: November 23, 2013, 06:27:43 AM by ArenMook »

    OnlineCop

    • Jr. Member
    • **
    • Thank You
    • -Given: 0
    • -Receive: 0
    • Posts: 51
      • View Profile
    Re: Tweens
    « Reply #1 on: November 26, 2013, 07:28:46 PM »
    Could you provide an example of chained tweens? Like how to scale up an object, then rotate it once, then scale it back down?

    ArenMook

    • Administrator
    • Hero Member
    • *****
    • Thank You
    • -Given: 337
    • -Receive: 1171
    • Posts: 22,154
    • Toronto, Canada
      • View Profile
    Re: Tweens
    « Reply #2 on: November 26, 2013, 09:27:54 PM »
    Have you seen this? http://www.youtube.com/watch?v=OT0hTxjjkY4&list=UUQGZdUwzE8gmvgjomZSNFJg#t=881

    I create several chained tweens in that video where each tween finishing triggers another.

    OnlineCop

    • Jr. Member
    • **
    • Thank You
    • -Given: 0
    • -Receive: 0
    • Posts: 51
      • View Profile
    Re: Tweens
    « Reply #3 on: November 26, 2013, 11:50:58 PM »
    I may have missed it, but does this also provide details on how to chain Tweens via code?

    http://www.youtube.com/watch?feature=player_detailpage&v=uNSZsMnhS1o#t=73 discusses changes to how tweens work and are called, but it does not go into chaining them.
    « Last Edit: November 27, 2013, 12:02:07 AM by OnlineCop »

    ArenMook

    • Administrator
    • Hero Member
    • *****
    • Thank You
    • -Given: 337
    • -Receive: 1171
    • Posts: 22,154
    • Toronto, Canada
      • View Profile
    Re: Tweens
    « Reply #4 on: November 27, 2013, 12:36:51 AM »
    No that video just talks about how to upgrade.

    uberalles

    • Newbie
    • *
    • Thank You
    • -Given: 0
    • -Receive: 0
    • Posts: 4
      • View Profile
    Re: Tweens
    « Reply #5 on: November 27, 2013, 01:17:22 AM »
    This reply is really meant for Aren, I thought others might be in the same situation though. First off, these documentation posts look great/helpful. My only problem is that most tutorials cover pre-3.x versions of NGUI and I was wondering if you knew of some sort of learning project or tutorial that would be a great starting place for the newer version(s) (just bought NGUI this afternoon so I'm pretty new to the whole thing).

    Thanks in advance for any advice you can offer.

    ArenMook

    • Administrator
    • Hero Member
    • *****
    • Thank You
    • -Given: 337
    • -Receive: 1171
    • Posts: 22,154
    • Toronto, Canada
      • View Profile
    Re: Tweens
    « Reply #6 on: November 27, 2013, 01:26:44 AM »
    The videos I have linked at the top of the Overview post are all for 3.0+. Although with 3.0.6 creating UIs is quite a bit easier now that you can just drag & drop prefab controls directly into your scene to get started, so I'm going to have to redo them (again) soon. I'm thinking with 3.0.7 layout system changes. Anything in particular you want covered more thoroughly?

    uberalles

    • Newbie
    • *
    • Thank You
    • -Given: 0
    • -Receive: 0
    • Posts: 4
      • View Profile
    Re: Tweens
    « Reply #7 on: November 27, 2013, 08:25:25 AM »
    I feel like I'm complaining even though there's quite a lot of info already on this forum. I was just looking for some sort of tutorial project to work on but I think there's enough to go off of from this forum. If you were to make another video I wouldn't mind seeing something about using UIs within 3D space, having a menu pop out of an object for example.

    zipper

    • Newbie
    • *
    • Thank You
    • -Given: 0
    • -Receive: 0
    • Posts: 14
      • View Profile
    Re: Tweens
    « Reply #8 on: November 28, 2013, 11:47:39 PM »
    Many GUIs I create need something to trigger when the tween is finished moving forward, and a different set of things to trigger when the tween is finished in reverse.

    Is this something that could be added? Or am I missing something obvious that is already in NGUI?

    I was thinking of:
    OnFinishedForward
    OnFinishedReversed

    ArenMook

    • Administrator
    • Hero Member
    • *****
    • Thank You
    • -Given: 337
    • -Receive: 1171
    • Posts: 22,154
    • Toronto, Canada
      • View Profile
    Re: Tweens
    « Reply #9 on: November 29, 2013, 10:42:29 AM »
    That's an interesting thought, zipper. I can think of a few use cases for something like that... I'll most likely add that feature, thanks.

    Ernest

    • Newbie
    • *
    • Thank You
    • -Given: 0
    • -Receive: 1
    • Posts: 40
      • View Profile
    Re: Tweens
    « Reply #10 on: December 12, 2013, 05:37:04 AM »
    Can't we have a OnEnable() event on UIPlayTween?

    chirhotec

    • Newbie
    • *
    • Thank You
    • -Given: 0
    • -Receive: 0
    • Posts: 5
      • View Profile
    Re: Tweens
    « Reply #11 on: December 12, 2013, 11:57:44 PM »
    I just downloaded the newest version of NGUI, Playmaker the NGUI-Playmaker bridge (https://hutonggames.fogbugz.com/?W1111). The bridge is only set to support 3.0.5, but I thought I'd give it a try and see if it works.

    The only problem that I see so far, is that the UITweener Reset function is set to private (by access modifier omission) in one of the latest versions, and the NGUI-Playmaker bridge makes calls to it externally. According to the UITweener documentation, this function is listed under Public Member Functions (http://tasharen.com/ngui/docs/class_u_i_tweener.html#a79fb68d0ae50721d7fe351d8725bbb01). Any reasons not to make it public?

    ArenMook

    • Administrator
    • Hero Member
    • *****
    • Thank You
    • -Given: 337
    • -Receive: 1171
    • Posts: 22,154
    • Toronto, Canada
      • View Profile
    Re: Tweens
    « Reply #12 on: December 13, 2013, 05:53:35 AM »
    As it turns out, Reset() is a specific keyword within Unity's Monobehaviours. The Reset() function is called every time you choose the "Reset" option from the right-click menu, and is also called as soon as you add the script to the game object -- which is why tweens were assuming the starting value as soon as you added them (which was annoying).

    What UITweener.Reset() used to do is now in UITweener.ResetToBeginning().

    rain

    • Jr. Member
    • **
    • Thank You
    • -Given: 2
    • -Receive: 4
    • Posts: 79
      • View Profile
    Re: Tweens
    « Reply #13 on: January 28, 2014, 12:21:35 PM »
    What happened to the UITweener's OnEnabled function? Where can I find the replacement for this(if such exists) in the new version?

    ArenMook

    • Administrator
    • Hero Member
    • *****
    • Thank You
    • -Given: 337
    • -Receive: 1171
    • Posts: 22,154
    • Toronto, Canada
      • View Profile
    Re: Tweens
    « Reply #14 on: January 29, 2014, 10:43:52 AM »
    What was in it that you need?