Search Unity

[SOLVED] How to track GUI.SelectionGrid clicks properly?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Ony, Sep 30, 2009.

  1. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,977
    I have the following code:

    Code (csharp):
    1. var selectedOutfit : int = 0;
    2. var checkInt : int = 0;
    3.  
    4. function OnGUI () {
    5.  
    6.     GUI.BeginGroup (Rect (0, 0, 500, Screen.height));
    7.  
    8.     selectedOutfit = GUI.SelectionGrid(Rect(10, 60, 480, 600), selectedOutfit, theButtonContent, 2);
    9.    
    10.     GUI.EndGroup ();
    11.  
    12. }
    13.  
    14.  
    15. function Update()
    16. {
    17.    if (selectedOutfit != checkInt) {
    18.       print ("clicked: " + selectedOutfit);
    19.       checkInt = selectedOutfit;
    20.    }
    21. }
    22.  
    23.  
    When I run this I can click each button in the grid (there are 8 total), and as I click each one it prints in the console that it was clicked. So far so good.

    The problem is, I can only get each button to register its click once. If I click it again it does nothing.

    Can someone tell me what I'm doing wrong? Also, is there some better way to track clicks from a selectiongrid?

    *(theButtonContent in my code is an array containing the GUIContent strings and is working fine.)
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Are you saying that once you have selected an item, subsequent clicks on the same item aren't registered? The SelectionGrid method can only report when the selected item has changed. You can use the Event to detect GUI mouse clicks. You could try checking each mouse click to see if it occurs within the SelectionGrid rectangle - if it does, but the selected item doesn't change, then the currently selected item must have been clicked a second time.
     
  3. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,977
    Yes, that's correct. If I click a second time nothing registers. I even did the code in several different ways to see if it was just my Update code, but each gave the same result. For instance, I tried doing a GUI.changed section inside the OnGUI and got the exact same thing: each button would register once, and that's it. Subsequent clicks did nothing.

    I'm going to mess around with it here some more including trying some things with an "event", but if it ends up not working I guess I'll have to just lay out a button grid by hand.

    ADDED: Ok I just did some more testing and if I watch the Inspector window my variables are indeed registering each time I click a button, but the "print" code, or even Debug.Log code, will only print to the console once per click. So it appears that there isn't an issue after all with the buttons in the grid registering, but rather an issue with the console only wanting to print a result for the first click of each one and then ignoring them after that. Weird.

    In any case, at least I can see now that they are actually registering, so I'll move on from here and test some functions with them.
     
  4. 30harry

    30harry

    Joined:
    Nov 19, 2008
    Posts:
    8
    Sounds like you have the "Collapse" option set in the console window. Make sure it's not selected - I think that it's on by default when you first install
     
  5. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,977
    That was it! Heh, thank you. I got the buttons working fine with the functions I was using them with but still the console would only log each hit once; the first time I did it. I turned off "collapse" and now each hit shows in the console just fine. Awesome.
     
  6. Will Gifford

    Will Gifford

    Joined:
    Sep 7, 2009
    Posts:
    11
    Can you post your solution. I have a similar problem. I can detect everything but the fact I clicked on the same SelectionGrid button more than once. I'm using the SelectionGrid as a poor man's combobox.

    Thanks!
     
  7. gevarre

    gevarre

    Joined:
    Jan 19, 2009
    Posts:
    132
    I know this is an old thread, but I'm bumping this old thread because I just spent hours trying to figure out the same problem and I'm guessing others have, too:

    Using the manual's "GUI.changed" example in the "Controls" section, the console would only print out that I clicked each button once. I thought maybe there was a bug with the code.

    As 30harry said, the problem isn't the code. It's that the button labeled "Collapse" in the console window was checked, which evidently prevents messages from being duplicated.
     
  8. GabrielKevers

    GabrielKevers

    Joined:
    Aug 1, 2012
    Posts:
    2
    try this ;-)

    Code (csharp):
    1.  
    2.         LastSelected=-1;
    3.         LastSelected=GUI.SelectionGrid( RectPosInventaire,LastSelected,ImageInventaire,ImageInventaire.Length);
    4.         if(LastSelected>-1)
    5.         {
    6.                 print ("down :" +LastSelected);
    7.         }
    8.  
     
    Gooren likes this.