- Home /
GUIStyle doesn't work when renamed. What to do here?
I'm fighting with this thing all day and here's what I ended up with. I guess it may be a bug in Unity 2020.1.0f1 but I didn't test it in other versions except for 2018.1.9f2 and it works properly there.
Here are steps to reproduce this. In editor script, OnInspectorGUI() do this:
//This works just fine
GUILayout.Button(new GUIContent("Hello one"),GUI.skin.GetStyle("Button"));
//Copyting the button style
GUIStyle buttonStyle=new GUIStyle(GUI.skin.GetStyle("Button"));
//Changing its name to anything except Button, ButtonLeft, ButtonMid, ButtonRight
buttonStyle.name="MyButton";
//Same button as above but using my style with changed name
GUILayout.Button(new GUIContent("Hello two"),buttonStyle);
Here's how it renders:
What I'm trying to do is I want to create custom styles for GUILayout.Toolbar based on Button style, which seems to be really hard already. And what I came up with works in older Unity but it doesn't in 2020. So I narrowed it to this case where changing the name somehow invalidates the style.
UPD. Found another weird thing about this. If I apply buttonStyle to another button before renaming the style, it will work even after renaming. So it somehow matters if the style was used in UI before it was renamed. Maybe its name gets registered somewhere. But how can I do this without using the style to draw the buttons I don't need?
UPD2. Oh no! Another amazing development! I can't believe my eyes. I have to share this with someone. Looks like the element with non-renamed style has to be visible on screen. As soon as it's out of the view, the element with renamed style looses the style. How am I supposed to beat this? I really want to use those renamed styles. Which hoops should I jump through?
UPD3. It feels like style's name has to be registered somewhere. Like there's some cache that needs to be updated with newly added names. Because you can rename style to existing style names but can't rename to something custom.
Answer by Bunny83 · Sep 14, 2020 at 02:11 AM
Where and when do you create your custom style? GUIStyles are actually native objects (at least they have a native counter part). Do you create the styles once, or do you recreate them every time?
So make sure you create your styles once and cache them properly. Depending on what you're doing you may want to consider to create a custom GUISkin
Note that I haven't used the IMGUI system recently. However since they developed the UIElements it seems they gradually replace a lot of the old IMGUI things with optimised UIElements behind the scenes. So your scrollview might just cut off everything that went out of the view.
The source of the IMGUI system is over here. However keep in mind that the lowlevel parts are implemented on the native C++ side. Changing the name does in fact change the rawname on the native side which could cause issues on the native side. If that's really the case I would consider it as a bug. However you should first check if the issue persists when you properly create your style once and cache it.
It would help to have your actual code available to investigate this issue.
Your answer
Follow this Question
Related Questions
Setting custom amount of space between elements in a custom inspector with EditorGUILayout ? 1 Answer
Is it possible to store and display EditorGUILayout.Toggles? 0 Answers
Custom Inspector and ExposedRefreces 1 Answer
EditorGUI, EditorGUILayout, GUI, GUILayout... pshhh... WHEN TO USE WHAT?! 3 Answers