Sending messages

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:

  1. Select our Exit GameObject and perform the following steps:
    1. Attach a Button Message component to it by navigating to Component | NGUI | Interaction.
    2. Drag our Container GameObject from MainMenu into its Target field.
    3. Type in CloseMenu in the Function Name parameter.
  2. Select our Container GameObject in MainMenu and open its attached 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:

  1. Select our GameManager GameObject and perform the following steps:
    1. Open its attached GameManager.cs script.
    2. Replace the line in the ExitPressed() method with the following line:
      //Call the exit function in 0.5s
      Invoke("QuitNow", 0.5f);
  2. Add a new 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:

  1. Select our Play button GameObject and open its attached LaunchValidator.cs script.
  2. We will need our Container GameObject from MainMenu. Declare it as follows:
    public GameObject menuContainer;
  3. Go back to Unity and assign this new menuContainer variable as follows:
    1. Select our Play button GameObject and drag our Container GameObject from MainMenu to its MenuContainer field.
    2. Go back to our LaunchValidator.cs script. We simply have to replace the Application.LoadLevel("Game") line with the following lines:
      menuContainer.SendMessage("CloseMenu");
      Invoke("LaunchNow", 0.5f);
  4. Ok, now we can add a new 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.

Note

We didn't need the Include Children Boolean checked. But it's interesting to note that you can send a message to the target and all its children at once.

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

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