Unity 4.6 - How to make main menu navigation with buttons only?

Hello,

I am trying to make a main menu where you can use only the arrow keys to navigate. The player can use the mouse or arrow keys, but for testing purposes, I have disabled the mouse. When the game first starts, everything is okay and when I use the arrow keys, everything gets selected fine. But when I press enter, then a menu pops up, but no button is selected. I think this may be because the menu was disabled and then it was re-enabled so the Unity system isn’t detecting that. But I am just SO lost on how to do any sort of navigation with buttons. Someone PLEASE help me. One more question I have, how would I make a button the “selected”/“focused” button when the mouse hovers over it? And how would I make buttons that were disabled, the “selected”/“focused” button when they are enabled. Please, please help! There is no help anywhere else for this EXACT problem!

I would use something like a var selection : int; and then depending on which key you press it changes the number, then calls a function for a Switch statement that activates which menu that number it’s assigned too after pressing Enter.

Are you using the 4.6 GUI??
If so this “question” I had may be of use.

You add your menu panel and buttons to a canvas and then use the button navigation options in the inspector. The event system built in standalone component will automate much of this for you and you can choose defaults there.

Thank you. This helped a lot. I got it to work your way, and I got it to work by using just a few lines of “{Button Variable}.Select()”, so thank you. This helped. I also needed Explicit navigation for my case.

The EventSystem class may offer the functionality that you need.
http://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html
In particular, the currentSelectedGameObject property may be useful.

Problem 1: nothing is selected when you open a new menu.

You may want to add a script to your panel, with a “public Selectable toSelectOnOpen;” an OnEnable method that does “EventSystem.currentSelectedGameObject = this.toSelectOnOpen;”. For example.

Problem 2: get the keyboard focus to follow the mouse focus.

Create a Monobehavior script that implements the “IPointerEnterHandler” interface.

Warning: I have no access to unity right now so the following code may have typos and errors, but I hope it gives you an idea. Also, I do not know yet how to format code in this forum :/.

using UnityEngine;
using UnityEngine.EventSystems;

public class SelectOnHover : Monobehaviour, IPointerEnterHandler {
void OnPointerEnter(PointerEventData eventData) {
EventSystem.currentSelectedGameObject = this.gameObject;
}
}

You would have to add this script to every UI element that can be selected. An other possibility would be to implement a custom StandaloneInputModule.

I get a static member reference from that script:

Assets/Scripts/UI Scripts/SelectOnHover.cs(8,21): error CS0120: An object reference is required to access non-static member `UnityEngine.EventSystems.EventSystem.currentSelectedGameObject’

I don’t know what to fix here. Are there any special components I am supposed to have on the buttons or anything?

On the button component look into the “navigation” property, I personally just use automatic as it works pretty well.

furthermore the navigation is setup with the horizontal and vertical axis (if I recall correct)

Without looking at my code I am pretty sure that you need to add Eventsystem as a member of the script. Eventsystem eventsystem. Then drag the Eventsystem object from the scene to the script comonent??

Automatic doesn`t play nice when adding more than one menu in the same canvas where you onky want the active menu to be navigable.

I use explicit. Automatic does lot work with my main menu. I have aw u that works with the keyboard keys and a controller now.

I am not at Unity as well now, but I will try this later. I do wan to point out that I can use the menu with keys and a game pad now, just when the mouse hovers over buttons, it does not select that button.