Reader small image

You're reading from  Unity Game Development Essentials

Product typeBook
Published inOct 2009
Reading LevelIntermediate
PublisherPackt
ISBN-139781847198181
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Will Goldstone
Will Goldstone
author image
Will Goldstone

Will Goldstone is a longstanding member of the Unity community and works for Unity Technologies as a Technical Support Associate, handling educational content and developer support. With an MA in Creative Education, and many years experience as a lecturer in higher education, Will wrote the first ever Unity book, the original Unity Game Development Essentials, and also created the first ever video tutorials for the package. Through his sites http://www.unity3dstudent.com and http://learnunity3d.com Will helps to introduce new users to the growing community of developers discovering Unity every day.
Read more about Will Goldstone

Right arrow

Interfaces and menus


Menus are most commonly used to set up controls and to adjust game settings, such as graphics and sound, or to load saved game states. In any given game, it is crucial that the accompanying menu does not hinder access to the game or any of its settings. When we think of a great game, we always remember it for the actual game itself, rather than the menus — unless they were especially entertaining, or especially badly designed.

Many games seek to tie the menu of their game with the game's design or themes. For example, in 2D Boy's excellent World Of Goo, the cursor is changed to the form of a goo ball with a trail that follows it in the menus and game, tying the game's visual concept with the game interface. This is a good example, as the game itself is already giving the player something to toy with as they navigate through the opening menu.

In Media Molecule's LittleBigPlanet, this concept is taken to another level by giving the player a menu that requires them to learn...

Creating the menu — approach 1


In this first approach, we'll create a menu that uses a transparent background texture as a GUI Texture, in the same way as we have just done with our main title logo.

We will then need to write a script in order to make the texture receive mouse events for mouse enter, mouse exit, and mouse down/up.

Adding the play button

In the Menu folder in the Project panel, select the texture called PlayBtn. Go to GameObject | Create Other | GUI Texture. Select the PlayBtn object in the Hierarchy that you have just made. In the Inspector, set its Transform position to (0.5, 0.6, 0.

GUI Texture button script

This is the first of our three buttons, and because they all have common functions, we will now write a script that can be used on all of the three buttons, using public member variables to adjust settings. For example, each button will:

  • Play a sound when clicked on

  • Load a different level (or Scene in Unity terms) when clicked on

  • Swap texture when the mouse is over them...

Creating the menu — approach 2


As we already have a working menu, rather than removing it from our scene, we will temporarily disable the objects that make it up. By doing this, you can choose which menu you prefer later and reinstate as necessary.

Disabling Game Objects

One at a time, select the PlayBtn, the InstructionsBtn, and the QuitBtn in the Hierarchy, and deactivate them by doing the following:

  • In the Inspector, deselect the checkbox to the left of the name of the object

  • Ensure that this has turned the text of the object to light gray in the Hierarchy and that the element itself has disappeared from the Game view preview

Writing an OnGUI() script for a simple menu

Now, create a new empty object by going to GameObject | Create Empty. This makes a new object in the Hierarchy called GameObject with only a Transform component attached. This will be the holder object for our GUI 2.0 menu. This is because the script we are going to write will need to be attached as a component in...

Summary


In this chapter, we have looked at the two core ways of creating interface elements in Unity — GUI scripting and GUI Textures. By now you should have a good understanding of how to implement either approach to build interfaces. While there are many more things you can do with GUI scripting, the techniques for establishing elements that we have covered here are the essentials you'll need each time you write a GUI script.

We are still to produce the Instructions scene containing information for the player, but don't worry, we'll be creating this as we look at some new techniques for animation in the next chapter, among other finishing touches for the game itself.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Game Development Essentials
Published in: Oct 2009Publisher: PacktISBN-13: 9781847198181
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Will Goldstone

Will Goldstone is a longstanding member of the Unity community and works for Unity Technologies as a Technical Support Associate, handling educational content and developer support. With an MA in Creative Education, and many years experience as a lecturer in higher education, Will wrote the first ever Unity book, the original Unity Game Development Essentials, and also created the first ever video tutorials for the package. Through his sites http://www.unity3dstudent.com and http://learnunity3d.com Will helps to introduce new users to the growing community of developers discovering Unity every day.
Read more about Will Goldstone