Simulate touches
You can simulate touch input from pointer devices such as mouse and pen devices in the following ways:
- Enable touch simulation in the Unity Editor.
- Add touch simulation to a GameObject.
- Enable touch simulation in startup code.
Enable touch simulation in the Unity Editor
To enable touch simulation in the Unity Editor, perform the following steps:
- Open the Input Debugger (Window > Analysis > Input Debugger)
- Select the Options dropdown, and enable Simulate Touch Input From Mouse or Pen.
Add touch simulation to a GameObject
Add the TouchSimulation MonoBehaviour to a GameObject in your scene. TouchSimulation adds a Touchscreen device and automatically mirrors input on any Pointer device to the virtual touchscreen device.
Enable touch simulation in startup code
Call TouchSimulation.Enable somewhere in your startup code:
void OnEnable()
{
TouchSimulation.Enable();
}
Troubleshoot touch simulation
If simulated touches don't appear in your app, check the following:
- If you use the Player Input component, the player's control scheme must include the touchscreen. Simulated touches come from a separate touchscreen device, so for a single player, the component usually switches to a touch control scheme automatically. If it doesn't, for example because Auto-Switch is disabled or there are several players, switch to a control scheme that includes the touchscreen yourself, such as the Touch scheme in the default project-wide actions. For example, call
playerInput.SwitchCurrentControlScheme("Touch", Touchscreen.current). - If you read touches with the enhanced touch API, such as
Touch.activeTouches, callEnhancedTouchSupport.Enablefirst. Otherwise, the API throws anInvalidOperationExceptionwith the messageEnhancedTouch API is not enabled; call EnhancedTouchSupport.Enable(). Touch simulation and enhanced touch support are separate, so you must enable both. - If the Device Simulator view is open, it suppresses events from touch simulation, and the Input Debugger's Options menu shows Simulate Touch Input From Mouse or Pen (events suppressed by Device Simulator view). The Device Simulator view simulates touches itself, so use it instead, or close it.