Pen/Tablet/Stylus Support
Pen support comprises both tablets on desktops (such as the various tablets produced by Wacom) as well as styluses support on mobile devices such as the stylus on the Samsung Note, the Apple Pencil on iOS, or the Surface Pen on the Micrsoft Surface line of notebooks.
Pens generally offer pressure-sensitivity, in-range detection (i.e. being able to control the cursor while not yet touching the tablet/screen surface), and often the ability to flip the pen for eraser-like behavior.
Pens are represented by the Pen
device layout which is implemented by the Pen
class. Pens are based on the Pointer
layout.
The last used or last added pen can be queried with Pen.current
.
NOTES:
- Pen/tablet support is currently implemented on Windows, UWP, iOS, and Android. Support on macOS is coming in Unity 2020.1.
- Some devices support tracking multiple pens independently. This is not currently supported by the input system in Unity.
Controls
Additional to the controls inherited from Pointer
, Pen devices implement the following controls:
Control | Type | Description |
---|---|---|
tip |
ButtonControl |
Whether the tip of the pen touches the surface. Same as the inherited Pointer.press . |
eraser |
ButtonControl |
Whether the eraser/back end of the pen touches the surface. |
firstBarrelButton |
ButtonControl |
Whether the first button on the barrel of the pen is pressed. |
secondBarrelButton |
ButtonControl |
Whether the second button on the barrel of the pen is pressed. |
thirdBarrelButton |
ButtonControl |
Whether the third button on the barrel of the pen is pressed. |
fourthBarrelButton |
ButtonControl |
Whether the forth button on the barrel of the pen is pressed. |
inRange |
ButtonControl |
Whether the pen is currently in detection range of the tablet. |
tilt |
Vector2Control |
Tilt of the pen relative to the surface. |
twist |
AxisControl |
Rotation of the pen around its own axis. Only supported on few devices. |
Pressure, Tilt, Twist
The current pressure is available through the Pen.pressure
control. Pen pressure is normalized (0=no pressure, 1=maximum pressure). However, note that pressure may go beyond 1 in case the system is applying a custom pressure curve where reaching a pressure value of 1 does not require pressing the pen down all the way to the maximum supported by hardware.
If a pen does not support pressure, the pressure
control will always return 1.
If supported, the Pen.tilt
control represents the tilt angle of the pen, i.e. the angle with which the pen is titled towards the tablet/screen surface. The X and Y axis correspond to respective screen axes. A value of 1 on either axis means that the pen is fully parallel to the tablet/screen surface on the respective axis whereas a value of 0 means that the pen is perpendicular to the tablet/screen surface on the respective axis. If not support, Pen.tilt
will always be (0,0)
.
A small number of pens (such as the Wacom Art Pen) also support twist, i.e. detecting the pen rotating about its own axis. If support, Pen.twist
represents the current rotation with 0 meaning the pen is facing up on Y and values close to 1 meaning the pen is fully rotated clock-wise about its own axis.
In-Range Detection
A pen may not need to touch the tablet/screen surface in order to be able to control the cursor. The inRange
control can be used to determine whether the pen is currently in detection range. If the button is on, the pen registers with the tablet/screen.
For devices that do not support this feature, inRange
is always on.
Barrel Buttons
Pen devices often have one or multiple buttons on the side of the pen. These are represented by the firstBarrelButton
, secondBarrelButton
, thirdBarrelButton
and fourthBarrelButton
where applicable.