A small script to lock and hide the cursor in Unity.
using UnityEngine;
public class CursorHide : MonoBehaviour
{
public bool cursorOff;
void Awake()
{
CursorOff(cursorOff);
}
void CursorOff(bool off)
{
if (off == true)
{
if (Cursor.visible == true) Cursor.visible = false;
if (Cursor.lockState != CursorLockMode.Locked) Cursor.lockState = CursorLockMode.Locked;
}
else
{
if (Cursor.visible == false) Cursor.visible = true;
if (Cursor.lockState != CursorLockMode.None) Cursor.lockState = CursorLockMode.None;
}
}
}