- Home /
linked GUILayout.Toggle
hi folks!
is there an easy way to link two toggle buttons, f.e. for choosing one language option out of two possible languages?
thnx!
Answer by spinaljack · May 06, 2010 at 11:58 AM
Read the manual about GUI styles.
I think you want the tool bar as it lets you have any number of language options and only one can be active at any one time. You can also have it in a column instead of a row if you prefer.
/* GUI.Toolbar example */
var toolbarInt = 0; var toolbarStrings : String[] = ["Toolbar1", "Toolbar2", "Toolbar3"];
function OnGUI () { toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings); }
oh, i've overseen this when i was searching for the one-toggle-only-option. thanky you!
Answer by Ray-Pendergraph · May 06, 2010 at 11:38 AM
This seems to work for just two options:
bool choice1 = true; bool choice2 = false;
void OnGUI(){ choice1 = GUILayout.Toggle(!choice2, new GUIContent("Choice 1")); choice2 = GUILayout.Toggle(!choice1,new GUIContent("Choice 2")); }
I have used a more general purpose solution multiple options I called a toggle group. The controller paints the group of buttons and the index of the true toggle gets painted with a true in the GUILayout call. If a button is pressed (other than the current true toggle) the controller's selected index gets reset to the new index.
Answer by luhuiya · Dec 09, 2011 at 04:32 PM
for (var i=0;i<4;i++){
toogleBool[i] = GUI.Toggle(Rect(locX + (200 * (i + 1)),locY + (disY * 3),200,40),toogleBool[i],"Choice " + (i+1));
}
for (i=0;i<4;i++){
if(activeToogle != i)
{
if(toogleBool[i] == true)
{
toogleBool[activeToogle] = false;
toogleBool[i] = true;
activeToogle = i;
}
}
}
Answer by luhuiya · Dec 09, 2011 at 04:32 PM
var toogleBool = new Array(true,false,false,false);
var activeToogle = 0;
function OnGUI()
{
for (var i=0;i<4;i++){
toogleBool[i] = GUI.Toggle(Rect((200 * (i + 1)),100,toogleBool[i],"Choice " + (i+1));
}
for (i=0;i<4;i++){
if(activeToogle != i)
{
if(toogleBool[i] == true)
{
toogleBool[activeToogle] = false;
toogleBool[i] = true;
activeToogle = i;
}
}
}
}