Advertisement
mvaganov

CursorLock.cs

Feb 26th, 2018
306
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. // license: Copyfree, public domain. This is free code! Great artists, steal this code!
  6. // latest version at: https://pastebin.com/raw/zqd0yK40
  7. namespace NS {
  8.     public class CursorLock : MonoBehaviour {
  9.         void FixedUpdate() {
  10.             LockUpdate();
  11.         }
  12.         // whether cursor is visible or not
  13.         private static bool m_cursorIsLocked = false;
  14.         public static void LockUpdate() {
  15.             if(Input.GetKeyUp(KeyCode.Escape)) {
  16.                 m_cursorIsLocked = false;
  17.             } else if(Input.GetMouseButtonUp(0)) {
  18.                 m_cursorIsLocked = true;
  19.             }
  20.  
  21.             if (m_cursorIsLocked) {
  22.                 Cursor.lockState = CursorLockMode.Locked;
  23.                 Cursor.visible = false;
  24.             } else if (!m_cursorIsLocked) {
  25.                 Cursor.lockState = CursorLockMode.None;
  26.                 Cursor.visible = true;
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement