What just happened?

We have just created a background for our main menu scene. As you have noticed, this item is not parented to any part of the scene and will display without respect to any other items in the scene. Now we can move on to adding buttons to our menu.

Putting the menu on the screen

To make the immediate mode GUI work with Unity we simply need to provide one function in a script, OnGUI. Like any other script we will attach our UI script to a GameObject that is located in the scene and Unity will call it every frame to ensure that any GUI events that have happened during that frame are processed. One obvious warning is that this means that your GUI loops should be tight loops and highly optimized; you shouldn't be performing complex time-consuming computations inside the GUI code.

If we look back at our main menu example we know that we have two simple buttons that need to perform very specific actions when clicked. Thus, from a pseudo code perspective, we know that our application simply needs a basic if/else condition to examine the two controls that we're using.

// Use this for initialization
if ( MainMenuButton( control_x, control_y, width, height) )
{
  loadMainGameScene();
}
else if ( CreditsMenuButton( control_x, control_y, width, height)  ) 
{
  loadCreditsScreen();
}

In this example MainMenuButton and CreditsMenuButton represent two GUI controls that we will define in our script that will cause the main scene or credits scene to be opened respectively.

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

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