My way of handling the pause state

I’m sure there are a million ways of handling the pause state in lecture 278, but here’s mine.

public class GameManager : MonoBehaviour {

public bool recording = true;

private bool isPaused = false;
private bool pauseState = false;
private float initialFixedDeltaTime;

void Start ()
{
    initialFixedDeltaTime = Time.fixedDeltaTime;
}

void Update ()
{
    recording = CrossPlatformInputManager.GetButton("Fire1") ? false : true;

    if (Input.GetKeyDown(KeyCode.P)) { pauseState = !pauseState; }

    if (isPaused != pauseState) { TogglePause(); }
}

void TogglePause()
{
    if (isPaused)
    {
        Time.timeScale = 1;
        Time.fixedDeltaTime = initialFixedDeltaTime;
        isPaused = false;
    }
    else
    {
        Time.timeScale = 0;
        Time.fixedDeltaTime = 0;
        isPaused = true;
    }
}

private void OnApplicationFocus(bool focus)
{
    pauseState = !focus;
}
}

My humble attempt. I like to keep the pause stuff with the pause property so that any changes can be done in one place in the code.

public class GameManager : MonoBehaviour {

	public bool recording = true;

    private bool _isPaused = false;
    public bool IsPaused{
        get { return _isPaused; }
        set{
            if (value == _isPaused) return;
            if (value){// pause it
              Time.timeScale = 0;
              Time.fixedDeltaTime = 0;
            } else{ //resume it
                Time.timeScale = 1f;
                Time.fixedDeltaTime = _initialFixedDelta;
            }
            _isPaused = value;
         }
    }

    private float _initialFixedDelta;

	void Start () {
		PlayerPrefsManager.UnlockLevel (2);
		print (PlayerPrefsManager.IsLevelUnlocked (1));
		print (PlayerPrefsManager.IsLevelUnlocked (2));
		_initialFixedDelta = Time.fixedDeltaTime;
	}

	// Update is called once per frame
	void Update (){
	    recording = !CrossPlatformInputManager.GetButton ("Fire1");

	    if (Input.GetKeyDown (KeyCode.P)){
		    IsPaused = !IsPaused;
		}
	}

	void OnApplicationPause (bool pauseStatus) {
		IsPaused = pauseStatus;
		// TODO this will need completing to actually trigger a pause, reader exercise.
	}
}

Privacy & Terms