Keyboard Focus in Unity 4.6 Pause Menu

Started by
-1 comments, last by _eternal 9 years, 3 months ago
In my pause menu, I'm trying to give keyboard focus to the first button of each submenu when the submenu is opened. If I don't do this, the player can't navigate the menu without the mouse. Here's my current pause code:

    public class Pause : MonoBehaviour
    {
     // Rewired
     private Player player;
     private CharacterController cc;
     // Rewired
    
     public GameObject pausePanel;
     public GameObject items;
        public GameObject chat;
        public GameObject returnButton;
        public GameObject itemTest;
    
     bool homeScreen;
     bool paused = false;
     bool isFocused = false;
    
    
    
     void Start() {
     homeScreen = true;
     }
    
     void Awake() {
     // Get the Rewired Player object for this player and keep it for the duration of the character's lifetime
     player = ReInput.players.GetPlayer(0);
     
     // Get the character controller
     cc = GetComponent<CharacterController>();
     }
     
     void Update()
     {
     if(player.GetButtonDown ("Pause") && homeScreen) {
     paused = togglePause();
     }
     }
     
     void OnGUI()
     {
     if (paused) {
       pausePanel.SetActive(true);
                Debug.Log("pause");
    
                //Focus the first item of the main menu
                if (isFocused == false && homeScreen)
                {
                    focusOn(items);
     }
     }
    
     if (paused == false) {
                pausePanel.SetActive(false);
                isFocused = false;
     }
            
     }
     
     bool togglePause()
     {
    
            //stops game from being paused
     if(Time.timeScale == 0f)
     {
     Time.timeScale = 1f;
     return(false);
     }
    
            //pauses game
     else
     {
     Time.timeScale = 0f;
     return(true);  
     }
     }
    
     public void focusOn(GameObject button) 
        {
            Debug.Log(isFocused);
            if (isFocused == false) //was the menu just loaded?
            {
     EventSystem.current.SetSelectedGameObject(button, null);
                isFocused = true; //menu is loaded and player can now navigate without game resetting focus to button
                Debug.Log("focus on");
     }
     }
    
        public void focusOff()
        {
            isFocused = false;
        }
    
     // Disables Back from exiting pause menu if not on initial pause screen.
     public void isNotHomeScreen () {
     homeScreen = false;
     }
    
     //Allows Back to exit pause menu when on initial pause screen.
     public void isHomeScreen () {
     homeScreen = true;
     }
    
    }
And here's a screencap of chat's button component: http://i.imgur.com/3csu2b9.png
As you can see, I'm trying to call focusOn to give focus to the Return button, which is the only button within chat right now. However, the submenu opens without focus being assigned, and I don't know why.

This topic is closed to new replies.

Advertisement