I'm using mouse lock as part of my first person camera rotation in a webGL game.
I'd like to ensure that I have focus on the game, when focus is lost / escape key is pressed I want to display a pause screen with the text 'click here to continue'.
Here is my Pause class
using UnityEngine;
using System.Collections;
public class Pause : MonoBehaviour {
public RotateCamera rotateCamera;
public GameObject pauseMenu;
public bool paused;
public void Start()
{
PauseGame();
}
void OnApplicationFocus(bool hasFocus)
{
if (hasFocus == true)
{
UnPause();
}
if (hasFocus == false)
{
PauseGame();
}
}
void Update () {
if (Input.GetKeyDown(KeyCode.Escape))
{
PauseGame();
}
if (paused)
{
if (Input.GetMouseButtonDown(0))
{
UnPause();
}
}
}
public void PauseGame()
{
rotateCamera.LockCursor(false);
paused = true;
pauseMenu.SetActive(true);
}
public void UnPause()
{
rotateCamera.LockCursor(true);
paused = false;
pauseMenu.SetActive(false);
}
}
The problems are
When the user switches focus e.g tabs to something else the game doesn't pause
When the user presses escape the game doesn't display the pause menu. Its as though the escape key press isn't registered. They are able to see their cursor again in the browser (which makes first person control confusing).
And if this helps any further, this is how I am handling mouse lock in in my rotateCamera script.
public void LockCursor(bool _lock)
{
lockCursor = _lock;
Screen.lockCursor = lockCursor;
}