Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Need help with PAUSE MENU w/ KEYBOARD

Discussion in 'Unity UI & TextMesh Pro' started by symorian, Jun 16, 2017.

  1. symorian

    symorian

    Joined:
    Jun 7, 2017
    Posts:
    45
    Hi,

    I'm trying to create a pause menu in which the buttons can be controlled via KEYBOARD.

    The problem is I either cannot make the buttons to be highlighted as default when the pause screen opens yet they get highlighted after the first key press OR I can highlight the first button as default BUT this time the system insists on selecting that only 1 button and cannot navigate through other button objects. If I press the down arrow I can see it quickly flashes as if it tries to change the selection to the next button but fails.

    This is probably because that the SetSelectedGameObject is called continuously in Update and it is strictly assigned to the button "selectedBut". That's why it insists on highlighting that button. But if I don't do that, this time no button gets highlighted as default when pause screen is opened until I press an arrow key.

    I think I have to add some kind of a "listener" in order to change the currently highlighted button but couldn't figure how or from where I should do that.

    Here is my code...

    I've also tried playing around with "es.currentSelectedGameObject" with no luck.

    Code (CSharp):
    1.  
    2.  
    3. void Update () {
    4.  
    5.         if (isPaused)
    6.         {
    7.             pauseMenuCanvas.SetActive(true);
    8.             Time.timeScale = 0f;
    9.  
    10.             es.SetSelectedGameObject(selectedBut);
    11.  
    12.             player.Instance.enabled = false;
    13.            
    14.         }
    15.         else
    16.         {
    17.             pauseMenuCanvas.SetActive(false);
    18.             Time.timeScale = 1f;
    19.             player.Instance.enabled = true;
    20.             es.SetSelectedGameObject(null);
    21.         }
    22.  
    23.         if (Input.GetKeyDown(KeyCode.Escape))
    24.         {
    25.             isPaused = !isPaused;
    26.         }
    27.  
    28.     }
    29.  
    Thank you all!
     
  2. symorian

    symorian

    Joined:
    Jun 7, 2017
    Posts:
    45
    OK,

    Here is an update.

    After I played around with different scenes, I have seen that there is no problem with the main Title screen.
    First button is highlighted as default and everything works well with navigating keyboard.

    The problem occurs in the "in game/pause" menu. Since there are also player controls available and probably whole EventSystem is focused on that one, when I jump into PAUSE menu, the first item isn't highlighted by default as opposed to main title menu and I need to press down arrow and navigate to the second item in order to let the "highlighting" start working and ACTUALLY activate the focus on pause menu.

    In the main scene where the title menu is, there is ONLY ONE CANVAS.

    However, in the scene where pause menu is present, there is another canvas, making that level actually has two canvases. One for pause menu itself and one for the scene.

    Probably there becomes a FOCUS conflict either between the ingame controls and the pause menu UI OR there is a conflict between the two canvases on the second scene.

    Here is what it looks like,

    WORKS PROPERLY. WHEN STARTED, 1st ITEM in MENU IS HIGHLIGHTED
    IntroScene
    |
    |--->Canvas
    --------|
    --------|--->mainTitleMenu button 01 (Selected, Highlighted & rest is diminished)


    |------------------------------------------------------------|

    DOES NOT WORK AS EXPECTED. FIRST ITEM ISN't HIGHLIGHTED DESPITE THAT I CAN SEE IT IS SELECTED IN DEBUG.
    Level1Scene
    |
    |--->Canvas
    |
    |--->PauseMenu
    --------|
    --------|--->Canvas
    -------------------|
    -------------------|--->PauseMenuButton01 (Selected, NOT Highlighted, & same with the rest, UNTIL Down arrow is pressed.)


    How can I correct this misbehaviour? BTW, I'm testing and making this for keyboard nav. Not for the mouse.

    Thank you all!
     
  3. symorian

    symorian

    Joined:
    Jun 7, 2017
    Posts:
    45
    BUMP! Anyone to help or give some suggestion?

    I've also tried disabling the other canvas and the problem still persists.
    So, it is not about two canvases fighting over focus.

    It's about the input controls not being transferred from character control to the pause menu. I have to push a button for the menu to get the necessary focus.


    Need help... Have tried many things without luck. Don't know if this is kind of a bug since most people design their games for mouse and control pad interfaces nowadays and this can be something skipped about complete keyboard control.

    I'm pretty sure it would have a solution regarding the event listeners but I have no idea how to solve this.

    Thanks a lot!
     
  4. Duke64

    Duke64

    Joined:
    Dec 28, 2017
    Posts:
    2
    I'm stuck with the same problem. Do you found out what the problem is?
     
  5. BaronL

    BaronL

    Joined:
    Sep 29, 2017
    Posts:
    1
    Same thing here.
    I've tried: UnityEngine.EventSystems.EventSystem.current.firstSelectedGameObject = reference.GetComponent<UnityEngine.UI.Button>().gameObject;
    But no luck.

    Thing is, I remember in another game I made previously, the event system automatically focused on activated elements by default, as with the main menu. Also, sometimes it does not focus at all, so even worse than having to press the keys first.

    I just had a thought, maybe if you keep menus in different canvases and activate each canvas, and not the menus Inside the canvas?
     
    symorian likes this.