Is there a way to programmatically pause the game (triggering all MonoBehaviour.OnApplicationPause() callbacks) ?

Hi,

Looking for the best way to pause the game.

Intuitively, I expected Application class to provide Pause() / Resume() methods to have MonoBehaviour-s paused / resumed (just like when hitting Editor’s pause button).

OnApplicationPause is a message that a MonoBehaviour can use to handle when the application loses focus or is put in background.

Usually, to pause the game, you want to use a GameState enum, with a static property along with an event so that any component can subscribe to it, like :

	public enum STATE
	{
		Running,
		Paused,
		Over
	}

	public delegate void StateChange(STATE state) ;

	static public event StateChange StateChanged;
	static private STATE _state;
	static public STATE State
	{
		get { return _state; }
		set
		{
			if (value != _state)
			{
				_state = value;

				switch (_state)
				{
					case STATE.Running:
						Time.timeScale = 1;
						break;
					case STATE.Paused:
						Time.timeScale = 0;
						break;
					case STATE.Over:
						Time.timeScale = 0;
						break;
					default:
						break;
				}

				if (StateChanged != null)
					StateChanged(_state);
			}
		}
	}