Created
July 13, 2016 06:52
-
-
Save wcoastsands/b10b2a5658cfa0a67092211ac4d3b02d to your computer and use it in GitHub Desktop.
CursorManager for Unity Standard Assets FirstPersonController
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 FirstPersonController = UnityStandardAssets.Characters.FirstPerson.FirstPersonController; | |
public class CursorManager : MonoBehaviour | |
{ | |
public FirstPersonController fpsController; | |
public bool locked { get; private set; } | |
void OnEnable () | |
{ | |
if (!fpsController) | |
{ | |
fpsController = FindObjectOfType<FirstPersonController>(); | |
if (!fpsController) | |
{ | |
enabled = false; | |
Debug.LogWarning("FirstPersonController object not found. CursorManager will remain disabled."); | |
} | |
} | |
if (fpsController) SetCursorLockedAndHidden(true); | |
} | |
void Update () | |
{ | |
if (Input.GetKeyDown(KeyCode.Escape)) | |
{ | |
fpsController.enabled = false; | |
SetCursorLockedAndHidden(false); | |
} | |
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) | |
{ | |
fpsController.enabled = true; | |
SetCursorLockedAndHidden(true); | |
} | |
} | |
void SetCursorLockedAndHidden (bool locked) | |
{ | |
Cursor.lockState = (locked ? CursorLockMode.Locked : CursorLockMode.None); | |
Cursor.visible = !locked; | |
this.locked = locked; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment