Skip to content

Instantly share code, notes, and snippets.

@mirague
Last active October 28, 2016 11:07
Show Gist options
  • Save mirague/65c7494ecbacf11ea4910100d446f54e to your computer and use it in GitHub Desktop.
Save mirague/65c7494ecbacf11ea4910100d446f54e to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
public class Main : Button {
void OnGUI() {
// Q: Enter PointerLock state
if (Input.GetKeyDown(KeyCode.Q)) {
EnterPointerLock();
}
// O: Exit PointerLock state
// When entered with CLICK: Releases Pointer Lock but does not Reveal Cursor.
// When entered with Q: Works as intended. Released Pointer Lock and Reveals the Cursor.
if (Input.GetKeyDown(KeyCode.O)) {
ExitPointerLock();
}
// ESC: Exit PointerLock state
// When entered with CLICK: Releases Pointer Lock but does not Reveal Cursor.
// When entered with Q: On FIRST press it Releases Pointer Lock (cursor still hidden), on SECOND press it Reveals the Cursor!
if (Input.GetKeyDown(KeyCode.Escape)) {
ExitPointerLock();
}
}
void EnterPointerLock() {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void ExitPointerLock() {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
public override void OnPointerDown(UnityEngine.EventSystems.PointerEventData eventData) {
EnterPointerLock();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment