- Home /
ui radio button
hi guys, Im trying to create a UI with a radio option button in it
in the unity UI I saw i can create a toggle, so i did.
its not exsectly shaped as radio option button.
I can choose 2 options, and thats somthing i cant have, how can I change it only to one
thanks in advance
Gal
To allow only one selection at a time, use a toggle group. http://docs.unity3d.com/$$anonymous$$anual/script-ToggleGroup.html
Answer by tebandesade · May 06, 2015 at 08:05 PM
Hi ! To answer your first question, when you create a toogle you see that it has "background" as a child, click it. Now you'll see in the inspector that it has a Source Image below the Image (Script) componente. Press on the right side the circle to the right of the Source Image slot. Change your background image to whichever fits the shape you considered it is a radio option buttón.
Regarding your second question you have to add the toggle to a group. What does this mean. Create several toggles for instance and later select and drag (you have to do it simultaneously) one of the toggles different from the one you are seeing in the inspector from the Hierarchy View Panel and drag it to the Group property of the Toggle (Script) component . At the beginning you'll see the slot of this property saying None (Toggle Group). But once you drag and drop another Toggle it will appear the group that that toggle belongs to.
I was unable to drag and drop another Toggle into the Group field of a selected Toggle. But dragging a gameobject that had a ToggleGroup on it worked.
Thanks, $$anonymous$$Featherly, your answer is working fine for me.
Hi tebandesade, Thanks, it is working fine. Clicking on one toggle is deactivating other toggles which are in same toggle group. I am trying to make use of this as "Tabs" functionality in my game. Clicking on one Tab, am showing its related tab data panel and deactivating other Tabs and its related data panels. But the problem is, when the user clicks on a tab that has already been clicked, it toggles off which makes my Tab related data deactivate. It is working fine unless clicking on already selected toggle. How can i make sure, clicking on already toggled on Tab to not to toggle off that Tab? any help would be much appreciated.... Thanks $$anonymous$$ohan
Answer by yousuffahim8 · May 11, 2019 at 03:39 AM
I think this is exactly what you are looking for:
int selected =0;
public void OnGUI()
{
string[] options = new string[] { " version 1", " version 2" };
selected = GUILayout.SelectionGrid(selected, options, 1, EditorStyles.radioButton);
if (selected == 0)
{
Debug.Log("version 1 is selected");
}
else if (selected == 1)
{
Debug.Log("version 2 is selected");
}
}