Search Unity

4x4 grid of buttons that need three "visual states"

Discussion in 'Immediate Mode GUI (IMGUI)' started by DaveyJJ, Apr 17, 2009.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    I have a 4x4 grid of buttons. I need to cycle through and display (the same) three visual states when a button is tapped ...

    Not tapped = no texture seen
    Tapped once = texture A
    Tapped a second time = texture B
    Tapped a third time = back to no texture seen

    All 16 buttons use the same textures A and B.

    Can someone tell me the best way to do that programmatically? Thank you!
     
  2. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    No one?
     
  3. careyagimon

    careyagimon

    Joined:
    Dec 20, 2007
    Posts:
    209
    Code (csharp):
    1.  
    2. var buttonTextures : Texture2D[];
    3. var rows : int = 4;
    4. var columns : int = 4;
    5.  
    6. private var buttonStates : int[];
    7.  
    8. function Start () {
    9.    buttonStates = new int[rows * collumns];
    10.   for(i=0; i<buttonStates.Length; i++) {
    11.     buttonStates[i] = -1;
    12.   }
    13. }
    14.  
    15. function OnGUI () {
    16.   GUILayout.BeginVertical();
    17.     for(i=0; i<rows; i++) {
    18.       GUILayout.BeginHorizontal();
    19.         for(j=0; j<columns; j++) {
    20.           if(buttonsStates[i*columns + j] > -1) {
    21.             if(GUILayout.Button(buttonTextures[buttonsStates[i*columns + j]]) {
    22.               buttonsStates[i*columns + j]++;
    23.               if(buttonsStates[i*columns + j] >= buttonTextures.Length)
    24.               buttonsStates[i*columns + j] = -1;
    25.             }
    26.           } else {
    27.             if(GUILayout.Button("") {
    28.               buttonsStates[i*columns + j]++;
    29.               if(buttonsStates[i*columns + j] >= buttonTextures.Length)
    30.               buttonsStates[i*columns + j] = -1;
    31.             }
    32.           }
    33.         }
    34.       GUILayout.EndHorizontal();
    35.     }
    36.   GUILayout.EndVertical();
    37. }
    This was hastily typed up, so you might need to tweak it into working. The whole buttonStates = -1 thing might be able to be simplified if GUILayout.Button(null) works. If so, you could use a texture array with the first entry empty. I haven't tested it myself. Hope this gets you started!
     
  4. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    It will, thanks! I usually just need a push in the right direction and then can hack things to make them work to actually understand them.