Author Topic: Selecting a button via code?  (Read 4411 times)

Nerosam

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Selecting a button via code?
« on: April 14, 2013, 01:14:47 PM »
Hi folks.

A quick thanks for this awesome unity asset. Works very well.

I have a question. I've spent some hours  searching Google and NGUI forums for the answer but cant seem to find it. I have a panel with buttons on it and I want to ad any time be able to call a function which selects a button. For example:-

buttonObject.Select();

I know its possible to have buttons selected by hovering over it but I am trying to implement control pad and/or keyboard support for navigating through the UI.


Any help would be much appreciated.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Selecting a button via code?
« Reply #1 on: April 14, 2013, 01:36:50 PM »
NGUI comes with an example for controller / keyboard input. A main menu driven by controller/keyboard to be precise.

Nerosam

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Selecting a button via code?
« Reply #2 on: April 14, 2013, 04:22:33 PM »
NGUI comes with an example for controller / keyboard input. A main menu driven by controller/keyboard to be precise.

Wow....how could I have forgotten about that...thanks for the reminder. Il check it out.

p3v1

  • Guest
Re: Selecting a button via code?
« Reply #3 on: June 16, 2013, 12:58:13 AM »
I can't get the key functionality to work even if I do it exactly like you have in that controller example (except that I don't use any animations but just change menu states with SetActiveChildren in code). I have main menu and game over menu. For the first time after starting the game key controls works just fine but once I come back to main menu from game over menu, keys just stop working. I change menu statesin code like this:

  1. NGUITools.SetActiveChildren(playUI,false);
  2.                 NGUITools.SetActiveChildren(gameOverUI,true);

and I have set my Play and Back to Menu buttons StartsSelected to true. I have also tried UICamera.selectedObject = startButton; (Play button) and if I Debug.log that, it is selected but the button doesn't really get selected in the game... What am I doing wrong here?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Selecting a button via code?
« Reply #4 on: June 16, 2013, 09:49:50 PM »
Activate first, select after.