- Home /
How can I change the position of an enum toggle?
I just seen a thread about toggles and it turned out to be exactly what I was looking for. My question is how can I use an enum on custom toggles?
For example, I have 2 custom toggles and I want make it so that one when box is selected, it will automatically deselect the other box? It is similar to another thread but the other one is for basic toggles.
These are my buttons, and this is the code for them.
enum ButtonPressed {Stats, Info};
ButtonPressed selectedButton;
void OnGUI() {
foreach (ButtonPressed value in ButtonPressed.GetValues(typeof(ButtonPressed))) {
if(GUILayout.Toggle (value == selectedButton, value.ToString() + "")){
selectedButton = value;
}
}
isStats = GUI.Toggle(new Rect (1460, 521, 110, 35), isStats, "");
isInfo = GUI.Toggle (new Rect (1275, 520, 110, 35), isInfo, "");
}
Ok, now my question again. How can I make the enum count for the two bools I have for the buttons, instead of just making two different buttons?
Answer by SkaredCreations · Dec 17, 2014 at 12:53 PM
If you're not really near the completion of your project and you're using Unity 4.6 then I'd suggest to switch to the new UI tools where you have a ToggleGroup component because it's much easier to manage a visual UI.
Else you need to couple your toggles with the enum, for example:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Test : MonoBehaviour {
enum ButtonPressed {Stats, Info};
class MyButton {
public ButtonPressed value;
public Rect rect;
public GUIContent content;
public MyButton(ButtonPressed value, Rect rect, GUIContent content) {
this.value = value;
this.rect = rect;
this.content = content;
}
}
List<MyButton> buttons = new List<MyButton>();
MyButton selectedButton;
void Start() {
buttons.Add(new MyButton( ButtonPressed.Info, new Rect (0, 0, 110, 35), new GUIContent("Info") ));
buttons.Add(new MyButton( ButtonPressed.Stats, new Rect (0, 40, 110, 35), new GUIContent("Stats") ));
}
void OnGUI() {
foreach (MyButton button in buttons) {
if (GUI.Toggle(button.rect, (selectedButton == button), button.content)) {
selectedButton = button;
}
}
}
}
Sweet thanks, works perfectly. Now what about the ToggleGroup? What does that do exactly?
ToghleGroup does exactly what I did in the code, just assign the toggle group to the toggles. It's part of the new UI tools shipped in the version 4.6. Please mark the answer as accepted so it goes away from queue, if you have enough karma points on UA to do that.
For future reference, do you $$anonymous$$d explaining how to do this? I'm playing around with it right now and I don't really understand it. Does it require scripting? How can I attach the togglegroup script to the GUIToggles made in a different script?
As I already said, ToggleGroup is part of the NEW UI, not UnityGUI :)
So, if it's part of the new UI, am I capable of attaching skins to this "ToggleGroup" I am kind of confused on what you mean. I read the documents for the ToggleGroup and attempted it, I added Toggles and grouped them together, but neither of them would toggle. Should I just make a new question?
Your answer
