Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created October 10, 2021 15:58
Show Gist options
  • Save kurtdekker/ca0423af018105697b5ba0764e84c08d to your computer and use it in GitHub Desktop.
Save kurtdekker/ca0423af018105697b5ba0764e84c08d to your computer and use it in GitHub Desktop.
Simple stateless in-game pause mechanism
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