Before we end this chapter, we need to do a last thing: add the Game Won and
Game Over screens to the prototype. At this point, our project consists of two screens: the home screen, where the game starts, and level_01
, our actual game stage. Now we are going to add two more scenes: a Game Over screen to send the application to if the player loses, and a Game Won scene to be displayed if the player completes the game goals. For each new screen, we need a new scene and a script. In each screen we add, we plan to display a message and a button to send the application back to the Home screen.
Let's get to work!
Save your project and the scenes you built so far, which should be two: the Home screen and level_01
.
game_won
.You can duplicate the 3D Text in the scene, change its text (and its font size and style too), set the position for the second line, and then make it a child of the first line, or add a script to the 3D Text game object with a line like this:
public class Texter : MonoBehaviour { private string wonText; void Start () { wonText = "Congratulations Level Complete"; GetComponent<TextMesh>().text = wonText; } }
The
operator is used to tell the parser that it must go to a new line.
OnGUI()
section of the script:void OnGUI() { if(GUI.Button(new Rect (120, 120, 150, 30), "Home")){ Application.loadedLevel("home"); } }
game_lost
, add the 3D Text to it, and drag a script onto the text. The code to be added is as follows:public class Texter : MonoBehaviour { private string lostText; void Start () { lostText = "Too bad, you run out of lives! Play again"; GetComponent<TextMesh>().text = lostText; } void OnGUI() { if(GUI.Button(new Rect (120, 120, 150, 30), "Home")){ Application.loadedLevel("home"); } } }
We keep up with the logic we implemented so far. Each game state sends the application to a different scene. Each scene handles itself autonomously and provides options to send the application to the next state depending on the player's input. Should we need to change anything from any state, our changes won't affect the other states.
The 3D Text game object is a newcomer with the latest Unity version, and it is a welcome addition for many HUD- and interface-related needs. Refer to http://docs.unity3d.com/Manual/class-TextMesh.html for a detailed description of the control parameters of TextMesh
.
3.147.58.194