Menu appearance

It would be good to have our main menu window appear smoothly with a gradual scale increase. We'll do that using tweens.

The tweening concept

Tweening is the process of automatically generating intermediate values between two start and end values. You define the start and end values manually, and let the computer do the calculations to go from start to end, applying smoothing methods if requested.

Considering this concept, you can tween scale, position, rotation, and more. Indeed, if we needed to tween our window's scale from 0 to 1, we could simply tween the three X, Y, and Z scale values, and the window will gradually become bigger until it reaches its final size.

With NGUI, we have a component to do just that—the Tween Scale component.

Tween Scale

The Tween Scale component can be added to any GameObject to configure a scale tween from one value to another within a given duration. Let's see how it works.

Usage

Add the Tween Scale component to our main menu window:

  1. In Hierarchy, select our UI Root | Main GameObject.
  2. At the bottom of the Inspector view, click on the Add Component button.
  3. Type scale with your keyboard to search for components with that word.
  4. Select Tween Scale and hit Enter or click on it with your mouse.

Ok, we've just added the Tween Scale component to our main menu window. Let's see what its parameters are before we configure it.

Parameters

Now added to our main menu window, the Tween Scale component has 10 parameters, as follows:

Parameters
  1. From: The X, Y, and Z values for the initial local scale at the start of the tween.
  2. To: Set here the X, Y, and Z values for the final local scale at the end of the tween.
  3. Update Table: If checked, the Table component will be updated during the tween. The Table component is used to align objects in a grid. We will use this component later on.
  4. Play Style: Choose one of these three play styles:
    • Once: The tween is played only once
    • Loop: The tween plays in a loop indefinitely
    • PingPong: The tween plays back and forth indefinitely
  5. Animation Curve: This is the curve used to define the tween's acceleration. Click on the displayed curve to display Unity's Animation Curve editor. At the bottom of the animation curve editor, you can choose one of the presets—they are usually enough to define a tween acceleration method.
  6. Duration: You can define here the tween's duration.
  7. Start Delay: Enter here the desired delay before the tween starts playing.
  8. Tween Group: Set here the group ID for this tween. A tween group is used to differentiate multiple tweens attached to the same GameObject (different groups) or play multiple tweens at once (same group).
  9. Ignore TimeScale: If checked, the tween will always play at normal speed even if Time.timeScale is not at 1. It can be useful to play tweens even when the game is paused using Time.timeScale = 0.
  10. On Finished: Here, you can choose which method you want to call as soon as the tween is finished.

Note

A Tween Scale is played as soon as it's enabled. If you disable it in the editor and enable it during runtime (through code or Inspector), it will play instantly.

Now, we can configure it to smoothly enlarge our main menu at the start.

Configuration

Let's configure it to enlarge our main menu window gradually. Set our Main GameObject's Tween Scale component parameters as follows:

Configuration
  1. Set the From values to {0, 0, 0}.

    Note

    It is recommended that you set a very small scale of 0.01f instead of 0. Indeed, setting a scale of 0 will imply a division by zero for the anchors' calculations, which could lead to unwanted behavior for these anchors.

  2. Leave the To values to {1, 1, 1}.
  3. Set a smoother Animation Curve:
    • Click on the default linear animation curve (3)
    • The Curve editing window appears
    • At the bottom of the window, you'll find curve presets. Click on the one that matches the one on the previous image (3)
    • Close the Curve editing window
  4. Set the Duration to 0.6 seconds.

Hit Unity's play button. Now, our main menu window scales up from {0, 0, 0} to {1, 1, 1} in 0.6 seconds at start!

The Animation Curve we've set makes the ending of the tween smoother—it's called an Ease Out effect on the tween.

OK. Now, let's see how we can make this window scale down when we exit the game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.151.164