- Home /
Question by
acesouthall · Feb 24, 2014 at 11:40 PM ·
guimenupause
How to pause a game with a GUI button
I know how to create a pause menu and how to access it and pause the game via pressing the escape button.
I am developing a mobile game so how would I do it with a gui button instead of a key press.
How would I do this?
Comment
Answer by tw1st3d · Feb 24, 2014 at 11:51 PM
using UnityEngine;
using System.Collections;
public class Pause : MonoBehavior
{
// Has the game been paused?
private bool isPaused;
// Has the game gone through it's pause transition?
private bool hasPaused = false;
public void Start()
{
// Default to not paused.
this.isPaused = false;
}
public void OnGUI()
{
// If you're paused
if(this.isPaused) {
// And nothing has initialized
if(!this.hasPaused) {
// Pause the game
this.pauseGame();
this.hasPaused = true;
}
// GUI changes.
} else {
if(this.hasPaused) {
this.resumeGame();
this.hasPaused = false;
}
}
// If you click the button,
if(GUI.Button(new Rect(100,100,100,100), "PAUSE")) {
// Toggle the games pause state.
this.changePauseState();
}
}
public void pauseGame()
{
// Pause the game
Time.timeScale = 0;
// Any other logic
}
public void resumeGame()
{
// Resume the game
Time.timeScale = 1;
// Any other logic
}
public void changePauseState()
{
// Alternate bool value per button click
this.isPaused = !this.isPaused;
}
}
Your answer
Follow this Question
Related Questions
changing from mouse click to keys 0 Answers
Multiplayer pause menu won't disappear 0 Answers
In-Game GUI menu. 2 Answers
Pause Problem 1 Answer