MonoBehaviour.OnApplicationFocus

function OnApplicationFocus (focus : boolean) : void

Description

Sent to all game objects when the player gets or loses focus.

OnApplicationFocus can be a co-routine, simply use the yield statement in the function.

JavaScript
    var paused: boolean;
function OnGUI() {
// Show a message if the game is paused.
if (paused) {
GUI.Label(new Rect(100, 100, 50, 30), "Game paused");
}
}
function OnApplicationFocus(focusStatus: boolean) {
paused = focusStatus;
}