2.0 KiB
uid
| uid |
|---|
| input-system-polling-touch-input |
Touch polling
The EnhancedTouch.Touch class provides a polling API for touch input similar to UnityEngine.Input.touches. You can use it to query touches on a frame-by-frame basis.
Important
Don't use
Touchscreenfor polling. If you read out touch state fromTouchscreendirectly inside of theUpdateorFixedUpdatemethods, your application misses changes in touch state.
Enable enhanced touch
Because the API comes with a certain overhead due to having to record touches as they happen, you must explicitly enable it. To do this, call EnhancedTouchSupport.Enable:
using UnityEngine.InputSystem.EnhancedTouch;
// ...
// Can be called from MonoBehaviour.Awake(), for example. Also from any
// RuntimeInitializeOnLoadMethod code.
EnhancedTouchSupport.Enable();
Note
You don't need to enable
EnhancedTouchSupportif you're using theTouchscreenclass. You only need to callEnhancedTouchSupport.Enableif you want to use theEnhancedTouch.Touch] API.
Read all touches
To get all current touches from the touchscreen, use EnhancedTouch.Touch.activeTouches. You must first enable enhanced touch support.
For example:
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
public void Update()
{
foreach (var touch in Touch.activeTouches)
Debug.Log($"{touch.touchId}: {touch.screenPosition},{touch.phase}");
}
You can also use the lower-level Touchscreen.current.touches API to read all touches.