There are some instances when you may need to lock the mouse cursor to the screen. For example rotating the camera where you would press down on the right mouse button then drag the mouse to rotate the camera. If you wish to hide the mouse cursor and lock it to the screen while dragging you can use Screen.lockCursor.
using UnityEngine;
public class lockCursorExample : MonoBehaviour
{
void DidLockCursor()
{
Debug.Log("Locking cursor");
}
void DidUnlockCursor()
{
Debug.Log("Unlocking cursor");
}
private bool wasLocked;
void Update()
{
if (!Screen.lockCursor && this.wasLocked)
{
this.wasLocked = false;
this.DidUnlockCursor();
}
else if (Screen.lockCursor && !this.wasLocked)
{
this.wasLocked = true;
this.DidLockCursor();
}
if (Input.GetMouseButtonDown(1))
{
Screen.lockCursor = true;
}
if (Input.GetMouseButtonUp(1))
{
Screen.lockCursor = false;
}
}
}