Game Development Reference
In-Depth Information
Although you can put code for the GUI elements on multiple scripts, the best practice is generally to
keep it all in one place. This way, you can control the layering order by using the order in the script
rather than explicitly adding code to do so. Let's begin by creating a large message to let the player
know the game is over. For testing, once again, you will want to be able to end the game quickly.
You will begin with a winning game scenario.
You should already have the value 10 set to stop bunny drops, but this will be a good time to make
it even easier to reach the goal while setting up the end-game scenarios. The biggest drawback to
setting public variables is remembering that once they are exposed their values take precedence.
1.
Open the GardenLevel1 scene.
2.
Open the ScoreKeeper script, and create a new variable:
public int stopPopX = 1; // variable for stopping extra zombie bunny drops
Change the // stop the population explosion! line to
3.
if (currentBunCount <= stopPopX) { // stop the population explosion!
4.
Save the script.
5.
Select the GameManager object, and set its Scorekeeper's Stop Pop X to 20 .
The most logical place to add the Unity GUI is to the Game Manager object. To keep it easy to find,
you can create a script expressly for the in-game GUI elements.
6.
Create a new C# Script, and name it EndGameGUI .
You will create several scripts for the GUI and menus, so now is a good time to create a new folder
to keep them separate from the rest of the game scripts.
7.
Create a New Folder in the Project view, and name it Menu Scripts .
8.
Drag the new EndGameGUI script into it.
9.
Open the script, and add the following variable:
string finishedMessage= "Garden Secure"; // message for finished garden
10.
Add the following function:
void OnGUI () {
GUI.Label(new Rect(0,0,1000,100), finishedMessage);
}
The Unity GUI script is handled in an OnGUI function that is called every frame. With Unity GUI, you
will be able to see the GUI elements only during run-time and only in the Game view. The “controls,”
buttons, labels, scroll bars, etc., are defined by starting with a rect, which is a rectangular position
and size.
 
Search WWH ::




Custom Search