Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

CrossPlatformInputManager.GetButtonDown behaving differently in Editor vs iOS/Android

Discussion in 'Editor & General Support' started by ZzappSizzler, Apr 21, 2018.

  1. ZzappSizzler

    ZzappSizzler

    Joined:
    Jan 15, 2017
    Posts:
    33
    My game 'Bear Beware' is released on iOS/Android. I was getting reports of the controls stopping working occasionally. No matter how I tried I could not recreate this in the Editor with Unity Remote. I put debugging information in the iOS/Android build, and, after many builds trying to track down the problem, I observed a scenario where the game lift control buttons are switched off and then on again a second later. If the player jumped while on a lift I correctly switched off the lift control buttons until he landed back on the lift. But, at this point the controls ceased to work on touchscreen. Many hours later I have learned (forgive me if you already know this):

    button.setactive(false) will switch the UI button off.
    - With iOS/Android, if there is a buttondown event not yet processed by Getbutton() that state gets ‘remembered’.

    Switching back on with button.setactive(true) will give a virgin button state in the Unity editor. - Inside iOS and Android however, the previous unprocessed down state will be SET IN PLACE until the button is pressed.

    In this condition, even though the button is not pressed, GetButtonDown() will return false (Correctly), but GetButton() will ALWAYS return true until the button is actually pressed which resets it’s state.

    People don’t generally press the up button on a lift when it’s at the top floor, but that was what was being reported by the up button. The other controls (left/right etc) were disabled (by design) as a result. Hence the reports of controls not working.

    I have replaced all Button.setactive(true) with

    button.GetComponent<Image>().raycastTarget = true;
    button.GetComponent<Image>().color = new Color(1, 1, 1, myTransparency);

    and all button.setactive(false) with

    button.GetComponent<Image>().raycastTarget = false;
    button.GetComponent<Image>().color = Color.clear;

    And the controls are working properly!

    10 hours work, 4 hours sleep in the middle of trying to understand what was happening, and (hopefully) it’s fixed. I’m fairly sure this is why users in my test country (Philippines) were giving up and uninstalling the game. I do not believe this is the way GetButton() is meant to work and is therefore a bug in Unity. The Editor should respond the same way as the device..

    I hope that helps someone!
     
    Last edited: Apr 22, 2018