Search Unity

exclude buttons from navigation in menu

Discussion in '2D' started by snakeeye98, Sep 26, 2019.

  1. snakeeye98

    snakeeye98

    Joined:
    Sep 2, 2019
    Posts:
    17
    Hello,
    I have an "animated shop"-panel wich opens when I click a button. Since this panel overlaps the first layer of the main menu I disabled the buttons under it to not make them clickable anymore - works so far. But I can still navigate through them, even though the navigation should stay inside the newly opened panel. I only want to enable navigation inside the panel as long as it is opened.

    This is how I made them non-interactable:
    Code (CSharp):
    1. public void toggleButtonActive()
    2.     {
    3.         Animator animator = Panel.GetComponent<Animator>();
    4.         bool isOpen = animator.GetBool("open");
    5.  
    6.         if (isOpen == true)
    7.         {
    8.             btns = GameObject.FindGameObjectsWithTag("menu");
    9.  
    10.             foreach (GameObject button in btns)
    11.             {
    12.                 button.GetComponent<Button>().interactable = false;
    13.             }
    14.         }
    15.         else if (isOpen == false)
    16.         {
    17.             btns = GameObject.FindGameObjectsWithTag("menu");
    18.  
    19.             foreach (GameObject button in btns)
    20.             {
    21.                 button.GetComponent<Button>().interactable = true;
    22.             }
    23.         }
    24.     }
    Are there any kind of "Button-Goups" I can create and exclude from the navigation? I already have the buttons tagged as you see. Maybe I can use this tag to exclude them from navigation with mouse and keyboard?

    Thank you.
     
    Last edited: Sep 26, 2019
  2. snakeeye98

    snakeeye98

    Joined:
    Sep 2, 2019
    Posts:
    17
    Is there a parameter of a button I can set? Something similar to "button.interactable". Maybe "button.navigatable". But I can't find such parameter since "button.navigation" just changes the keypress needed to navigate to the button (I don't think its possible to assign a NULL value there).
     
  3. ZliVuk

    ZliVuk

    Joined:
    Mar 24, 2018
    Posts:
    61
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Well, you pretty much said it. You need to access the buttons "interactable".

    let's say your button is named, by a variable, "button"

    Code (CSharp):
    1. button.interactable = false; // Can't click
    2.  
    3. button.interactable = true; // Can Click
     
  5. snakeeye98

    snakeeye98

    Joined:
    Sep 2, 2019
    Posts:
    17
    button.interactable does not change the possibillity to navigate to the button. It only changes the ability to click it. if I only have 1 button on my shop panel I can still press the top, bottom, left and right arrow keys to navigate away from the shop button on the panel. And then the buttons under the shop overlay panel become selected (but not clicked!)

    I hope you could understand me now, sorry for possible confusion.
     
    Last edited: Sep 30, 2019
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I'm assuming you have your button('s) set up like this:
    Code (CSharp):
    1. [SerializedField]private Button myButton;
    Instead, you can do it like this:
    Code (CSharp):
    1. [SerializedField]private GameObject myButton;
    You can now use SetActive(false) to completely remove the button from the screen.
    Use SetActive(true) to make it reappear.

    Code (CSharp):
    1. myButton.SetActive(false); // Button is gone
    2. myButton.SetActive(true); // Button is back
    If you have a script attached to your buttons, I don't think this is a good approach unless you don't need the script('s) until the button is active.

    Another option is to put your buttons into an array. Have an active button array and when a button is disabled, remove it from the array and add it into the inactive array. Use a foreach() to quickly SetActive(false) for each item in the inactive array. Then you can remove the button from the inactive array and set it back into the active array when you need to with the same principle.

    Code (CSharp):
    1. [SerializeField]private GameObject[] myActiveButtons;
    2. [SerializeField] private GameObject[] myInactiveButtons;
    3.  
    4. void Start()
    5. {
    6. foreach(GameObject button in myActiveButtons)
    7. {
    8. button.SetActive(true);
    9. }
    10.  
    11. foreach(GameObject button in myInactiveButtons)
    12. {
    13. button.SetActive(false);
    14. }
    15. }
    You'd really design a method though that adds/removes the buttons as needed and puts them in the proper list(and the foreach loops will be inside the method not Start). More coding than I feel like doing at the moment, for I'm half asleep right now. I hope you catch my drift here.
     
    snakeeye98 likes this.
  7. snakeeye98

    snakeeye98

    Joined:
    Sep 2, 2019
    Posts:
    17
    Thank you, that's a simple solution! I am wondering why I did not have this idea myself. You helped me a lot. :)
     
    MisterSkitz likes this.
  8. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Glad to help, amigo! :)