Search Unity

Radio buttons

Discussion in 'Immediate Mode GUI (IMGUI)' started by ITAmember, Jun 4, 2010.

  1. ITAmember

    ITAmember

    Joined:
    Feb 13, 2010
    Posts:
    88
    I would like to make a radio button control out of multiple toggle controls. However, since each of the toggle controls are independent of each other you can select multiple ones, I only want the user to be able to select one at a time.

    I know I can do this with brute force in scripting by storing the last state of the toggle, testing for change, and setting all the others to false, but there should be a better way.

    If my description of a radio button is confusing here is a wikipedia link. http://en.wikipedia.org/wiki/Radio_button
     
  2. unitymatrix

    unitymatrix

    Joined:
    Dec 29, 2009
    Posts:
    120
    you can use SelectionGrid with toggle style to realize it.
    Code (csharp):
    1. GUILayout.SelectionGrid(0,text,count,"toggle");
    or ToolBar is also Ok
     
    aimansarie and oferei like this.
  3. ITAmember

    ITAmember

    Joined:
    Feb 13, 2010
    Posts:
    88
    Thank you, works great!
     
  4. mythicwave

    mythicwave

    Joined:
    Jul 13, 2008
    Posts:
    144
     
  5. mythicwave

    mythicwave

    Joined:
    Jul 13, 2008
    Posts:
    144
    Actually, I got it without SelectionGrid. The code isn't that tricky :roll:
     
  6. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    332
    I can't believe i fiddled farted with this for hours, when "unitymatrix" had the perfect solution!

    Thanks!

    -Raiden
     
  7. Cesarecf

    Cesarecf

    Joined:
    Sep 1, 2012
    Posts:
    3
  8. vikingfabian-com

    vikingfabian-com

    Joined:
    Dec 5, 2013
    Posts:
    9
    Toggle will give you checkboxes, use EditorStyles.radioButton instead
    Code (CSharp):
    1.  
    2. var text = new string[] { "option1", "option2", "option3", "option4" };
    3.   GUILayout.SelectionGrid(0, text, 1, EditorStyles.radioButton);
    4.  
     
    aimansarie, BLokerse, ecv80 and 4 others like this.
  9. rainabba

    rainabba

    Joined:
    Dec 10, 2014
    Posts:
    3
    Looks great, but the selection won't change and I can't find ANY decent documentation on EditorStyles.radioButton. Do I need to add a handler or something?
     
  10. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    EditorStyles is just a regular GUIStyle but it's themed with the editor art, it does not effect input at all. My guess is that you copied and pasted the code above without looking at how it works.

    If you notice the first argument is a 0 and GUILayout.SelectionGrid returns an int. Try saving that value and sending it back in.

    http://docs.unity3d.com/ScriptReference/GUILayout.SelectionGrid.html
     
  11. balthatrix

    balthatrix

    Joined:
    Mar 23, 2015
    Posts:
    3
    tarahugger and Menyet7 like this.
  12. elenzil

    elenzil

    Joined:
    Jan 23, 2014
    Posts:
    72
    this answer is true if you are using the new unity GUI system (which you should be, if it's an option)
    but sometimes it's not an option, like if you're building editor windows.
     
  13. yousuffahim8

    yousuffahim8

    Joined:
    Feb 21, 2019
    Posts:
    3
    I think this is exactly what you are looking for:
    Code (CSharp):
    1. int selected = 0;
    2. public void OnGUI()
    3.     {
    4. string[] options = new string[] { "  version 1", "  version 2" };
    5. selected = GUILayout.SelectionGrid(selected, options, 1, EditorStyles.radioButton);
    6. if (selected == 0)
    7.                 {
    8.                     Debug,Log("Version 1 is selected");
    9.                 }
    10. else if (selected == 1)
    11.                 {
    12.                     Debug,Log("Version 2 is selected");
    13.                 }
    14. }
     
    mcroswell likes this.
unityunity