- Home /
Touchscreen tap not updating on custom device
I am integrating a remote touchscreen device into the new Input System by calling QueueStateEvent(_touchscreen, touchState)
with the latest state I have received from a touchscreen, see example snippet below.
When looking at the InputDebug, I can see that the primaryTouch.position
values and primaryTouch.phase
are being correctly updated however when tapping, primaryTouch.tap
and primaryTouch.tapcount
are not being updated. I have also setup an InputActions on an object to react to a touchscreen tap but the function OnTap()
within a script attached to said object is never being called.
Is this the correct method for updating a custom Touchscreen device? What is the procedure for setting a tap and notifying the InputActions?
The below example has been adapted from the instructions here on how to update a custom device.
class TouchscreenManager
{
Touchscreen _touchscreen;
public TouchscreenManager()
{
NativeApi.RegisterTouchscreenStateCallback(TouchstateStateReceived);
_touchscreen = InputSystem.AddDevice<Touchscreen>("Example Touchscreen");
}
private void TouchstateStateReceived(TouchState touchState)
{
// touchState includes a consistent touchid of 1 and updated phase and position
InputSystem.QueueStateEvent(_touchscreen, touchState);
}
}
Looking deeper into the issue, it seems that Touchscreen.OnNextUpdate()
is resetting primaryTouch.tapcount
each time a tap occurs, InputState.Change(primaryTouch.tapCount, (byte)0);
Answer by holo-aidan · Sep 01, 2021 at 09:03 AM
InputSystem.QueueStateEvent
takes 3 arguments, Input Device device
, TState state
and double time = -1
, see here. If you use the default time value, the InputSystem will use InputRuntime.s_Instance.currentTime
.
When queueing state events for Touchscreen the touch start time is recorded with the time which was passed in from QueueStateEvent
. Within Touchscreen.OnNextUpdate
a check is done to confirm whether the tap count is more than 0 and the current time is greater than the tap start time + the time for a tap + the tap delay: if (touchStatePtr->tapCount > 0 && InputState.currentTime >= touchStatePtr->startTime + s_TapTime + s_TapDelayTime)
. For reasons unknown to me, this check is done with InputState.currentTime
instead of InputRuntime.s_Instance.currentTime
. When debugging it was seen that these values are in fact different, therefore the tapcount was always being reset.
The solution was to pass in InputState.currentTime
to QueueStateEvent
which synchronised the check for if taps are within the allowed timeframe. See updated example below.
class TouchscreenManager
{
Touchscreen _touchscreen;
public TouchscreenManager()
{
NativeApi.RegisterTouchscreenStateCallback(TouchstateStateReceived);
_touchscreen = InputSystem.AddDevice<Touchscreen>("Example Touchscreen");
}
private void TouchstateStateReceived(TouchState touchState)
{
// touchState includes a consistent touchid of 1 and updated phase and position
InputSystem.QueueStateEvent(_touchscreen, touchState, InputState.currentTime);
}
}
Your answer
Follow this Question
Related Questions
Tap/Touch to Start 0 Answers
How to show and hide an image by swiping 0 Answers
Problem on new InputSystem touch(UI) 1 Answer
How to determine a tap velocity or a tap strength on iOS and Android with Unity? 2 Answers
Menu on Android Application 1 Answer