Author Topic: UIKeyNavigation  (Read 16583 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
UIKeyNavigation
« on: March 22, 2014, 08:46:10 PM »
Overview

UI Key Navigation component makes it possible to add keyboard and controller-based navigation to your UIs.



By default it's not possible to navigate the UI using keyboard, joystick or a 360 controller because without a touch or mouse input it's not immediately clear what should be receiving the events. To remedy this situation, NGUI treats the currently selected object as the object that will receive the events, and the Key Navigation component makes it possible to change the selection based on user input -- such as pressing arrow keys on the keyboard or using a thumb stick on the 360 controller.

To begin, simply attach the UIKeyNavigation component to the widgets that should respond to controller events. This generally means all your buttons, sliders, toggles, popup lists, etc. If your game is not meant to be controlled by mouse or touch, it's also a good idea to ensure that one of the navigation scripts has an active Starts Selected flag so that it becomes selected (and thus -- controllable) from the very beginning.

The script will automatically determine what should get selected when the arrow keys get pressed, selecting the next active object that has UIKeyNavigation attached in the direction of movement. If you want to restrict movement, use the Constraint field. "None" means no restriction on movement. "Vertical" and "Horizontal" limit the movement only to the axis of your choice. Setting the Constraint to "Explicit" will disable the automatic  detection altogether, and only the explicitly specified values under the Override section will be used.

Speaking of that, Override is what ultimately "forces" the value to be what you chose it. If an override value is set, that value will always be used regardless of the Constraint setting. So if you wanted to fine-tune the navigation on specific buttons to select something that may not be obvious to the system, you would do it by setting the appropriate override fields.

Last but not least, if you have a button that opens a new window on click, you may want to set the Select on Click field to a control inside that window so that the selection is moved accordingly. In the Controller Input example scene the "Options" button shows the Options menu and also gives focus to a button inside of it.

Pro-Tip

You don't need to set the Override fields unless pressing the key does not result in the correct widget being selected.

Class Documentation

http://tasharen.com/ngui/docs/class_u_i_key_navigation.html

If you have a question regarding this component or would like me to clarify something, just post a reply here.

deram_scholzara

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UIKeyNavigation
« Reply #1 on: April 09, 2014, 11:09:36 PM »
The Tab and Shift-Tab functionality should be made clear here.

The following are the fall-through charts for Tab & Shift-Tab presses.  If the first item in the list is not assigned, it goes to the next item.

Tab:
  • Right
  • Down
  • Up
  • Left
Shift-Tab:
  • Left
  • Up
  • Down
  • Right

Alendro305

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: UIKeyNavigation
« Reply #2 on: May 22, 2014, 02:11:30 AM »
I am having an issue with the key navigation, I am trying to get a controller working with the menu I have created but every time I go to hit Up on my controller DPad, it shifts to the button that is labeled down.

For example: There are 3 buttons, while on focus on Button_01, hitting down would put focus on Button_03 rather than Button_02. Left and Right work perfectly fine but Up and Down are reversed for some odd reason.

I am not sure if it is because of the asset I am using for my Gamepad support, It is called GamePad Input on the asset store.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation
« Reply #3 on: May 22, 2014, 04:28:53 PM »
I suggest checking to see what kind of info you get from the input device. If you really are getting up for down and vice versa. Remember, you can always set the navigation fields explicitly to force it to select a specific object.

Nige777

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UIKeyNavigation
« Reply #4 on: June 04, 2014, 06:11:55 AM »
Hi All
Is there any way of binding the navigation to the D-Pad rather than the left thumbstick on the Xbox controller (on OSX using the tattiebogle drivers)? This is what it has defaulted to and I'm using that for moving my character around - can't sen to find a way to do this ...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation
« Reply #5 on: June 05, 2014, 01:48:08 AM »
D-Pad are keys are also axes: http://wiki.unity3d.com/images/a/a7/X360Controller2.png

You will need to define these axes in the Input Manager and choose them on the UICamera.

stilghar

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: UIKeyNavigation
« Reply #6 on: June 10, 2014, 02:08:30 AM »
I'm having a weird issue here. I have 4 buttons, the menu works fine with mouse. Now that I added UIKeyNavigation to them I can select them also with keyboard. All fine until here. But the input seems to be too sensitive as I get erratic behaviour. Only if I press (and depress) the keys really, really fast I get the correct behaviour. Otherwise the focus changes to a random button (sometimes even the same one that was already on focus). It looks as if registering many key presses when only doing it once.

Any ideas?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation
« Reply #7 on: June 10, 2014, 03:07:40 AM »
Let's stick to one thread, please. http://www.tasharen.com/forum/index.php?topic=9862

Fractalbase

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: UIKeyNavigation
« Reply #8 on: June 27, 2014, 04:44:02 PM »
Ok.  I have a Panel and Grid in my scene.  Using C#, I dynamically add a number of the same prefab to the Grid.  The prefab is a Sprite with a child Label.  The sprite in the prefab has a box collider, button, and key navigation.  When I instantiate the prefabs into the grid, I mark the first instantiated prefab's key navigation to have the "Start Selected" parameter set to true.

The issue is that none of the prefabs are selected by default.  up and down do nothing.  Once I click on the sprite/button with a mouse, then the up and down keys navigate through my buttons.

Am I missing something?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation
« Reply #9 on: June 28, 2014, 05:25:03 PM »
'startsSelected' option is checked in OnEnable, which is fired right after Awake and before you change its value.

Do your own selection. Keep "starts selected" off, and just give selection to the item of your choice via UICamera.selectedObject = yourGameObject;

Morgoth

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UIKeyNavigation
« Reply #10 on: September 17, 2014, 09:36:37 AM »
When cancel is bound in UIcamera and pressed while key navigation is going on, the selection is lost and key naviagation no longer works.
How would i go about regaining a UI element selection when a navigation key is pressed?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation
« Reply #11 on: September 17, 2014, 01:27:05 PM »
This is only like that if you have mouse or touch navigation enabled. If you have only controller and joystick navigation, it's not possible to lose selection by pressing the cancel key.