Last active
September 3, 2020 02:27
-
-
Save daemon3000/dae423bc175684b14db6 to your computer and use it in GitHub Desktop.
A pause manager for Unity
This file contains 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; | |
using System; | |
using System.Collections.Generic; | |
public enum PauseManagerState | |
{ | |
Pausing, | |
UnPausing, | |
Idle, | |
Paused | |
} | |
public sealed class PauseManager : MonoBehaviour | |
{ | |
[SerializeField] | |
private bool m_dontDestroyOnLoad; | |
private PauseManagerState m_state; | |
private float m_lastTimeScale; | |
private bool m_hardPause; | |
private static PauseManager m_instance; | |
public static PauseManagerState State | |
{ | |
get | |
{ | |
return m_instance != null ? m_instance.m_state : PauseManagerState.Idle; | |
} | |
} | |
public static bool IsPaused | |
{ | |
get | |
{ | |
return m_instance != null ? (m_instance.m_state == PauseManagerState.Paused) : false; | |
} | |
} | |
public static bool Exists | |
{ | |
get { return m_instance != null; } | |
} | |
public static void Pause() | |
{ | |
if(m_instance != null) | |
{ | |
// The game will be paused at the start of the next update cycle. | |
if(m_instance.m_state == PauseManagerState.Idle) | |
{ | |
m_instance.m_state = PauseManagerState.Pausing; | |
} | |
} | |
} | |
public static void UnPause() | |
{ | |
if(m_instance != null) | |
{ | |
// The game will be unpaused at the start of the next update cycle. | |
if(m_instance.m_state == PauseManagerState.Paused) | |
{ | |
m_instance.m_state = PauseManagerState.UnPausing; | |
} | |
} | |
} | |
private void Awake() | |
{ | |
if(m_instance != null) | |
Debug.LogWarning("You shouldn't have more than one instance of PauseManager"); | |
m_instance = this; | |
m_hardPause = false; | |
m_lastTimeScale = Time.timeScale; | |
m_state = PauseManagerState.Idle; | |
if(m_dontDestroyOnLoad) | |
DontDestroyOnLoad(gameObject); | |
} | |
private void Update() | |
{ | |
switch(m_state) | |
{ | |
case PauseManagerState.Pausing: | |
m_lastTimeScale = Time.timeScale; | |
m_state = PauseManagerState.Paused; | |
Time.timeScale = 0.0f; | |
break; | |
case PauseManagerState.UnPausing: | |
m_state = PauseManagerState.Idle; | |
Time.timeScale = m_lastTimeScale; | |
break; | |
default: | |
break; | |
} | |
if(Input.GetButtonDown("Pause")) | |
{ | |
if(m_state == PauseManagerState.Idle) | |
{ | |
m_state = PauseManagerState.Pausing; | |
m_hardPause = true; | |
} | |
else if(m_state == PauseManagerState.Paused) | |
{ | |
if(m_hardPause) | |
{ | |
m_state = PauseManagerState.UnPausing; | |
m_hardPause = false; | |
} | |
} | |
} | |
} | |
private void OnLevelWasLoaded() | |
{ | |
if(m_state != PauseManagerState.Idle) | |
{ | |
Time.timeScale = m_lastTimeScale; | |
m_state = PauseManagerState.Idle; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment