21
2/2/2015 Menus loading and restarting the game — Pixelnest Studio http://pixelnest.io/tutorials/2dgameunity/menus/ 1/21 Our game — Steredenn — is on Steam Greenlight. Help us with your vote! Back to Pixelnest Tutorial: Creating a 2D game with Unity follow us on twitter Prev. Summary Next Download Graphics, Stock Photos, Fonts, Themes & more on Creative Market. Powered by Fusion Menus loading and restarting the game Pixelnest 18 nov. 2013 We have finished our first level with a basic gameplay, some sounds, graphics and particles. However, when the player dies, the game continues to run and it's impossible to start again. Moreover, when you launch

Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 1/21

Our game — Steredenn — is on Steam Greenlight. Help us with your vote!

Back to Pixelnest Tutorial: Creating a 2D game withUnity

follow us on twitter

← Prev. Summary Next →

Download Graphics, Stock Photos, Fonts, Themes & more on Creative Market.Powered by Fusion

Menus ­ loading and restarting the game

Pixelnest

18 nov. 2013

We have finished our first level with a basic gameplay, somesounds, graphics and particles.

However, when the player dies, the game continues to runand it's impossible to start again. Moreover, when you launch

Page 2: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 2/21

it, you start directly. We are clearly in a situation where weneed some menus to control the game.

Damien : Let's say it immediately: menus and GUI are no fun in a game. Youusually have to use a very basic GUI framework (or no framework at all). It takes a lot of time and the result is just... menus that players will skip asfast as possible.

Matthieu : Unlike many game developers, I disagree with the previousstatement. Creating a good game GUI is not an easy task, but it can berewarding and interesting. However, creating menus requires good tools andsome sensible design decisions (like for an app interface).

But yeah; a good menu must be invisible and the players should not evennotice it exists.

Unfortunately, Unity is not really well­equipped to createfancy menus without investing a lot of time or using a third­party library.

We have no ambition to create a complex GUI for thistutorial. The built­in tools will be enough in our case, but youwill likely find that they are too... limited.

Let's start with the basics.

Assets

Background

Page 3: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 3/21

(Right click to save the image)

Logo

Page 4: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 4/21

(Right click to save the image)

Import those assets in the project. You can put them in a"Menu" subfolder of "Textures". Otherwise "background" willerase the previous game file.

Page 5: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 5/21

For the buttons, we will use the (ugly) Unity standard ones.

Title screen

Nearly all games have a title screen. It's where the playerlands when starting the game.

Damien : Some are just awesomes and memorables: Megaman, Metal Slug...(I'm a big fan of title screens).

What we are going to create will not be that awesome, but...simple!

Scene

First, create a new scene:

1. "File" ­> "New scene".2. Save it in the "Scenes" folder as "Menu".

Tip : You could also press the cmd+N (OS X) or ctrl+N (Windows) shortcuts.

Our title screen will be made of:

A background.A logo.A script that will display the buttons.

Page 6: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 6/21

For the background:

1. Create a new Sprite2. Position it at (0, 0, 1)3. Size (2, 2, 1)

For the logo:

1. Create a new Sprite2. Position it at (0, 2, 0)3. Size (0.75, 0.75, 1)

You should have:

Page 7: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 7/21

Of course, you can add your name, instructions, jokes andanimations. Menus are a land of freedom. Just keep in mindthat a gamer wants to play as quickly as possible.

Loading script

Now we will add a button to start the game, via a script.

Create a new "MenuScript" in the "Scripts" folder, and attachit to a new empty game object (called... "Scripts"? Justsaying.):

using UnityEngine;

/// <summary>/// Title screen script/// </summary>public class MenuScript : MonoBehaviour void OnGUI() const int buttonWidth = 84; const int buttonHeight = 60;

// Determine the button's place on screen // Center in X, 2/3 of the height in Y Rect buttonRect = new Rect( Screen.width / 2 ‐ (buttonWidth / 2), (2 * Screen.height / 3) ‐ (buttonHeight / 2), buttonWidth, buttonHeight );

Page 8: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 8/21

// Draw a button to start the game if(GUI.Button(buttonRect,"Start!")) // On Click, load the first level. // "Stage1" is the name of the first scene we created. Application.LoadLevel("Stage1");

About the syntax : Yes, the syntax is a bit weird.

We are just drawing a button which will load the scene"Stage1" when the player clicks on it.

Note : The OnGUI method is called every frame and should embed all the codethat display a GUI element: lifebar, menus, interface, etc. The GUI object allows you to quickly create GUI components from the code,like a button with the GUI.Button method.

Then, launch the game and watch our wonderful menu:

Page 9: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 9/21

Click and... Crash!

Level 'Stage1' (‐1) couldn't be loaded because it has not been added to the build settings. To add a level to the build settings use the menu File‐>Build Settings...

What we need to do is clearly written in the error message.

Adding scenes to the build

Go to "File" ­> "Build Settings":

Page 10: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 10/21

Now, drag all the scenes you want to package within yourgame. Here it's simple: it's "Menu" and "Stage1".

Page 11: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 11/21

Back to the menu. Click and... Play!

Page 12: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 12/21

Tip : The Application.LoadLevel() method job is to clear the current scene and toinstantiate all the game objects of the new one. Sometimes, you want to keepa game object of a first scene into a second (e.g., to have a continuous musicbetween two menus).

Unity provides a DontDestroyOnLoad(aGameObject) method for these cases. Just callit on a game object and it won't be cleared when a new scene is loaded. Infact, it won't be cleared at all. So if you want to remove it in a further scene,you have to manually destroy it.

Deaths and restarts

Finally, we will allow the player to restart the game once hedied. And as you may have seen, it happens a lot (we will"simplify" the game in an upcoming next chapter).

Our actual game flow is:

1. The player gets hit by a bullet.2. HealthScript.OnCollisionEnter is called.3. The player looses 1 health point.4. "HealthScript" destroys the player since he has less than 1

health point.

We will add two new steps:

1. PlayerScript.OnDestroy is called.2. A "GameOverScript" is created and added to the scene.

Page 13: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 13/21

Create a new "GameOverScript" in the "Scripts" folder.

It's a little piece of code that will display a "Restart" and a"Back to Menu" buttons:

using UnityEngine;

/// <summary>/// Start or quit the game/// </summary>public class GameOverScript : MonoBehaviour void OnGUI() const int buttonWidth = 120; const int buttonHeight = 60;

if ( GUI.Button( // Center in X, 1/3 of the height in Y new Rect( Screen.width / 2 ‐ (buttonWidth / 2), (1 * Screen.height / 3) ‐ (buttonHeight / 2), buttonWidth, buttonHeight ), "Retry!" ) ) // Reload the level Application.LoadLevel("Stage1");

if ( GUI.Button( // Center in X, 2/3 of the height in Y

Page 14: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 14/21

new Rect( Screen.width / 2 ‐ (buttonWidth / 2), (2 * Screen.height / 3) ‐ (buttonHeight / 2), buttonWidth, buttonHeight ), "Back to menu" ) ) // Reload the level Application.LoadLevel("Menu");

It's exactly identical to the first script we wrote, with twobuttons.

Now, in the "PlayerScript", we must instantiate this newscript on death:

void OnDestroy() // Game Over. // Add the script to the parent because the current game // object is likely going to be destroyed immediately. transform.parent.gameObject.AddComponent<GameOverScript>();

Launch the game and try to die (it shouldn't take long):

Page 15: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 15/21

You can find the script somewhere in the scene:

Page 16: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 16/21

Of course, this can be improved with scores and animations,for example.

But it works! :)

"It's so ugly my eyes bleed"

Damn!

If you want to do something about it, you can create a "GUISkin".

"Assets" ­> "Create" ­> "Gui Skin":

Page 17: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 17/21

Page 18: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 18/21

Inside the "Inspector", you can tweak the UI controls to getsomething more fancy. Make sure to put this skin inside the"Resources" folder.

Note : The "Resources" folder is a special one in Unity. Everything insidethis folder will be packed with the game and can be loaded using theResources.Load() method. This permits you to load objects at runtime, and those objects may comefrom your users (mods anyone?).

However, the skin is not applied until you set it in yourscripts.

In all our previous GUI scripts, we have to load (only once,not for each frame) the skin using GUI.skin =Resources.Load("GUISkin"); .

Here is an example inside the "MenuScript" (Observe theStart() method):

using UnityEngine;

/// <summary>/// Title screen script/// </summary>public class MenuScript : MonoBehaviour private GUISkin skin;

void Start() // Load a skin for the buttons

Page 19: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 19/21

skin = Resources.Load("GUISkin") as GUISkin;

void OnGUI() const int buttonWidth = 128; const int buttonHeight = 60;

// Set the skin to use GUI.skin = skin;

// Draw a button to start the game if (GUI.Button( // Center in X, 2/3 of the height in Y new Rect(Screen.width / 2 ‐ (buttonWidth / 2), (2 * Screen.height / 3) ‐ (buttonHeight / 2), buttonWidth, buttonHeight), "START" )) // On Click, load the first level. Application.LoadLevel("Stage1"); // "Stage1" is the scene name

As you can see, this is a lot of boring work just for a dead­simple menu.

Note : If you have some money and you need a lot of menus and texts inyour game, consider buying the NGUI plugin. It worths it. Really.

Next Step

Page 20: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 20/21

click to get more information about our upcoming game!

We just learned how to make the inevitable menus for ourgame.

Look at what you have done until now :

A parallax scrolling with 3 background layers.Lot of particles!A title screen.Graphics and sounds.A shmup gameplay with one player and multiplesenemies.

Congratulations! But, unfortunately, it's only on yourcomputer. To sell this new award­gaining game, we need todistribute it.

That's what we will talk about in the last chapter:deployment.

← Prev. Summary Next →

Page 21: Menus loading and restarting the gamementorshipacademy.org/ourpages/auto/2014/8/24/59022702/Menus … · 24/08/2014  · 2/2/2015 Menus loading and restarting the game — Pixelnest

2/2/2015 Menus ­ loading and restarting the game — Pixelnest Studio

http://pixelnest.io/tutorials/2d­game­unity/menus/ 21/21

© 2013 pixelnest.io ­ we craft games and apps