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

Select menu buttons ?

Discussion in 'UGUI & TextMesh Pro' started by Quast, Aug 25, 2016.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Hi Guys
    In my scene there are three Canvas. One for menu buttons, other options, last one for the levels select.
    Now when i use xbox controller to select from menu buttons I can see that is moving to option and levels canvas ! these two canvas are hidden (option.enabled = false) but i can select from it ! I don't want my selection to move from one to another canvas. Only the active one can select from it and not allowed to move to hidden canvas.

    How to solve this ?
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    This might be a logical bug in the UI system. I haven't done any testing. But I do notice if I SetActive(false) on a Canvas, (unclick the enabled box at the top in the Editor) the children are all still active. Disabling a parent definitely causes all the children not to be drawn or seen by the Event System.. but maybe the selection function isn't checking if any of a button's parents are active. You could right your own disabling code that disables or enables all the children.


    Code (CSharp):
    1. public void MySetActive(GameObject object,bool flag)
    2. {
    3.     Transform[] children = object.GetComponentsInChildren<Transform>();
    4.    for (int i=0;i<children.Count;i++)
    5.             MySetActive(children[ i ].gameObject,flag);
    6.        object.SetActive(flag);
    7. }
    I just realized the above code might not work. I think once you SetActiveFalse on an object, GetComponent Doesn't work anymore. If you know your Canvas only has buttons your worried about you can try this instead:

    Code (CSharp):
    1. public void MySetEnable(Canvas canvas, bool flag)
    2. {
    3.     Button[] buttons = object.GetComponentsInChildren<Button>();
    4.      for (int i=0;i<buttons.Count;i++)
    5.           buttons[ i ].enabled = flag;
    6.       canvas.enabled = flag;
    7. }
     
    Last edited: Aug 26, 2016
  3. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Instead of having 3 canvases have just 1, but make 3 empty objects. It would be much easier to control each other.
     
    Quast likes this.
  4. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Sorry, I was busy.
    I did what you say. it's work with Plane and gameobject. just one more problem.

    Here in main menu the new game button is first selected. what about the other option and levels menus. how to define the first selected this in these menus ?
     
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You probably need to do that in a script. Whenever you enable one of the gameObjects with buttons in them
    Code (CSharp):
    1. EventSystem.SetSelectedGameObject(buttonYouwant);
     
    Quast likes this.
  6. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Thank you takatok ;)
    [SOLVERD]