Mouse.md 3.7 KB

Mouse support

The Input System represents mouse input with the Mouse Device layout that the Mouse class implements. Mice are based on the Pointer layout.

To query the last used or last added mouse, use Mouse.current.

    var mouse = Mouse.current;

Note: The Input System does not currently support:

  • Input from multiple mice at the platform level.
  • Identifying the current display a mouse is on.

Controls

In addition to the Controls inherited from Pointer, Mouse devices implement the following Controls:

Control Type Description
leftButton ButtonControl The left mouse button. Same as the inherited Pointer.press.
rightButton ButtonControl The right mouse button.
middleButton ButtonControl The middle mouse button.
forwardButton ButtonControl Used for other mouse buttons where applicable.
backButton ButtonControl Used for other mouse buttons where applicable.
clickCount IntegerControl A Control which lets you read the number of consecutive clicks the last mouse click belonged to, as reported by the OS. Use this to distinguish double- or multi-clicks.
scroll Vector2Control The input from the mouse scrolling control expressed as a delta in pixels since the last frame. Can come from a physical scroll wheel, or from touchpad gestures.

Cursor warping

On desktop platforms (Windows, Mac, Linux, and UWP), you can move the mouse cursor via code. Note that this moves the system's actual mouse cursor, not just Unity's internally-stored mouse position. This means that the user sees the cursor jumping to a different position, which is generally considered to be bad UX practice. It's advisable to only do this if the cursor is hidden (see the Cursor API documentation for more information).

To move the cursor to a different position, use Mouse.WarpCursorPosition. The coordinates are expressed as Unity screen coordinates, just like Mouse.position.

    Mouse.current.WarpCursorPosition(new Vector2(123, 234));

Note: If the cursor is locked, warping the mouse position is only temporary and Unity resets the cursor to the center of the window every frame.