Now that our main menu appears smoothly, let's make sure it disappears smoothly before it exits the game.
In order to make this window scale down, we can simply request the tween to play in reverse. That's where the UIPlay Tween
component comes in: it lets us play any other tween in the scene on a specified event, with different behaviors.
Add the UIPlay Tween
component to our Exit
button:
Button
| Exit
GameObject.play
with your keyboard to search for components with that word.
and hit Enter or click on it with the mouse.Ok, we just added the UIPlay Tween
component to our exit button. Let's see what its parameters are before we configure it.
Now added to our exit button, the UIPlay Tween component has nine parameters, as shown in the following screenshot:
Play Tween
will search for tween components in the Tween Target children and play them too.Toggle
: Tween is played in the opposite direction each timeForward
: The tween will only play in a forward directionReverse
: The tween will only be played in reverseContinue From Current
: Continue playing the current tweenRestart Tween
: Interrupt current tween and restart it nowRestart
If
Not
Playing
: Restart the tween only if it's not playingDo Not Disable
: When the tween is finished, nothing happensDisable After Forward
: Disable the target at the end of the tween when played in a forward directionDisable After Reverse
: Disable the target when the tween ends when played in reverseNow, we can configure it to request the main menu window to play Tween Scale
in the reverse direction to make it disappear on click.
Set our Exit
GameObject's UIPlay Tween parameters as follows:
We can see that our UIPlay Tween on our Exit
button requests our Main GameObject to play in Reverse its attached tweens of group 0. Also, the target (Main), will be disabled automatically when the tween finishes.
Hit Unity's
play button. Now, our main menu window scales down from {1, 1, 1} to {0, 0, 0} when the Exit
button is clicked!
OK. Now, let's make sure we quit the game when the tween is finished.
For the game to exit, we will create a new C# script called MenuManager.cs
in which an Exit()
function will be added. This new Exit()
method will be called at the end of the tween we just created.
Let's start by creating a new MenuManager.cs
script that will hold useful methods:
UI Root
GameObject.MenuManager
on your keyboard. No match will be found.OK, now open our new MenuManager.cs
script, and add this new Exit()
method:
// Method called when we want to exit
public void Exit ()
{
// If currently in Unity Editor
#if UNITY_EDITOR
// Stop play mode
UnityEditor.EditorApplication.isPlaying = false;
#endif
// Exit the game
Application.Quit();
}
This Exit()
method exits the game on its last line. The fourth—highlighted—line of code is only executed in Unity Editor. It makes sure the editor stops playing so that we still have feedback without having to build to check if the Exit()
method works.
Now that we have our Exit()
function, let's link it to the tween's OnFinished
event:
Exit
button.UI Root
GameObject in the OnFinished event's Notify field.Now that we have dragged a GameObject in the Notify field, we can select which method to assign within its attached components. Select the Exit method of MenuManager:
Hit Unity's play button. As soon as the Exit
button is pressed, the window disappears and the Exit()
method is called at the end of the tween, exiting the game or play mode! Now, let's move on to see how we can show the options.
18.118.139.224