An extended Touch input module for Unity
- Download and import the asset package located in this repository (or grab the c# files)
- Select Assets/import package/custom package…
- Select Touch.Unity
- Drag/drop TouchHandler.cs to your EventSystem
- It’s good to go!
TouchHandler component option breakdown
- Dont Destroy on Load (checkbox): Keep the event system when loading a new scene.
- Display Stats in Editor (checkbox): Show relevant data for all touches plus a relevant marker for the location of each touch.
- Tap Timeout (textbox): Amount of time to give the user between taps. If time is exceeded, tap will be registered as new touch.
- Long Press Time (slider): Amount of time user needs to press in one place to register a long press.
- Measure Units (dropdown): Centimeter is default. Automatically converts thresholds to correct unit based on pixel density of device. This is to help ensure a consistent experience on a multitude of devices.
- SwipeThreshold (textbox): Movement speed required to consider a touch (drag) event as a swipe
- DragThreshold (textbox): Distance in MeasureUnits required for the touch to move in order to be considered a drag event. Don’t set this to 0 because there is always a small amount of movement at the beginning of every touch event.
- TouchSprite (spritebox): Sprite to use for touch events when Display Stats In Editor is enabled.
- Simulate Touch with Mouse (checkbox): Use the mouse to simulate 1 finger touch events. Press Left+Ctrl while clicking to simulate a basic pinch event.
- Tap: rapidly tap screen to add to the tap count
- Long Press: Press and hold in one place to activate
- Drag: Drag finger across screen
- Pinch: (2 fingers only) Move closer and further to change pinch radius
- Swipe: Drag finger across screen rapidly
- 2-Finger Swipe: Ditto, but with 2 fingers
Accessing the data
Most of the data can be accessed through the Input.touches extension modules
- float/int/vector2/vector3.PixelsToTouchUnits(): Converts # of pixels to selected Touch Measure Units (centimeter by default)
- Input.touches.GetDoubleSwipeDirection(): Returns TouchHandler.directions, the cardinal direction of the double swipe (type: TouchHandler.directions)
- Input.touches.SetPinchDelay(float): Set the Time that both touches must exceed before pinch begins
- Input.touches.IsPinching(): Returns bool, user is pinching or not
- Input.touches.GetPinchDistance(): returns float, change in distance since start of pinch (+ pinch bigger, - pinch smaller)
- Input.touches.GetPinchDistanceDelta(): returns float, change in distance since last change
- Input.touches.GetPinchRatio(): returns float, ratio of change in distance since start ( >1.0 pinch bigger, <1.0 pinch smaller)
- Input.touches.GetPinchRatioDelta(): returns float, ratio change in distance since last change
- Input.touches.GePinchRayHit3D(out RaycastHit, layersToIgnore): returns bool, raycasts at center of pinch. Layers to Ignore is comma delemited integers (ex. Input.touches.GetPinchRayHit3d(out rHit, 8,9,10) will ignore all colliders on layers 8,9,10)
- Input.touches.GePinchRayHit2D(out RaycastHit, layersToIgnore): returns bool, raycasts at center of pinch. Layers to Ignore is comma delemited integers (ex. Input.touches.GetPinchRayHit2d(out rHit, 8,9,10) will ignore all colliders on layers 8,9,10)
- Input.touches.GetPinchPhase(): returns touchphase, (began, moved, stationary, ended)
- Input.touches.CheckRayHit3D(): returns TouchInstance, causes all current touches to raycast3D. Each touch instance will store the data in touchInstance.raycastHit.
- Input.touches.CheckRayHit2D(): returns TouchInstance, causes all current touches to raycast2D. Each touch instance will store the data in touchInstance.raycastHit2D.
- Input.touches[int].GetAction(): returns TouchHandler.actions, for single touch event
- Input.touches[int].GetTapCount(): returns int, tap count for touch event
- Input.touches[int].GetSwipeDirection(): returns TouchHandler.directions, (left, right, up, down, none)
- Input.touches[int].GetPressTime(): returns float, get current press time of touch event
- Input.touches.GetRaw(): returns TouchInstance, access all extended public variables/methods of each touch event
- Input.touches[int].GetRaw(): returns TouchInstance, access all extended public variables/methods of single touch event