Graphics Reference
In-Depth Information
Once the GUI.matrix is set, everything we draw in the OnGUI function will be scaled
accordingly so we can go on and start drawing the menus.
To set a GUI skin, we need to tell Unity about it in the OnGUI function. You can use
as many different skins as you like, and changing/setting the skin is as easy as
// set the GUI skin to use our custom menu skin
GUI.skin= menuSkin;
In our UI function, menuSkin is a public variable so that we can set the GUI skin
value in the Inspector window of the editor.
The next step is to check the variable whichMenu to see “which menu” we need to
render. here are four different screens we may display here, the first one being the main
menu (case 0):
switch(whichMenu)
{
case 0:
To help us to position the menu and for the most flexibility, we will render it within a
group. Using a group is similar to drawing the UI inside a window, which may be moved
around with its contents keeping the same layout. The coordinate system for the GUI contents
within the group starts at 0,0 in the top left of the group and remains to be unaffected by screen
positioning (which continues to be determined by the group rather than by its content).
GUI.BeginGroup (new Rect (default _width / 2 - 150, default _height /
2 - 250, 500, 500));
Putting content into a group is simple. Use GUI.BeginGroup() within an OnGUI
function to start the content and GUI.EndGroup() once the content is complete.
GUI.Label(new Rect( 0, 50, 300, 50 ), gameDisplayName, "textarea");
One way to get text onto the screen is to use GUI.Label. In the line above, we define
a rectangle where we are going to render (keep in mind that this is happening within a
group, so the coordinate system starts at 0,0 top left of the group). As this is the name of
the game, we are using a string to hold the game name called gameDisplayName. Using
a public string in this way means that we can change the game name in the Unity editor's
Inspector window without having to edit the script.
The final part of the GUI.Label line is a GUIStyle. This is just a string that refers to a
style defined in a GUISkin. In this case, it's the style called “textarea,” and it just makes
sure that when this text is rendered that it uses the correct font, size, and text alignment.
The next part of the script renders and acts on a button. The code to draw and check
for a button press is all in just a single line:
if(GUI.Button(new Rect( 0, 200, 300, 40 ),"START SINGLE", "button"))
GUI.Button uses the defined rectangle to draw a button with the specified text
“START SINGLE” in it. Again, you can see here that, to encourage predictability, we spec-
ify the GUIStyle of “button.” Notice that this line acts like a Boolean in this conditional
statement, and if the button is pressed, everything within the curly brackets occurs:
Search WWH ::




Custom Search