Author Topic: UIButton color change not triggered on Android  (Read 3161 times)

Brendtron5000

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
UIButton color change not triggered on Android
« on: June 17, 2014, 02:10:13 AM »
Hi,

I'm using NGUI on OUYA, an Android device. OUYA's latest SDK has a new input method that doesn't use Unity's input mapper. In order to get input to NGUI I wrote a mapper that sends key strokes to via UICamera.notify. If the user pushes down on the left stick, for example, I'd do this:

  1. GameObject selectedObject = UICamera.selectedObject;
  2. UICamera.Notify(selectedObject, "OnKey", KeyCode.DownArrow);
  3.  

This selects the next button in the list according to UIKey Navigation. It doesn't fire the color change, however, so users won't be able to see which button is selected. I know it's moving down the list of button though, as I can count my down movements and the button action works as expected.

It's worth noting that the item with "Starts selected" checked shows up as the appropriate color and then is unhighlighted as I move away.

I'm on Unity 4.5.0 with the latest NGUI as of tonight. Just can't figure this out.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton color change not triggered on Android
« Reply #1 on: June 17, 2014, 01:35:57 PM »
I would advise you to add a custom input function (UICamera.onCustomInput). Look at what happens in existing input functinos such as UICamera.ProcessFakeTouches().

UIButton reacts to OnPress, not OnKey. You should be calling UICamera's ProcessTouch() function in your onCustomInput callback, passing the pressed and unpressed state. NGUI will do the rest.

Brendtron5000

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UIButton color change not triggered on Android
« Reply #2 on: June 17, 2014, 03:55:21 PM »
Okay, I'll move the code and use a custom input function, which is a cleaner way to do it. I'm not having issues with the pressing of the button though, just the display of it after the selection is changed. It doesn't change color even though the state has changed, which I can verify with debug output added to NGUI code.

The code I'm using to cycle through buttons is actually really similar to what you have in ProcessOthers():

  1.                 if (useController)
  2.                 {
  3.                         if (!string.IsNullOrEmpty(verticalAxisName)) vertical += GetDirection(verticalAxisName);
  4.                         if (!string.IsNullOrEmpty(horizontalAxisName)) horizontal += GetDirection(horizontalAxisName);
  5.                 }
  6.  
  7.                 // Send out key notifications
  8.                 if (vertical != 0)
  9.                 {
  10.                         currentScheme = ControlScheme.Controller;
  11.                         Notify(mCurrentSelection, "OnKey", vertical > 0 ? KeyCode.UpArrow : KeyCode.DownArrow);
  12.                 }
  13.  

If I'm understanding this correctly it's taking controller input and sending the appropriate key to the selected object with OnKey (in my case a button.) The selection does change, but the display of the button doesn't.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton color change not triggered on Android
« Reply #3 on: June 18, 2014, 03:37:27 PM »
As I said... OnKey event is not what the button listens to. Visual pressing of the button is in response to OnPress.

OnKey is used only for controller-based navigation.

Brendtron5000

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UIButton color change not triggered on Android
« Reply #4 on: June 18, 2014, 10:38:43 PM »
Yes, I see that it's OnPress if I want the pressed button state, but it's the hover that I'm having a problem with. I send the down key to the currently selected button to get it to move to the next button, correct? That's what I see in your code. So I send the down arrow to my button, and it selects the next one, but the hover state isn't triggered, so the user has no idea that it's selected.

Edit: I have a solution. Also I think I didn't clarify what exactly I'm working with. OUYA is an Android device but it has no touch input, only a controller that does not send input through Unity's input manager. I'm using UIKey Navigation on my buttons, which works brilliantly with controllers in the editor but fails on the device.

The problem is that the input scheme was never changed to Controller, so the if statement that triggers OnHover falls through:

  1. if (isEnabled && (!isSelected || UICamera.currentScheme == UICamera.ControlScheme.Controller) && tweenTarget != null)
  2.                         OnHover(isSelected);

I needed to add a line to set the input to Controller before sending the key codes:

  1. UICamera.currentScheme = UICamera.ControlScheme.Controller;
  2.                         GameObject selectedObject = UICamera.selectedObject;
  3.                         UICamera.Notify(selectedObject, "OnKey", KeyCode.UpArrow);

Took a few debug statement to figure this all out. I imagine this isn't a common situation, but hopefully it will help somebody else in the future.
« Last Edit: June 19, 2014, 10:37:48 AM by Brendtron5000 »