A group dedicated to indie and standalone game development.

Report article RSS Feed 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 feillyne on Oct 24th, 2010
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:

javascript code:
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:

javascript code:
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:

javascript 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

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

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

javascript code:
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 Aug 3 2011, 2:41am says:

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

+2 votes     reply to comment
KyleHatch
KyleHatch Nov 24 2011, 6:12am replied:

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

+2 votes     reply to comment
KyleHatch
KyleHatch Nov 24 2011, 6:26am replied:

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.

+2 votes     reply to comment
Shockwave317
Shockwave317 Aug 4 2012, 8:04am says:

It brings up a error

+1 vote     reply to comment
SalRad
SalRad Sep 8 2012, 6:19pm says:

many many thanks :)

+1 vote     reply to comment
Guest
Guest Nov 16 2012, 5:29am says:

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

Guest
Guest Nov 16 2012, 5:58am says:

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

Guest
Guest Mar 26 2013, 2:35am says:

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

Guest
Guest Mar 27 2013, 12:13pm says:

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

Guest
Guest May 18 2013, 9:12am says:

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

Guest
Guest Jun 6 2013, 6:01am says:

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

Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Established
Jan 21, 2009
Privacy
Public
Subscription
Open to all members
Contact
Send Message
Email
Members Only
Membership
Join this group
Group Watch
Track this group
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Games
CarEdu
CarEdu Indie Single & Multiplayer Racing
Related Engines
Unity
Unity Commercial Released May 30, 2005
Related Groups
Indie Devs
Indie Devs Hobbies & Interests group with 1,047 members
Unity devs
Unity devs Hobbies & Interests group with 1,069 members