Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Mobile: Button Release not triggered

Discussion in 'Input System' started by ben4d85, Jun 11, 2022.

  1. ben4d85

    ben4d85

    Joined:
    Dec 26, 2018
    Posts:
    47
    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].

    Screenshot 2022-06-11 at 21.50.58.png

    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].

    Screenshot 2022-06-11 at 21.58.50.png

    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.

    Code (CSharp):
    1. public void OnDrive(InputAction.CallbackContext context)
    2.     {
    3.         Debug.Log(context);
    4.         driveInput = context.ReadValue<float>();
    5.     }
    Desktop log:

    When pressed:

    1.png

    When released:

    2.png

    Mobile log:

    When pressed:

    m.png

    Notice how the timestamp for the cancelled event appears to be wrong. All 3 log lines literally appear at the same time!

    When released:

    No change.

    ---

    PS: It appears that this forum member has the same problem, no solution: https://forum.unity.com/threads/new...ent-trigger-on-mobile-device-android.1025311/
     
    Last edited: Jun 11, 2022
    DrSpritz and dnnkeeper like this.
  2. Jacho_Mendt

    Jacho_Mendt

    Joined:
    Jun 20, 2015
    Posts:
    13
    Hi,

    I recently had the same issue, do you know if there's any update on this?
     
    ben4d85 likes this.
  3. DrSpritz

    DrSpritz

    Joined:
    Oct 6, 2013
    Posts:
    25
    Hi there,

    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 =(
     
  4. ben4d85

    ben4d85

    Joined:
    Dec 26, 2018
    Posts:
    47
    Please can you elaborate on this approach?
     
  5. DrSpritz

    DrSpritz

    Joined:
    Oct 6, 2013
    Posts:
    25
    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:
    Code (CSharp):
    1. public void OnPointerUp(PointerEventData eventData)
    2. {
    3.     SendValueToControl(0.0f);
    4. }
    5.  
    6. public void OnPointerDown(PointerEventData eventData)
    7. {
    8.     SendValueToControl(1.0f);
    9. }
    As you see, in OnPointerUp the script sends zero value.

    You can check the value in the InputAction.CallbackContext for Zero:
    Code (CSharp):
    1. private void OnAction(InputAction.CallbackContext context)
    2. {
    3.     // For buttons
    4.     var button = context.ReadValue<float>();
    5.     var isButtonCanceled = button == 0f;
    6.  
    7.     // Same for axis
    8.     var axis = context.ReadValue<Vector2>();
    9.     var isAxisCanceled = axis.sqrMagnitude == 0f;
    10. }
    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.
     
  6. Gustjc

    Gustjc

    Joined:
    Oct 16, 2017
    Posts:
    9
    I'm not sure is this is your problem, but here are some things to know that worked for me.
    Using input system 1.5.0

    I was doing some quick tests here so I was using the PlayerInput component with the Send Messages behavior.

    Code (CSharp):
    1.  
    2. public void OnAttack(InputValue value)
    3. {
    4.     Debug.Log(value.Get());
    5.     Debug.Log(value.GetType());
    6.     Debug.Log(value.isPressed);
    7. }
    8.  
    Under those conditions, if the inputActions ActionType is set to 'Button'
    It will only fire once, showing the log:
    1
    UnityEngine.InputSystem.InputValue
    True

    Changing the action type to Value fixed this and now its firing the pointerUp event:
    1
    UnityEngine.InputSystem.InputValue
    True
    Null
    UnityEngine.InputSystem.InputValue
    False

    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.