Created
October 10, 2021 15:58
-
-
Save kurtdekker/ca0423af018105697b5ba0764e84c08d to your computer and use it in GitHub Desktop.
Simple stateless in-game pause mechanism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
// @kurtdekker | |
// Ultra-simple clean stateless in-game pause/unpause mechanism. | |
// TODO: | |
// - put one of these script instances in your running game scene | |
// - be sure to set Time.timeScale = 1 when your game starts | |
public class InGamePause : MonoBehaviour | |
{ | |
// anywhere in your game can observe this: | |
public static bool IsPaused | |
{ | |
get | |
{ | |
return Time.timeScale == 0; | |
} | |
} | |
void Update () | |
{ | |
bool pauseUnpauseCommanded = false; | |
pauseUnpauseCommanded |= Input.GetKeyDown( KeyCode.Escape); | |
//TODO : add any other "input sources" here, have them set the bool pauseUnpauseCommanded | |
if (pauseUnpauseCommanded) | |
{ | |
// flip Time.timeScale, which will be our ONLY state | |
if (Time.timeScale == 0) | |
{ | |
Time.timeScale = 1; | |
} | |
else | |
{ | |
Time.timeScale = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment