Should the cursor be locked?
// Called when the cursor is actually being locked function DidLockCursor () {
Debug.Log("Locking cursor"); // Disable the button
guiTexture.enabled = false;
} // Called when the cursor is being unlocked
// or by a script calling Screen.lockCursor = false;
function DidUnlockCursor () {
Debug.Log("Unlocking cursor"); // Show the button again
guiTexture.enabled = true;
} function OnMouseDown () {
// Lock the cursor
Screen.lockCursor = true;
} private var wasLocked = false; function Update () {
// In standalone player we have to provide our own key
// input for unlocking the cursor
if (Input.GetKeyDown ("escape"))
Screen.lockCursor = false; // Did we lose cursor locking?
// eg. because the user pressed escape
// or because he switched to another application
// or because some script set Screen.lockCursor = false;
if (!Screen.lockCursor && wasLocked) {
wasLocked = false;
DidUnlockCursor();
}
// Did we gain cursor locking?
else if (Screen.lockCursor && !wasLocked) {
wasLocked = true;
DidLockCursor ();
}
}
using UnityEngine; using System.Collections; public class Example : MonoBehaviour { void DidLockCursor() { Debug.Log("Locking cursor"); guiTexture.enabled = false; } void DidUnlockCursor() { Debug.Log("Unlocking cursor"); guiTexture.enabled = true; } void OnMouseDown() { Screen.lockCursor = true; } private bool wasLocked = false; void Update() { if (Input.GetKeyDown("escape")) Screen.lockCursor = false; if (!Screen.lockCursor && wasLocked) { wasLocked = false; DidUnlockCursor(); } else if (Screen.lockCursor && !wasLocked) { wasLocked = true; DidLockCursor(); } } }
import UnityEngine import System.Collections public class Example(MonoBehaviour): def DidLockCursor() as void: Debug.Log('Locking cursor') guiTexture.enabled = false def DidUnlockCursor() as void: Debug.Log('Unlocking cursor') guiTexture.enabled = true def OnMouseDown() as void: Screen.lockCursor = true private wasLocked as bool = false def Update() as void: if Input.GetKeyDown('escape'): Screen.lockCursor = false if (not Screen.lockCursor) and wasLocked: wasLocked = false DidUnlockCursor() elif Screen.lockCursor and (not wasLocked): wasLocked = true DidLockCursor()