Introduction
Since it took me a day to figure it out and several hours of reading comments and suggestions which didn't quite work, let me help you by saving you time.
Background
I was looking for a way to control Pause event from "Game" window.
To be more specific, I wanted to catch the following event:
I read so many answers such as:
void OnApplicationPause( bool pauseStatus )
{
isPaused = pauseStatus;
}
or:
void OnGUI( )
{
if( isPaused )
}
which was not what I was looking for.
I continued searching and trying more things like: OnDisable
, OnApplicationPause
, OnApplicationFocus
, OnUpdate
, OnLateUpdate
, OnFixedUpdate
....
I've seen many things like this:
void Update ()
{
if(Input.GetKeyDown(KeyCode.P))
{
if(Time.timeScale == 1 && playerController.alive == true)
{
Time.timeScale = 0;
showPaused();
} else if (Time.timeScale == 0 && playerController.alive == true){
Time.timeScale = 1;
hidePaused();
}
}
At a certain point, I even thought about ways to detect my mouse position and set "hit" event when pressing my mouse, then trying to guess whether I hit the pause button.
Luckily for me, I'm too lazy to do that, so I continued reading and found these two links:
Success!
Using the Code
A breakthrough!
I continued reading more about the play mode and understood that I needed to catch those events.
https://docs.unity3d.com/Manual/GameView.html
using UnityEditor;
void OnEnable()
{
#if UNITY_EDITOR
EditorApplication.playmodeStateChanged += StateChange;
#endif
}
#if UNITY_EDITOR
void StateChange()
{
if (EditorApplication.isPlayingOrWillChangePlaymode && EditorApplication.isPlaying)
{
Debug.Log("GOT IT");
}
}
#endif
and this is the right event for those issues.
Good luck!