iOS Player Input

Simulate Player Input With Mouse

If you don’t have access to a phone you can simulate a touch screen inside the Unity Editor with the mouse by going to Window > Analysis > Input Debugger. This will bring up an Input Debug window. Inside window click the Options dropdown and select Simulate Touch Input from Mouse or Pen. Now clicking the mouse in play mode will simulate touching the screen. Unfortunately you cannot simulate multi touch using the mouse so to do that you must use Unity Remote or build the project to your phone (Unity iOS Build).

Touch Utilities

The script below provided the following functionalities:

  • Project a ray from the camera through the touch point and gets the location of where the ray intersects a plane specified by the user.

Get Touch Data Using Input Action Assets

Using Unity’s Input System (See: Using the Unity Input System Package), we can listen into touch events that allow us to detect when a touch begins and ends while also reading data from the input system to determine more information about the touche’s starting point, ending point, delta, etc.

Key Takeaways:

  • We create an Action Map containing Touch Actions.

  • We connect Touch actions to Input Events (See Scriptable Object Input Events V2) that trigger when touches begin and end (when the finger is placed on the screen, and released from the screen)

  • TouchVisualizer.cs connects activates / deactivates the touch visualization assets when detecting the touches happening. Additionally it reads in the TouchState property (touchAction.ReadValue<TouchState>()) from a Touch Action to get current and start position data.

Example

Important

This example requires the following packages to be installed:

  • TextMeshPro Essentials

  • Input System

Warning

This example also includes the following packages:

Note

This example also includes utilities for processing touch inputs as seen in Touch Utilities

Click to download InputActionTouchDataExample.unitypackage.

Get Touch Data Using EnhancedTouch

Unity’s Input System (See: Using the Unity Input System Package) comes with support for a class called EnhancedTouch.Touch. With this class we can detect how many fingers are touching the screen as well as the touch information associated with each finger.

Key Takeaways:

  • We enable Enhanced Touch features by calling EnhancedTouchSupport.Enable();

  • We connect Touch events to functions when enabling the TouchVisualizer (Touch.onFingerDown += OnFingerDown;).

  • We store a reference to a target “Finger” and then extract touch information from the finger (Touch touch = currentFinger.currentTouch;) to get current and start position data.

Example

Important

This example requires the following packages to be installed:

  • TextMeshPro Essentials

  • Input System

Note

This example also includes utilities for processing touch inputs as seen in Touch Utilities

Click to download EnhancedTouchDataExample.unitypackage.

Processing Touch Data Example

In the following code we are able to do more with processing touch input from the EnhancedTouch.Touch class. We have different behaviors for handling a single vs multiple touches.

Key Takeaways:

  • We enable Enhanced Touch features by calling EnhancedTouchSupport.Enable();

  • We connect Touch events to functions when enabling the TouchVisualizer (Touch.onFingerDown += OnFingerDown;).

  • We detect how many touches are occuring using Touch.activeTouches.Count and execute different behaviors depending on the amount of touches detected.

  • We extract touch data from individual touches using Touch.activeTouches[n].

Note

In this example we gather touch data from Touch.activeTouches instead of Touch.activeFingers since, at the time, touch data from fingers behaved in an odd way. For example, when the user’s finger stopped moving, the TouchPhase property of the touch data from Touch.activeFingers still registered as being Moved instead of stationary. Additionally the delta property would never reach 0. This was in contrast to touch data gathered from Touch.activeTouches which had phases and deltas that behaved as expected when a user’s finger stopped moving.

Important

This example requires the following packages to be installed:

  • Input System

Note

This example also includes utilities for processing touch inputs as seen in Touch Utilities

Click to download EnhancedMultiTouchExample.unitypackage.