One great aspect of NGUI is that you can use Unity's animation system on any kind of widget. There also are some Tween components that let you modify most values over time, such as dimensions, color, and scale. For example, you can change an object's color from color A to color B in 5 seconds.
We have a nice main menu. But we actually have our options that are constantly displayed. That is not very user friendly.
We will use animations and tweens to hide our options and show them only when the user clicks on the Options button. With options hidden, our menu will look as shown in the following screenshot:
But first, let's make our powers' apparition smoother.
Let's add Scale Tweens on our prefabs to make them appear smoothly by performing the following steps:
0
, 0
, 0
,}.0.2
.Now, as soon as these widgets are created, they scale from 0 to 1 in 0.2 seconds, which makes them appear smoothly.
We can now see how we'll hide and show options using a button.
First, we have to hide our option boxes. To do that, we will use Panel Clipping and increase their width when we need to show them. Let's set up the Clipping option:
515
x 850
.-1
.Container
.0
.515
x 1080
.Good, our options are now hidden. Your Hierarchy should look as shown in the following screenshot:
Let's add an Options button that will show or hide these options:
GameObject from Buttons and rename this new duplicate as Options
.0
, 0
}.140
, 40
, 0
}.140
x 40
.Options
.90
x 25
.Ok, so now we have an Options button. Next, we want it to enlarge our Window and the Panel Clipping width
of Container when clicked. We can do this using code, but we will use tweens and animations in the following manner to see how they work:
515
.1300
.0.5
.515
x 850
.We have a Tween component that will resize the width
of Window when activated. Let's use the UIPlay Tween component to start it when the Options button is clicked on:
Click on play. You will see that the window resizes as needed when Options is clicked. However, the Clipping parameter doesn't. Let's correct this using a Unity animation:
ShowOptions.anim
and perform the following steps:515
for the clipping X Size from UIPanel to add a key.0:30
.1300
for the clipping X Size from UIPanel to add a key.We have our animation ready. Now, let's link the button to the animation in the following manner:
ShowOptions
.Click on play. Our window and clipping both resize perfectly in both directions when the Options button is clicked.
But you can see that our Options widgets aren't visible until you actually drag the main menu around; that's because the clipping is not refreshed after the animation.
UpdatePanel.cs
script to it.UpdatePanel.cs
script.UpdateNow()
method to the script that will force a drag of (0, 0, 0) value on our MainMenu:public void UpdateNow() { //Force a drag of {0, 0, 0} to update Panel GetComponent<UIDraggablePanel>().MoveRelative(Vector3.zero); }
UpdatePanel.UpdateNow
method for the Method field.Great! We have used NGUI's Tween and Play Animations components to enhance our UI and make it nicer and more user friendly.
18.117.188.138