Touch Input
This article will provide you with sample interaction providers which demonstrate how to feed touch input events into Nova using both Unity's legacy Input APIs as well as Unity's new Input System Package.
See the Mobile Settings Menu Sample for an example of touch input in action.
See Input Overview for an overview of Nova's input system.
Legacy Input APIs
using Nova;
using UnityEngine;
public class LegacyTouchInputSample : MonoBehaviour
{
private void Update()
{
for (int i = 0; i < Input.touchCount; i++)
{
// Using Unity's legacy input system, get the first touch point.
Touch touch = Input.GetTouch(i);
// Convert the touch point to a world-space ray.
Ray ray = Camera.main.ScreenPointToRay(touch.position);
// Create a new Interaction from the ray and the finger's ID
Interaction.Update interaction = new Interaction.Update(ray, (uint)touch.fingerId);
// Get the current touch phase
TouchPhase touchPhase = touch.phase;
// If the touch phase hasn't ended and hasn't been canceled, then pointerDown == true.
bool pointerDown = touchPhase != TouchPhase.Canceled && touchPhase != TouchPhase.Ended;
// Feed the update and pressed state to Nova's Interaction APIs
Interaction.Point(interaction, pointerDown);
}
}
}
Input System Package
using Nova;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch;
using UnityEngine.InputSystem.Utilities;
public class TouchInputSample : UnityEngine.MonoBehaviour
{
private void OnEnable()
{
// Since we use the polling-based EnhancedTouchSupport APIs, we need
// to explicitly enable them
EnhancedTouchSupport.Enable();
}
private void Update()
{
ReadOnlyArray<Touch> touches = Touch.activeTouches;
for (int i = 0; i < touches.Count; i++)
{
Touch touch = touches[i];
// Convert the touch point to a world-space ray.
UnityEngine.Ray ray = UnityEngine.Camera.current.ScreenPointToRay(touch.screenPosition);
// Create a new Interaction from the ray, and give it a source ID.
Interaction.Update interaction = new Interaction.Update(ray, (uint)touch.finger.index);
// If the touch phase is valid, hasn't ended, and hasn't been canceled, then pointerDown == true.
bool pointerDown = touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled;
// Feed the update and pressed state to Nova's Interaction APIs
Interaction.Point(interaction, pointerDown);
}
}
}