Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Here's a radio buttons solution .. (RadioButtons)

Discussion in 'UGUI & TextMesh Pro' started by Fattie, Nov 19, 2014.

  1. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    475
    earlier i asked if there was a radio buttons solution kicking around

    here's one - very easy to use

    make a PANEL, put any buttons you want IN THE PANEL.

    drop the script on the PANEL and it's done, nothing else to do.

    hope it saves someone ten minutes

    Screen Shot 2014-11-19 at 18.54.54.png


    RadioButtons.cs ..

    Code (csharp):
    1.  
    2. /*
    3. For Unity3D's new UI (4.6>)
    4.  
    5. Automatic, easy-to-use implementation of RadioButtons
    6. (ie, exclusiveOr buttons, http://en.wikipedia.org/wiki/Radio_button)
    7.  
    8. (1) simply MAKE A PANEL (ie, UnityEngine.UI.Panel)
    9.  
    10. (2) put a few BUTTONS INSIDE THE PANEL (ie, UnityEngine.UI.Button)
    11.  
    12. (3) in the editor set the NAME (.name) of the EACH BUTTON to the logical
    13. value you want for that button.  For example, "a", "b" and "c".
    14. Note that the text on the button is irrelevant - use anything.
    15.  
    16. (4) Drop this script ON THE PANEL.
    17.  
    18. (5) That's it - it's all automatic.
    19.  
    20. Note -- if you leave the default value blank, it's the first one.
    21.  
    22. Note -- there's no problem having other items in the Panel (eg, labels etc)
    23.  
    24. Note -- just set the event (in the editor) same way as with all Unity UI controls
    25. Simply look at the value (.currentValue) when you get the onValueChanged callback.
    26.  
    27. Note -- the callback "onValueChanged" WILL NOT BE CALLED,
    28. when the default is set at startup, etc etc.
    29. only when user pushes a button or you call ForceToValue().
    30.  
    31. Note -- call ForceToValue() to change (display and currentValue) programmatically.
    32. if you send a nonexistent value it goes to the default.
    33.  
    34. Note -- this script is self-contained. No extensions etc needed.
    35.  
    36. Note -- it simply uses white / blue for the selected.
    37. If you really need to change that, it's easy
    38. */
    39.  
    40. using System;
    41. using UnityEngine;
    42. using UnityEngine.UI;
    43. using UnityEngine.Events;
    44.  
    45. public class RadioButtons:MonoBehaviour
    46.     {
    47.     public string defaultValue;
    48.  
    49.     public UnityEvent onValueChanged;
    50.     [NonSerialized] public string currentValue;
    51.  
    52.     public void ForceToValue(string v)
    53.         {
    54.         _oneRadioButtonLiterallyClicked(v);
    55.         }
    56.  
    57.     ///////////////////////////////////////////////////////////////////
    58.  
    59.     void Awake()
    60.         {
    61.         _setup();
    62.         _unselectAll();
    63.         _selectDefault();
    64.         }
    65.  
    66.     //// private ...
    67.  
    68.     void Start()
    69.         {
    70.         }
    71.  
    72.     private void _setup()
    73.         {
    74.         int k=0;
    75.         var tts = gameObject.GetComponentsInChildren<Transform>();
    76.         foreach ( Transform tt in tts )
    77.             {
    78.             if ( tt.GetComponent<Button>() )
    79.                 {
    80.                 Button bb = tt.GetComponent<Button>();
    81.                 string val = tt.name;
    82.                 Debug.Log("\t\t\tIn RadioButtons " +gameObject.name +" there's a button: " +val);
    83.                 bb.onClick.AddListener(delegate { _oneRadioButtonLiterallyClicked(val); });
    84.                 k++;
    85.                 }
    86.             }
    87.         Debug.Log("\t\t\tIn RadioButtons " +gameObject.name +" there are count buttons: " +k);
    88.         if (k==0) Debug.Log("\t\t\tYOU HAVE AN EMPTY RadioButton Panel, named " +gameObject.name);
    89.         }
    90.  
    91.     public void _oneRadioButtonLiterallyClicked(string v)
    92.         {
    93.         _unselectAll();
    94.         _selectValue(v);
    95.         onValueChanged.Invoke();
    96.         }
    97.  
    98.     /// set the whole group ...
    99.  
    100.     private void _unselectAll()
    101.         {
    102.         var tts = gameObject.GetComponentsInChildren<Transform>();
    103.         foreach ( Transform tt in tts )
    104.             {
    105.             if ( tt.GetComponent<Button>() )
    106.                 {
    107.                 Button bb = tt.GetComponent<Button>();
    108.                 _unselectedStyle(bb);
    109.                 }
    110.             }
    111.         }
    112.  
    113.     private void _selectValue(string v)    // select this named button; and set the currentValue
    114.         {
    115.         var tts = gameObject.GetComponentsInChildren<Transform>();
    116.         foreach ( Transform tt in tts )
    117.             {
    118.             if ( tt.GetComponent<Button>() )
    119.                 {
    120.                 if ( tt.name == v )
    121.                     {
    122.                     Button bb = tt.GetComponent<Button>();
    123.                     _selectedStyle(bb);
    124.                     currentValue = tt.name;
    125.                     return;
    126.                     }
    127.                 }
    128.             }
    129.         Debug.Log("\t\t\tNON-EXISTENT VALUE " +v +" for RadioButton Panel, named " +gameObject.name);
    130.         _selectDefault();
    131.         }
    132.  
    133.     private void _selectDefault()    // if dev has not set default in editor, use first one
    134.         {
    135.         if ( defaultValue == "" )
    136.             {
    137.             _selectFirstOne();
    138.             return;
    139.             }
    140.  
    141.         var tts = gameObject.GetComponentsInChildren<Transform>();
    142.         foreach ( Transform tt in tts )
    143.             {
    144.             if ( tt.GetComponent<Button>() )
    145.                 {
    146.                 if ( tt.name == defaultValue )
    147.                     {
    148.                     Button bb = tt.GetComponent<Button>();
    149.                     _selectedStyle(bb);
    150.                     currentValue = tt.name;
    151.                     return;
    152.                     }
    153.                 }
    154.             }
    155.         Debug.Log("\t\t\tNON-EXISTENT DEFAULT VALUE on RadioButton Panel, named " +gameObject.name);
    156.         currentValue = "";
    157.         }
    158.  
    159.     private void _selectFirstOne()
    160.         {
    161.         var tts = gameObject.GetComponentsInChildren<Transform>();
    162.         foreach ( Transform tt in tts )
    163.             {
    164.             if ( tt.GetComponent<Button>() )
    165.                 {
    166.                 Button bb = tt.GetComponent<Button>();
    167.                 _selectedStyle(bb);
    168.                 currentValue = tt.name;
    169.                 return;
    170.                 }
    171.             }
    172.         Debug.Log("\t\t\tYOU HAVE AN EMPTY RadioButton Panel, named " +gameObject.name);
    173.         currentValue = "";
    174.         }
    175.  
    176.     /// set colors on Button ...
    177.  
    178.     private void _unselectedStyle(Button bb)        // ie, "white"
    179.         {
    180.         ColorBlock cb = bb.colors;
    181.         cb.normalColor = Color.white;
    182.         cb.highlightedColor = Color.white;
    183.         bb.colors = cb;
    184.         }
    185.  
    186.     private void _selectedStyle(Button bb)        // ie, "blue"
    187.         {
    188.         ColorBlock cb = bb.colors;
    189.         cb.normalColor = Color.blue;
    190.         cb.highlightedColor = Color.blue;
    191.         bb.colors = cb;
    192.         }
    193.     }
    194.  
    195.  
    196. /*
    197. For testing, make a script UnitTest, attach to say the camera.
    198. On your RadioButtons (say, "motorSpeed"), be sure to set the
    199. OnValueChanged callback to
    200.  
    201. using UnityEngine;
    202.  
    203. public class UnitTest:MonoBehaviour
    204.     {
    205.     void Awake()
    206.         {
    207.         InvokeRepeating("_teste",3.0f,3.0f);
    208.         }
    209.  
    210.     void _teste()
    211.         {
    212.         GameObject.Find("motorSpeed").GetComponent<RadioButtons>().ForceToValue("c");
    213.         // "c" is one of the values (that is, the .name) of one of your buttons
    214.         }
    215.  
    216.     public void ClickedExample()
    217.         {
    218.         Debug.Log("Testing: motorSpeed changed to .. "+
    219.             GameObject.Find("motorSpeed").GetComponent<RadioButtons>().currentValue );
    220.         }
    221.     }
    222. */
    223.  
     
    Last edited: Nov 19, 2014
  2. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    475
    NOTE - you also have ToggleGroup as rakkage points out in the other question!

    it gives you a toggle set with on/off checkmarks

    It is not in the main "create" submenu, go to Component menu to get it. Thanks rakkage!
     
  3. Tanay-Singhal

    Tanay-Singhal

    Joined:
    Oct 9, 2014
    Posts:
    3
    Thank you! Works great!