The Notify
parameter in the Inspector window we used in previous chapters to call methods on a precise event is usually enough for sending messages. However, you may need to send a message to another GameObject and maybe to its children too.
That's where the UIButton Message component comes in handy. We will use this to make our MainMenu GameObject scale down before the game actually quits:
MainMenu
into its Target field.CloseMenu
in the Function Name parameter.AppearFromAbove.cs
script.In this script, add a simple CloseMenu()
method containing the following lines:
void CloseMenu() { //Tween the menu's scale to zero TweenScale.Begin(this.gameObject, 0.5f, Vector3.zero); }
Now we need to delay the execution of the Application.Quit()
method, otherwise we won't see our Tween. We do this using the following steps:
QuitNow()
method to actually exit the application as follows:void QuitNow() { Application.Quit(); }
Save the script and click on the play button. When you exit the application, our Menu will automatically disappear. That's because the Invoke()
method enables us to call a function with delay
as the second parameter.
Let's add this nice scaling effect also when the player launches the game by performing the following steps:
LaunchValidator.cs
script.public GameObject menuContainer;
LaunchValidator.cs
script. We simply have to replace the Application.LoadLevel("Game")
line with the following lines:menuContainer.SendMessage("CloseMenu"); Invoke("LaunchNow", 0.5f);
LaunchNow()
method to actually launch the game scene as follows:void LaunchNow() { Application.LoadLevel(levelName); }
Great, on exit or game launch, the menu scales out, which makes a nicer transition. We have also learned how to use a UIButton Message component.
3.144.47.218