Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by KyleStank, Jan 13, 2015.

  1. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    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!
     
  2. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    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.
     
  3. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    283
  4. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    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.
     
  5. NirielNabokov

    NirielNabokov

    Joined:
    Dec 22, 2014
    Posts:
    9
    The EventSystem class may offer the functionality that you need.
    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.
     
  6. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    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?
     
  7. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    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)
     
  8. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    283
    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.
     
  9. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    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.