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. Dismiss Notice

Bug UI Buttons need two clicks after OnScreenControl were disabled

Discussion in 'Input System' started by vizgl, Sep 22, 2021.

  1. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    61
    I made custom mobile onscreen Joystick. I use OnScreenControl component to emulate native screen touch when virtual stick(ui Image) dragged. All work perfectly except this:
    - when I disable Joystick and enable some ui buttons
    - buttons stop react to first click, only on second one

    After investigate I found strange behaviour in the deep of OnScreenControl.OnDisable method:
    upload_2021-9-23_1-4-33.png

    So, when OnScreenControl will disable, next click on ui component will be always ignored.

    P.S.
    Reproduced: Editor, Windows build.
    Not tested: WebGL, iOS.
    Not reproduced: Android.
     
    Last edited: Sep 22, 2021
  2. angusbjones

    angusbjones

    Joined:
    Nov 24, 2012
    Posts:
    8
    vizgl likes this.
  3. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    61
    Thanks for such interesting workaround. Did you reproduce this bug on mobile without fix?

    I go another way to fix that. Emulate mouse click far outside of game view:
    - made class CustomOnScreenButton : OnScreenControl
    - override OnDisable method

    Code (CSharp):
    1. protected override void OnDisable()
    2. {
    3.    base.OnDisable();
    4.  
    5. #if UNITY_EDITOR || UNITY_WEBGL || UNITY_STANDALONE
    6.    EmulateMouseClick();
    7. #endif
    8. }
    9.  
    10. private void EmulateMouseClick() {
    11.    Mouse mouse = InputSystem.GetDevice<Mouse>();
    12.  
    13.    if (mouse != null)
    14.    {
    15.       StateEvent.From(mouse, out InputEventPtr eventPtr);
    16.    
    17.       InputControl position = mouse["position"];
    18.       if (position != null)
    19.       {
    20.          position.WriteValueIntoEvent(new Vector2(1000000, 1000000), eventPtr);
    21.  
    22.          InputControl leftBtn = mouse["leftButton"];
    23.          if (leftBtn != null)
    24.          {
    25.             leftBtn.WriteValueIntoEvent<float>(1, eventPtr);
    26.             InputSystem.QueueEvent(eventPtr);
    27.  
    28.             leftBtn.WriteValueIntoEvent<float>(0, eventPtr);
    29.             InputSystem.QueueEvent(eventPtr);
    30.          }
    31.       }
    32.    }
    33. }
     
  4. angusbjones

    angusbjones

    Joined:
    Nov 24, 2012
    Posts:
    8
    The next sprint includes work that requires iOS builds so I'll let you know if I encounter it.
    Your workaround looks good too. If I were to use it I'd guess in my case all I'd need to include is a re-selection of the focused element as I've got "Deselect on Background" switched on and it is possible for the OnscreenControl to be disabled while an element should stay in focus.