On mobile, releasing my on-screen button does not fire an input event. But on desktop, it does fire the event correctly. How can this be addressed, please?
Here’s my setup: As part of my UI Canvas, I have a UI Button that has Unity’s On-Screen Button component with the Control Path set to Button South [Gamepad].
In my Input Actions, I have an Action called Drive. It has the Action Type set to Button. Under Interactions, I have added Press with the Trigger Behaviour set to Press and Release. This Action has a Binding set to Button South [Gamepad].
When I test this on desktop, the input event will be fired both when the button is pressed and when it is released. It works fine both for keyboard and gamepad. But when I test it on Android, the input event will only be fired when the button is pressed - but not when it is released. This also is reflected when logging the context.
public void OnDrive(InputAction.CallbackContext context)
{
Debug.Log(context);
driveInput = context.ReadValue<float>();
}
Same issue on my Android Nexus 5. I manually check the zero values to detect whether the player cancels the input action. A horrid decision with this =(
If I understand correctly, the phases are based on input values. I mean, when the system gets from an input device Zero, or Vector2.Zero, or Vector3.Zero values, the system interprets a phase of the event as the Cancel phase.
For example, you can see the code of the OnScreenButton script, provided with the InputSystem:
public void OnPointerUp(PointerEventData eventData)
{
SendValueToControl(0.0f);
}
public void OnPointerDown(PointerEventData eventData)
{
SendValueToControl(1.0f);
}
As you see, in OnPointerUp the script sends zero value.
You can check the value in the InputAction.CallbackContext for Zero:
private void OnAction(InputAction.CallbackContext context)
{
// For buttons
var button = context.ReadValue<float>();
var isButtonCanceled = button == 0f;
// Same for axis
var axis = context.ReadValue<Vector2>();
var isAxisCanceled = axis.sqrMagnitude == 0f;
}
I can’t guarantee that everything works that way 100%, but this approach does its job in my case.
In any case, I’m really looking forward to this problem being solved and everything will work normally.
I didn’t compile a mobile version but at least in my editor with 2021.3.19f1 it was fixed.
The input system packaged has some very big changes each version, so maybe updating it
to the latest 1.5 might solve your issue.