Advertisement
mvaganov

CursorLock.cs

Feb 26th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // author: mvaganov@hotmail.com
  6. // license: Copyfree, public domain. This is free code! Great artists, steal this code!
  7. // latest version at: https://pastebin.com/raw/zqd0yK40
  8. namespace NS {
  9.     public class CursorLock : MonoBehaviour {
  10.         void FixedUpdate() {
  11.             LockUpdate();
  12.         }
  13.         // whether cursor is visible or not
  14.         private static bool m_cursorIsLocked = false;
  15.         public static void LockUpdate() {
  16.             if(Input.GetKeyUp(KeyCode.Escape)) {
  17.                 m_cursorIsLocked = false;
  18.             } else if(Input.GetMouseButtonUp(0)) {
  19.                 m_cursorIsLocked = true;
  20.             }
  21.  
  22.             if (m_cursorIsLocked) {
  23.                 Cursor.lockState = CursorLockMode.Locked;
  24.                 Cursor.visible = false;
  25.             } else if (!m_cursorIsLocked) {
  26.                 Cursor.lockState = CursorLockMode.None;
  27.                 Cursor.visible = true;
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement