Why does using OnGUI() in another script than main, not use its Skin/Style?

Good Morning,

I’m struggling with using OnGui() in any other script, than my main script. In particular I can’t make it so, that the second script will use the same style/skin, the main script is using. So for example, when I use GUILayout.Box() in the main script, it will utilize a semi transparent background, while in the second script, the same code applying the same box, will have a solid black background.

I tried several approaches, from building custom skins/styles, having applied dozens of double checks etc. but nothing seems to use the default UI setup.

Also, if you did not yet utilize any custom skins, where exactly is the default UI template coded for changes? I can’t even find the GFX elements it’s using.

OnGUI() is on its way out. You can keep using it, but its time has passed.

That said, it still works fine within its limitations. It’s not very performant, and all the calls don’t know anything about each other, except via the .depth argument.

Personally I like to use OnGUI() because it saves time for quick demo purposes, not having to set up a Canvas, lay stuff out, handle anchors, etc.

When I use OnGUI() element methods, I always insert the extra argument to supply a style, and the I have a static class to produce those styles, a class I usually call OurStyles.

OurStyles pulls its styles out of a GUISkin object on disk in Resources and sizes it accordingly.

Instead of:

GUI.Label( rect, "Hello");

you would use:

GUI.Labe( rect, "Hello", OurStyles.LABELCJ(12));

You are welcome to take my example and go nuts with it. Variations are located in most of my public projects such as this one:

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

1 Like

Even though the GUI.skin property is static, it’s not a persistant setting. The IMGUI system is used for the whole Unity editor and when you set a GUISkin during an OnGUI call it will only be set for this call of OnGUI. So it has to be set every execution and of course every script with an OnGUI method is on it’s own.

So if you want to use a certain GUISkin in all of your IMGUI scripts you have to either pass a reference to your skin from the outside which you can set at the start of your OnGUI method, or use a central access point like a singleton to grab your skin.

Here’s my IMGUI crash course just for reference.

1 Like

It’s not that I can’t figure how to use my custom setup, and were to pull it from. I fail to see where the default UI setup is pulled from. There’s no resources anywhere in my install, unless they’re packed inside any of the .bin librarys.

That’s what I did. I referenced the main script in my second script, and did the GUI.skin call on the main script, like:

GUI.skin = main.myGUIskin;

That’s where the trouble started. Doing more debugging, I think it’s related to layer hierarchy.

However, at one point I lost my nerve, and just kept all the OnGUI() logic in my main script, calling functions for the second script from there. It would just have been a little bit more convenient the other way.

Lol. Yeah, at some point you have to cut your losses and get on with real valuable work that the user cares about.