Last active
October 28, 2016 11:07
-
-
Save mirague/65c7494ecbacf11ea4910100d446f54e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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