UI Buttons need two clicks after OnScreenControl were disabled

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:

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.

I posted about something similar and gave a workaround that might work for you while we’re waiting for the fix:
Bug - Disabling OnScreenControl in Update breaks first click of UI Button - Unity Forum

1 Like

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
protected override void OnDisable()
{
   base.OnDisable();

#if UNITY_EDITOR || UNITY_WEBGL || UNITY_STANDALONE
   EmulateMouseClick();
#endif
}

private void EmulateMouseClick() {
   Mouse mouse = InputSystem.GetDevice<Mouse>();

   if (mouse != null)
   {
      StateEvent.From(mouse, out InputEventPtr eventPtr);
   
      InputControl position = mouse["position"];
      if (position != null)
      {
         position.WriteValueIntoEvent(new Vector2(1000000, 1000000), eventPtr);

         InputControl leftBtn = mouse["leftButton"];
         if (leftBtn != null)
         {
            leftBtn.WriteValueIntoEvent<float>(1, eventPtr);
            InputSystem.QueueEvent(eventPtr);

            leftBtn.WriteValueIntoEvent<float>(0, eventPtr);
            InputSystem.QueueEvent(eventPtr);
         }
      }
   }
}

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.