- Home /
Can't change button GUIStyle background.
Unity version: 2019.4.1f1
I'm trying to change background of button in my EditorWindow. Trying to achieve effect pressed button in normal state.
void OnGUI() {
buttonStyleNormal = new GUIStyle(GUI.skin.button);
buttonStyleToggled = new GUIStyle(GUI.skin.button);
buttonStyleToggled.active.textColor = Color.blue;
buttonStyleToggled.normal = buttonStyleToggled.active;
...
GUILayout.Button("Developing", buttonStyleToggled);
GUILayout.Button("Compilation", buttonStyleNormal);
}
Result is here: Problem stays even if I set background explicitly:
buttonStyleToggled.normal.background = buttonStyleToggled.active.background;
buttonStyleToggled.normal.scaledBackgrounds = buttonStyleToggled.active.scaledBackgrounds;
This might not solve your problem but it seems ok code to me.. and I also tested it with inspector and editorwindow and it worked ok (didn't need the explicit setting) .. I do have different Unity version (2018.4).. hope you get it working
Answer by frankslater · Jul 13, 2020 at 10:35 AM
Hey @sheld,
When I saw your question, I recalled talks about a new GUI on its way, and we could actually see the look of the editor change in recent versions, so I looked into it a little.
Here is what I found:
The problem
My guess is that this is down to the new GUI: UIElements.
Specifically how IMGUI is converted to UIElements for built-in Editor GUI.skin
.
You may have noticed that backgrounds are null:
GUI.skin.button.normal.background == null
GUI.skin.button.active.background == null
So when you do buttonStyleToggled.normal.background = buttonStyleToggled.active.background;
, it actually does nothing.
If you set the background to a Texture2D (that's not null), that gets applied.
I also found that there is a UIElements Debugger at Window > Analysis > UIElements Debugger
. Looking at the UIElements Debugger, it looks like IMGUI is displayed through UIElements within an IMGUIContainer
, and my guess is that all built-in Editor GUI.skin have UIElements style sheets (USS) and elements they are automatically converted to.
For example, if you do this
GUIStyle btn = new GUIStyle("Button");
btn.name = "customButton"; // from "button"
btn = new GUIStyle(btn);
your button background will be gone, so I'm guessing the name is used as the style class or something similar. So it's important to note, that changing name of styles where you want to use builtin backgrounds will mess up your and possibly other things.
Solutions
1. Use UIElements instead of IMGUI. If you are lucky, your project only needs to work in Unity versions that already supports it (I think 2019.1+), so you can leverage its full potential.
If your project needs to support older (IMGUI only) Unity versions, you can use preprocessor directives e.g.
#if UNITY_2019_1_OR_NEWER
// UIElements GUI code
#else
// IMGUI code
#endif
but in this case you may be more limited in what you can do with UIElements if you want your users to not get confused when switching between the two. For this reason, I would recommend the two GUI to be fairly similar (and that's what limits you to what IMGUI can do).
Here is a short UIElements Editor GUI example: Customize the Unity Editor with UIElements
2. Create standard Toggle buttons. So for example, updating your code to this might yield better results
void OnGUI() {
buttonStyle = new GUIStyle(GUI.skin.button);
// Change buttonStyle in accordance with any custom look you would like
// your buttons to have
toggleButtonStyle = new GUIStyle(buttonStyle);
toggleButtonStyle.active.textColor = Color.blue;
...
toggleOn = GUILayout.Toggle(toggleOn, "Toggle Button", toggleButtonStyle);
GUILayout.Button("Normal Button", buttonStyleNormal);
}
Your answer
Follow this Question
Related Questions
Why is my customSkin null? 0 Answers
Problems controlling the GUI skin / style 2 Answers
GUI Button + GUISkin = Bug, Help? 1 Answer