Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Gui Selection Grid

Discussion in 'Scripting' started by kodagames, Dec 28, 2011.

  1. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Im testing a Gui Selection Grid with Images
    Code (csharp):
    1.  
    2. //guiSelectionGrid
    3. var selGridInt : int = 0;
    4. var selImages : Texture2D[];
    5.  
    6. function OnGUI ()
    7. {
    8.     selGridInt = GUI.SelectionGrid (Rect (25, 25, 200, 200), selGridInt, selImages, 2);//2 is how many buttons across
    9. }
    10.  
    each button has a colored image (texture) on it (green int =0, red int =1, blue int =2, yellow int =3).

    At the start the Green button int =0 is always selected.

    Here is the tricky part or the part I need help with if possible?
    How would I Click on another button and have the Images of the last selected button and the newly selected button trade (switch, swap, etc..) Images?

    for example:
    the green button is selected by default if I click on the yellow button, the yellow one now becomes green and the green one now becomes yellow.

    $help.jpg
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    A bit trickier since you implemented it as a selection grid - but not impossible. You'll have to maintain 2 arrays. The one you have and another one that you fill with the re-ordered values. You'll also need to create another variable to hold the last value selected from the grid, compare them, and if they are not equal then the user made some selection and you need to swap the index of the 2 values in the array.
     
  3. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    What would be an easier approach? If this one is a bit trickier, Im hoping I'll be able to understand the less tricker way of doing it.
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    It does not seem that trick to me.
    Each btn when pressed must be able to see what colour its peers are. Really alll that matters if it sees green. If itself is green, do nothing. If it is not green, make it green AND make he one that was green the color that the active btn was."

    Simple really.

    In your script just have
    1. each btn as a object var.
    2. each color as a potential state. For instance aBtn.green bBtn.blue if owner btn blue, make blue.
    3. On button pressed... Check color of owner and if not green, store color as it is to be passed to old green.
    Get which btn is green and swap.



    Simple i think.
     
  5. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Thanks renman3000 Im trying it out right now. :) I'll post back.
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
  7. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Im trying to follow your instructions but Im stuck at number 3.
    Im just not really clear check color of owner if not green store color as it is to be passed to old green.
    Im not sure how to store the color and Im guessing Id have to check against every other color as well?

    this is what I came up with so far:

    Code (csharp):
    1.  
    2. //guiSelectionGrid
    3. var selGridInt : int = 0;
    4. var selImages : Texture2D[];
    5.  
    6. //each color as a potential state
    7. var greenColor : Texture2D;
    8. var blueColor : Texture2D;
    9. var redColor : Texture2D;
    10. var yellowColor : Texture2D;
    11.  
    12. function OnGUI ()
    13. {
    14.     selGridInt = GUI.SelectionGrid (Rect (25, 25, 200, 200), selGridInt, selImages, 2);//2 is how many buttons across
    15. }
    16.  
    17. function Update()
    18. {
    19.     //each button as a object variable (int)
    20.     if(selGridInt == 0)
    21.     {
    22.         var greenBtn = selImages [0];
    23.         selImages[0] = greenColor;  //if owner green make green
    24.         print("Green");
    25.     }
    26.     if(selGridInt == 1)
    27.     {
    28.         var blueBtn = selImages [1];
    29.         selImages[1] = blueColor;   //if owner blue make blue
    30.         print("Blue"); 
    31.     }
    32.     if(selGridInt == 2)
    33.     {
    34.         var redBtn = selImages [2];
    35.         selImages[2] = redColor;    //if owner red make red
    36.         print("Red");  
    37.     }
    38.     if(selGridInt == 3)
    39.     {
    40.         var yellowBtn = selImages [3];
    41.         selImages[3] = yellowColor; //if owner yellow make yellow
    42.         print("Yellow");   
    43.     }
    44. }
    45.  
     
  8. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Ok I think Im working it out, an addition to above Store tempColor when clicked (I've added this to each button and its storing any button color that I click). Hopefully have some more progress in a few :)

    Code (csharp):
    1.  
    2. if(selGridInt == 0)
    3. {
    4.         var greenBtn = selImages [0];
    5.         selImages[0] = greenColor;  //if owner green make green
    6.         print("Green");
    7.        
    8.         //store tempColor when clicked
    9.         tempColor = selImages[0];
    10. }
    11.  
     
  9. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    I hope it is working.
    Your coding is not familar with me.
    If it works for you tho great.
    Where is your input tho?
    And is tnis a single script or all or each btn?
     
    Last edited: Jan 2, 2012
  10. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Ahh... Still not working

    I don't have any input I believe when you use a selection grid anytime you click a button the selGridInt will change 0 to 3 if you have 4 buttons so im using that variable as my Input. If that makes any sense?

    Its a single script for all and its attached to the Main camera then I just drag and drop the textures that'l be used for the colors (green, blue etc..)

    Is there any chance you have a code example of how to do something like this? its ok if the code is totally different :) I'll try anything, i've been trying to figure this out for a few days now and still no luck (i've come close a few times though). Im gonna try it again but a different way this time maybe regular buttons?
     
    Last edited: Jan 2, 2012
  11. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    I think the problem is that you are keeping your btns constant in color. What i mean by that is... It seems that if btn int == 0 then it is green correct? Well that is fine but to me it looks like you have not built a system which separates btn 1, 2, 3, 4 from color 1,2,3,4. So btn 0 will always be green. Not good.

    You need to first recognise the btn pressed.
    Then the color of the btn pressed.
    If btn pressed is not green then get color and store var.
    Then, check for which btn is green.
    Upon finding btn that is green swap its green color for stored var (btn pressed color)
    And make btn pressed color green.


    It should look something like ( forgive me i am on an ipad)

    //btnx pressed
    Get color of btn x
    If color != green
    Check other btn colors. ///how u do this is infintesmal..many ways
    Find btn y with green color
    Swap btn x and y colors.
     
  12. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Giving it another shot :)
     
  13. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Good luck!