• Register

A group dedicated to indie and standalone game development.

Post tutorial Report RSS How to create an in-game pause menu?

How to create a pause menu, showing the cursor (that was hidden), and pause/unpause the game itself.

Posted by on - Intermediate UI/HUD

1. Create HideCursorScript.js script (Assets -> Create -> JavaScript) and cursor hiding mini system following this tutorial here (only HideCursorScript.js):
Indiedb.com

This script hides the cursor at the beginning of the level. So it's rather clear it can't be used in MainMenuScene / your own main menu scene, because you need to see the cursor to click appropriate buttons, and instead, it is used in CompleteScene scene / or your own main game level.

2. Create UpdateScript.js script (Assets -> Create -> JavaScript), edit and paste the following code in:

function Update () {

if(Input.GetKey("escape")) {
		//pause the game
		Time.timeScale = 0;
		//show the pause menu
		var script3 = GetComponent("PauseMenuScript"); 
		script3.enabled = true;
		//disable the cursor hiding script
		var script4 = GetComponent("HideCursorScript"); 
		script4.enabled = false; 
		}
}

What does this script?
if player presses ESCAPE button, the script
1) pauses the game by setting timeScale application property to 0
2) shows the pause menu script (PauseMenuScript.js) that is disabled by default
3) hides the cursor hiding script (HideCursorScript.js) that is enabled by default

3. Now you need to create PauseMenuScript.js (Assets -> Create -> JavaScript) containing the code below:

var newSkin : GUISkin;
var logoTexture : Texture2D;

function thePauseMenu() {
		//layout start
		GUI.BeginGroup(Rect(Screen.width / 2 - 150, 50, 300, 250));
		
		//the menu background box
		GUI.Box(Rect(0, 0, 300, 250), "");
		
		//logo picture
		GUI.Label(Rect(15, 10, 300, 68), logoTexture);
		
		///////pause menu buttons
		//game resume button
		if(GUI.Button(Rect(55, 100, 180, 40), "Resume")) {
		//resume the game
		Time.timeScale = 1.0;
		//disable pause menu
		var script3 = GetComponent("PauseMenuScript"); 
		script3.enabled = false;
		//enable cursor hiding script
		var script4 = GetComponent("HideCursorScript"); 
		script4.enabled = true; 
		}
		
		//main menu return button (level 0)
		if(GUI.Button(Rect(55, 150, 180, 40), "Main Menu")) {
		Application.LoadLevel(0);
		}
		
		//quit button
		if(GUI.Button(Rect(55, 200, 180, 40), "Quit")) {
		Application.Quit();
		}
		
		//layout end
		GUI.EndGroup(); 
}

function OnGUI () {
		//load GUI skin
		GUI.skin = newSkin;
		
		//show the mouse cursor
		Screen.showCursor = true;
		
		//run the pause menu script
		thePauseMenu();
}

What does this script?
1) Makes space for GUI skin and a logo texture (lines: var newSkin : GUISkin; var logoTexture : Texture2D; )
2) Creates GUI layout, then a GUI box, then a logo at the top, and then respective buttons.
3) Shows the currently hidden cursor, so the player can click buttons without any unpredicted problems (line: Screen.showCursor = true;)

Let's take a closer look at the resume button, here's the code:

//game resume button
		if(GUI.Button(Rect(55, 100, 180, 40), "Resume")) {
		//resume the game
		Time.timeScale = 1.0;
		//disable pause menu
		var script3 = GetComponent("PauseMenuScript"); 
		script3.enabled = false;
		//enable cursor hiding script
		var script4 = GetComponent("HideCursorScript"); 
		script4.enabled = true; 
		}

Line:
if(GUI.Button(Rect(55, 100, 180, 40), "Resume")) {
means:
If (GUI button defined here) will be clicked, then

Time.timeScale = 1.0;
make the game run by setting timeScale to 1.0 from 0

var script3 = GetComponent("PauseMenuScript"); 
script3.enabled = false;

disable the pause menu script currently running (PauseMenuScript.js)

var script4 = GetComponent("HideCursorScript"); <br />script4.enabled = true;

and finally enable the cursor hiding script (HideCursorScript.js).

4. Now, just as in Unity: How to hide the cursor tutorial, drag these three scripts onto the desired game object. You can create a special camera for it, by selecting GameObject -> Create Other -> Camera.
Switch off all standard components (Camera, Flare Layer, GUILayer, Audio Listener), especially if you use it for the splitscreen multiplayer mode (to have GUI displayed in the middle instead of one of the halves).

5. Finally, set PauseMenuScript properties (New Skin and Logo Texture).

If all is done correctly, you should have such a camera:

Pause Menu Camera

To see how it looks in-game in CarEdu, you can visit the gallery, and this screenshot in particular:
Indiedb.com

Post comment Comments
SimonDenton
SimonDenton

Great tutorial but is it possible to get C# code assistance?

Reply Good karma Bad karma+2 votes
KyleHatch
KyleHatch

using UnityEngine;
using System.Collections;

public class PauseMenu : MonoBehaviour
{
GUISkin newSkin;
Texture2D logoTexture;

public void thePauseMenu()
{
//layout start
GUI.BeginGroup(new Rect(Screen.width / 2 - 150, 50, 300, 250));


//the menu background box

GUI.Box(new Rect(0, 0, 300, 250), "");

//logo picture

GUI.Label(new Rect(15, 10, 300, 68), logoTexture);


///////pause menu buttons

//game resume button

if(GUI.Button(new Rect(55, 100, 180, 40), "Resume"))

{

//resume the game

Time.timeScale = 1.0f;

Destroy(this);

}


//main menu return button (level 0)

if(GUI.Button(new Rect(55, 150, 180, 40), "Main Menu"))

{

Application.LoadLevel(0);

}


//quit button

if(GUI.Button(new Rect(55, 200, 180, 40), "Quit"))

{

Application.Quit();

}


//layout end

GUI.EndGroup();


}


void OnGUI ()

{

//load GUI skin

GUI.skin = newSkin;


//show the mouse cursor

Screen.showCursor = true;


//run the pause menu script

thePauseMenu();

}

}

----

sorry about the spacing, but theres a general c# class called PauseMenu().

I don't attach it to a script but call it from another, but it should do the same job. It can be edited to do as needed!

called in attached script via;

Time.timeScale = 0.0f;
gameObject.AddComponent("PauseMenu");

---

enjoy

Reply Good karma Bad karma+2 votes
KyleHatch
KyleHatch

you'll need to add the line

logoTexture = (Texture2D) Resources.Load("Textures/Menu/paused");

at the start of thePauseMenu() for a texture to show up, change the path to whatever your texture is called and where it is. Don't need the extension for the file.

If you edit the class so that the script is attached to an object, then just make the GUISKin and Texture2D public to edit in the inspector.

Reply Good karma Bad karma+2 votes
ShockingCactus
ShockingCactus

It brings up a error

Reply Good karma Bad karma+1 vote
SalRad
SalRad

many many thanks :)

Reply Good karma Bad karma+1 vote
Guest
Guest

This comment is currently awaiting admin approval, join now to view.

Guest
Guest

This comment is currently awaiting admin approval, join now to view.

Guest
Guest

This comment is currently awaiting admin approval, join now to view.

Guest
Guest

This comment is currently awaiting admin approval, join now to view.

Guest
Guest

This comment is currently awaiting admin approval, join now to view.

Guest
Guest

umm.. the pause menu always comes up when I start the game.. is that supposed to happen?

Reply Good karma Bad karma+1 vote
Guest
Guest

This comment is currently awaiting admin approval, join now to view.

Guest
Guest

This comment is currently awaiting admin approval, join now to view.

Guest
Guest

This comment is currently awaiting admin approval, join now to view.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: