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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

GUI.SelectionGrid() how to detect if a button was pressed

Discussion in 'Immediate Mode GUI (IMGUI)' started by llavigne, Oct 29, 2009.

  1. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    SelectionGrid returns a number even if nothing is pressed, how do I detect that user has pressed a button ?
     
  2. Timmer

    Timmer

    Joined:
    Jul 28, 2008
    Posts:
    330
    Hello,

    IIRC, SelectionGrid assumes one of the buttons is always selected and it just returns the index of that selected button. So there is no "onclick" type events just a return that tells you which button within the grid is selected.
     
  3. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    what is a common situation where you would use it ?
     
  4. Timmer

    Timmer

    Joined:
    Jul 28, 2008
    Posts:
    330
    Hopefully someone else can jump in here as I've never found a use for it. ;)

    (then again, maybe that is the answer)
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can't detect if a selected grid button has been clicked again when using GUI.SelectionGrid. You have to monitor the clicks and find the rectangle in which each click occurs.
     
  6. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,593
    I just use if (GUI.Changed) to check it an event happened and then check the index
     
  7. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    I've seen people taling about using it for inventory screens. I can see ways it would be used for puzzle games, too.
     
  8. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    How do I get the rectangle of a selected button ?
     
  9. Timmer

    Timmer

    Joined:
    Jul 28, 2008
    Posts:
    330
    I think you just have to figure it out yourself (if the bounds is X wide and you know there are two buttons in your grid then you can figure out the bounds for each button).

    This is one of the reasons why I haven't used this particular function -- it seems easier to just make your own at this point ;)
     
  10. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    476
    Except if your selection grid is bigger than the screen and uses a scrollbar it will report GUI.Changed when you touch the scrollbar to view more of the selection.
     
  11. patrick_vargas

    patrick_vargas

    Joined:
    Oct 13, 2011
    Posts:
    11
    dude...take the example.

    int _ClickedToolBarIndex = 0;
    string[] _ToolBarStrings = new string[] {"Option 1", "Option 2", "Option 3", "Cancel"};

    void OnGUI()
    {
    _ClickedToolBarIndex = GUI.Toolbar (new Rect (200, 5, 750, 15), _ClickedToolBarIndex, _ToolBarStrings);
    if(_ClickedToolBarIndex == 0)
    {
    //do it
    }
    if(_ClickedToolBarIndex == 1)
    {
    //do that
    }
    if(_ClickedToolBarIndex == 2)
    {
    ///take it
    }
    if(_ClickedToolBarIndex == 3)
    {
    //take that
    }
    if(_ClickedToolBarIndex == 4)
    {
    ///good bye
    }
    }
     
  12. archivision

    archivision

    Joined:
    Jun 17, 2009
    Posts:
    91
    hi,

    i know this is an really old threat.. but i figure that there are some people who may need this.
    at least.. i did.
    and i found a way to fix this.

     
    zanouk likes this.
  13. shlakva

    shlakva

    Joined:
    Oct 15, 2017
    Posts:
    13
    Hello guys

    Without any workaround just set you returned int to "-1" after proccessing:
    Code (CSharp):
    1.  
    2. int selInt = 0; // I have zero here and everything is ok, but anyway, try initialize with "-1"
    3. public void OnGUI()
    4. {
    5.     selInt = GUILayout.SelectionGrid(selInt,...);
    6.     if (selInt...)
    7.     {
    8.         //some action according to your business logic
    9.     }
    10.      selInt = -1;
    11. }
    12.