Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Hide/Show menu on KeyDown

Discussion in 'Scripting' started by unity_4bPeHNiaedralw, Jul 24, 2019.

  1. unity_4bPeHNiaedralw

    unity_4bPeHNiaedralw

    Joined:
    Sep 18, 2018
    Posts:
    2
    I got a canvas with multiple Menus : Main Menu and HighScore. Like seen below :
    upload_2019-7-23_14-51-49.png

    My Main Menu and HighScore got the same script and Animation attached to them.
    The animation lets the menu move from outside the screen towards the point it is now on the image above.
    The script on my menus are as follow :


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Menu : MonoBehaviour
    7. {
    8.     public Animator _animator;
    9.  
    10.     private CanvasGroup _canvasGroup;
    11.     SceneLoader sceneLoader;
    12.     MenuManager menuManager;
    13.  
    14.  
    15.     private bool idMenuHighscore;
    16.     private bool idMenuMain;
    17.  
    18.  
    19.     public bool IsOpen
    20.     {
    21.         get { return _animator.GetBool("IsOpen"); }
    22.         set { _animator.SetBool("IsOpen", value); }
    23.     }
    24.     public bool MainIsOpen
    25.     {
    26.         get { return _animator.GetBool("MainIsOpen"); }
    27.         set { _animator.SetBool("MainIsOpen", value); }
    28.     }
    29.  
    30.     public bool HighscoreIsOpen
    31.     {
    32.         get { return _animator.GetBool("HighscoreIsOpen"); }
    33.         set { _animator.SetBool("HighscoreIsOpen", value); }
    34.     }
    35.  
    36.     public void Awake()
    37.     {
    38.         _animator = GetComponent<Animator>();
    39.         _canvasGroup = GetComponent<CanvasGroup>();
    40.         var rect = GetComponent<RectTransform>();
    41.         rect.offsetMax = rect.offsetMin = new Vector2 (0, 0);
    42.         sceneLoader = FindObjectOfType<SceneLoader>();
    43.     }
    44.     public void Start()
    45.     {
    46.         menuManager = FindObjectOfType<MenuManager>();
    47.  
    48.     }
    49.  
    50.     public void Update()
    51.     {
    52.         GetCurrentAnimatorState();
    53.         ShowMenuOnKeyDown();
    54.  
    55.     }
    56.  
    57.     public void GetCurrentAnimatorState()
    58.     {
    59.         if (!_animator.GetCurrentAnimatorStateInfo(0).IsName("Open"))
    60.         {
    61.             _canvasGroup.blocksRaycasts = _canvasGroup.interactable = false;
    62.         }
    63.         else
    64.         {
    65.             _canvasGroup.blocksRaycasts = _canvasGroup.interactable = true;
    66.         }
    67.  
    68.     }
    69.  
    70.     public void ShowMenuOnKeyDown()
    71.     {
    72.  
    73.         if (idMenuMain == true && _animator.GetBool("MainIsOpen") == true && Input.GetKeyDown(KeyCode.Escape))
    74.         {
    75.             Debug.Log("false");
    76.             _animator.SetBool("MainIsOpen", false);
    77.             _animator.SetBool("IsOpen", false);
    78.        
    79.  
    80.  
    81.         }
    82.         else if (idMenuMain == true && _animator.GetBool("MainIsOpen") == false && Input.GetKeyDown(KeyCode.Escape))
    83.         {
    84.             _animator.SetBool("MainIsOpen", true);
    85.             _animator.SetBool("IsOpen", true);
    86.             Debug.Log("true");
    87.         }
    88.  
    89.     }
    90.  
    91.  
    92.  
    93.     public void SetAnimatorBool(int id)
    94.     {
    95.    
    96.         if( id== 1)
    97.         {
    98.             idMenuMain = true;
    99.             idMenuHighscore = false;
    100.             MainIsOpen = true;
    101.           //  HighscoreIsOpen = false;
    102.         }
    103.         else if (id == 2)
    104.         {
    105.             idMenuMain = false;
    106.             idMenuHighscore = true;
    107.             HighscoreIsOpen = true;
    108.           //  MainIsOpen = false;
    109.         }
    110.  
    111.     }
    112. }

    And i got a MenuManager that checks which menu is active and set it active :
    MenuManager is attached to my canvas.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MenuManager : MonoBehaviour
    6. {
    7.     public Menu CurrentMenu;
    8.  
    9.     public bool mainMenuIsOpen = false;
    10.     public int menuId;
    11.     Menu menu;
    12.  
    13.     public void Start()
    14.     {
    15.         menu = FindObjectOfType<Menu>();
    16.         ShowMenu(CurrentMenu);
    17.     }
    18.     private void Awake()
    19.     {
    20.         DontDestroyOnLoad(gameObject);
    21.     }
    22.  
    23.     public void ShowMenu(Menu menu)
    24.     {
    25.         if (CurrentMenu != null)
    26.             CurrentMenu.IsOpen = false;
    27.  
    28.         CurrentMenu = menu;
    29.         CurrentMenu.IsOpen = true;
    30.         Debug.Log(CurrentMenu);
    31.  
    32.          switch (menu.name)
    33.           {
    34.               case "Main Menu":
    35.                   menuId = 1;
    36.                   SendMenuId();
    37.                   break;
    38.  
    39.               case "HighScore":
    40.                   menuId = 2;
    41.                   SendMenuId();
    42.                   break;
    43.           }
    44.       }
    45.  
    46.       public void SendMenuId()
    47.       {
    48.           menu.SetAnimatorBool(menuId);
    49.       }
    50. }
    The thing that i want to do, when i press escape and the main menu is open, i want to close the main menu and same thing with the highscore menu. What is the best way to do this ?
    I've been working on this for couple of days now but i cant find a good solution...
    Thank you guys in advance!
    :)
     
    Last edited: Jul 24, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    When showing any menu, save reference to it somewhere in class fields. When ESC is pressed, check that reference and if it is not null, hide the menu it references and nullify the reference itself.
     
  3. unity_4bPeHNiaedralw

    unity_4bPeHNiaedralw

    Joined:
    Sep 18, 2018
    Posts:
    2
    Thank you for the replay! I changed my code to this and now it's working :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MenuManager : MonoBehaviour
    6. {
    7.     public List<Menu> Menus = new List<Menu>();
    8.  
    9.     Menu menu;
    10.  
    11.     public void Update()
    12.     {
    13.         ShowMenuOnKeyDown();
    14.     }
    15.  
    16.     public void Start()
    17.     {
    18.         var menu = FindObjectOfType<Menu>();
    19.         ShowMenu(menu);
    20.     }
    21.     private void Awake()
    22.     {
    23.         DontDestroyOnLoad(gameObject);
    24.     }
    25.  
    26.     public void ShowMenu(Menu menu)
    27.     {
    28.         if (menu != null)
    29.             menu.IsOpen = false;
    30.  
    31.         if (!Menus.Contains(menu))
    32.             Menus.Add(menu);
    33.  
    34.         foreach (var m in Menus)
    35.         {
    36.             m.IsOpen = false;
    37.         }
    38.  
    39.         menu.IsOpen = true;
    40.     }
    41.     public void ShowMenuOnKeyDown()
    42.     {
    43.         if (!Input.GetKeyDown(KeyCode.Escape))
    44.             return;
    45.  
    46.         foreach (var m in Menus)
    47.         {
    48.             if (m.name == "Main Menu")
    49.             {
    50.                 m.IsOpen = !m.IsOpen;
    51.             }
    52.             else
    53.             {
    54.                 m.IsOpen = false;
    55.             }
    56.         }
    57.     }
    58.  
    59. }
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Now it looks better. Next thing you'll probably want will be few open dialogs and the back button. For this, you can replace the open menu reference with Stack<T> collection.