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

Mutually exclusive toggle buttons?

Discussion in 'Immediate Mode GUI (IMGUI)' started by rkite, Jun 22, 2010.

  1. rkite

    rkite

    Joined:
    Feb 22, 2010
    Posts:
    33
    Howdy folks. Here is my question for today.

    I am trying to make some mutually exclusive toggle buttons to select one option from a set of options.

    The problem I have is that when you click on any of the toggle buttons it does not behave quite the way I want.
    What I am looking for is for each toggle to toggle on when clicked, and for it to toggle off the others in the same group if they are on.

    What I don’t want is if I click on a toggle that is on, for it to turn off and turn on one of the others, which is what this code does.

    Can anyone shed some light on how I would achieve the effect I am looking for?

    I am able to set a GUILayout.Toggle to be one of two options by doing this:
    Code (csharp):
    1.  
    2. OptonOne = GUILayout.Toggle(!OptionTwo, "OptonOne");
    3. OptionTwo = GUILayout.Toggle(!OptonOne, "OptionTwo");
    4.  
    This only allows one of the toggles to be true at a time.

    I want to have four mutually exclusive options and it kind of works if I do this:
    Code (csharp):
    1.  
    2. OptionThree = GUILayout.Toggle(!OptionFour  !OptionFive  !OptionSix, new GUIContent("OptionThree", ""));
    3.  
    4. OptionFour = GUILayout.Toggle(!OptionThree  !OptionFive  !OptionSix, new GUIContent("OptionFour", ""));
    5.  
    6. OptionFive = GUILayout.Toggle(!OptionThree  !OptionFour  !OptionSix, new GUIContent("OptionFive", ""));
    7.  
    8. OptionSix = GUILayout.Toggle(!OptionThree  !OptionFour  !OptionFive, new GUIContent("OptionSix", ""));
    9.  
    If you try more than four at once crashes Unity. :(
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I'm not getting a crash when I add an extra option with this code. Can you post the code that crashes Unity when you use it?
     
  3. cncguy

    cncguy

    Joined:
    Oct 26, 2009
    Posts:
    53
  4. rkite

    rkite

    Joined:
    Feb 22, 2010
    Posts:
    33
    it crashes if you use somthing like this:


    Code (csharp):
    1. OptionSeven = GUILayout.Toggle(!OptionThree  !OptionFour  !OptionFive, !OptionSix, new GUIContent("OptionSix", ""));

    Thanks, I will give that a try.
     
  5. bedford

    bedford

    Joined:
    Jan 29, 2012
    Posts:
    33
    This has been asked a while ago, I know, but I found an easier way and it may help other people looking for a solution (I had many toggles on same group) This is probable not an elegant solution but I'm still a newbie...
    Code (csharp):
    1. //declaration
    2. private bool q1a;
    3. private bool q1b;
    4. private bool q1c;
    5.  
    6. void OnGUI(){
    7.   change1a(GUI.Toggle(new Rect(10,10,10,10),q1a,""));
    8.   change1b(GUI.Toggle(new Rect(30,10,10,10),q1b,""));
    9.   change1c(GUI.Toggle(new Rect(50,10,10,10),q1c,""));
    10. }
    11.  
    12. private void change1a(bool newvalue){
    13.   if(newvalue){
    14.     q1b = false;
    15.     q1c = false;
    16.   }
    17.   q1a = newvalue;
    18. }
    19.  
    20. private void change1b(bool newvalue){
    21.   if(newvalue){
    22.     q1a = false;
    23.     q1c = false;
    24.   }
    25.   q1b = newvalue;
    26. }
    27.  
    28. private void change1c(bool newvalue){
    29.   if(newvalue){
    30.     q1a = false;
    31.     q1b = false;
    32.   }
    33.   q1c = newvalue;
    34. }
    Hope this help !
     
  6. ModStoryGames

    ModStoryGames

    Joined:
    Apr 27, 2012
    Posts:
    179
    Code (csharp):
    1.  
    2.         /// <summary>
    3.         /// Displays a vertical list of toggles and returns the index of the selected item.
    4.         /// </summary>
    5.         public static int ToggleList(int selected, GUIContent[] items)
    6.         {
    7.             // Keep the selected index within the bounds of the items array
    8.             selected = selected < 0 ? 0 : selected >= items.Length ? items.Length - 1 : selected;
    9.  
    10.             GUILayout.BeginVertical();
    11.             for (int i = 0; i < items.Length; i++)
    12.             {
    13.                 // Display toggle. Get if toggle changed.
    14.                 bool change = GUILayout.Toggle(selected == i, items[i]);
    15.                 // If changed, set selected to current index.
    16.                 if (change)
    17.                     selected = i;
    18.             }
    19.             GUILayout.EndVertical();
    20.  
    21.             // Return the currently selected item's index
    22.             return selected;
    23.         }
    24.  
    This will allow you to put in an array of GUIContents as your items, so all you have to do is check items[selected] to see what is selected. This can be modified to automatically come with a ScrollView as well, if needed, though it works fine as-is. I tested it with four items, but virtually any length will work fine.

    EDIT:

    Alternatively, you can use SelectionGrid and assign it the GUI.skin.toggle GUISkin, which will result in a similar looking control. However, I notice a bit of a pause in the control state graphics changes using this method (I mean, when you select a toggle, it pauses at the Clicked graphic for a moment before moving to the selected graphic). So either method works, but mine does not have the pause so far as I can tell.
     
    Last edited: Sep 24, 2012
    yasirkula and MUGIK like this.