- Home /
Creating a toolbar with toggleable buttons
I want to create a toolbar in the editor with buttons that remain active when clicked, to simulate "tabs" like these from the Lightmapping window:
Is it possible to make something like this look native, like in the example above? And if so, which control should I use, or any other directions?
No, it is really small... and I can see it. $$anonymous$$ust be a problem in the system. :P It is here, anyway: http://imgur.com/LTic7zo
Answer by moreyes · Jul 30, 2013 at 03:07 PM
Sorry for answering my own question.
There're actually built-in controls for this, I found out later... they are called GUILayout.Toolbar or GUI.Toolbar, and look exactly like the one from the Lightmapping window. :D
Answer by Jamora · Jul 30, 2013 at 10:39 AM
You need to use an array of booleans to back up a row of normal buttons.
If the boolean for the corresponding button is set true, then that button should be marked active. There are several ways you can do this. One way is to create a GUISkin and two styles, then set the values there. Or if you need to have it in a custom editor, then you might have to make three custom styles, inactiveButton and activeButton and a normalButton. normalButton is just a temp variable to tell the buttons. InactiveButton has the normal textures for its state, and activeButton has them inversed, i.e. looks like it's being pressed when it's not. (the values you need to modify in the GUIStyles are normal and active. ). You'll want to probably make your own textures, the default ones aren't very impressive. You can change it by accessing the background field in the styles.
Because what I just wrote is pretty incoherent, here's something I hacked together to give you an idea:
GUIStyle inActiveButton;
GUIStyle activeButton;
GUIStyle buttonStyle;
bool[] boolArray = {false,false};
void OnGUI(){
inActiveButton = new GUIStyle(GUI.skin.button);
activeButton = new GUIStyle(GUI.skin.button);
buttonStyle = new GUIStyle(GUI.skin.button);
GUI.skin.button.margin = new RectOffset(0,0,0,0);
GUILayout.BeginHorizontal();
for(int i=0;i<2;i++){
if(boolArray[i] == true){
activeButton.normal = GUI.skin.button.active;
buttonStyle = activeButton;
}
else
buttonStyle = inActiveButton;
if(GUILayout.Button(i.ToString (),buttonStyle)){
boolArray[i] = !boolArray[i];
}
}
GUILayout.EndHorizontal();
}
Custom button styles then, eh? Let me play with these ideas, and try to replicate EditorStyles.$$anonymous$$iButtonLeft
and so on to group the buttons. The hard part will be to make it look native with normal and pro skins.