- Home /
Global GUISkin?
I have quite a big project with many tests and experiments in over 10 different scenes. I wanted to change the GUI Skin so it would all look a bit better.
Does anyone know of a simple way to globally change the GUISkin?
I did it the hard way now, went into every scene, looked for the GUI-Script and included a public GUISkin-Variable which I then set in the Inspector. This took some time since I often have several GUI-Scripts for the same scene (its all a bit chaotic, but its only for testing after all...)
Answer by Molix · Aug 05, 2011 at 02:12 PM
You may want to look into using a 'singleton', or something similar. For example:
public class MyGuiManager : MonoBehaviour
{
public GUISkin guiSkin;
private static MyGuiManager instance;
void Awake()
{
instance = this;
}
public static GUISkin GetSkin()
{
return instance.guiSkin;
}
}
As long as you have one game object in your scene with this script, then all your other OnGUI() functions can just call:
void OnGUI()
{
GUI.skin = MyGuiManager.GetSkin();
// ...
}
If the game object containing MyGuiManager is in a prefab, then changing the skin for everything only has to happen there.
Yep, that's the way to go, but you should use DontDestroyOnLoad or you get in trouble after a LoadLevel. Another way would be to copy the guiSkin reference into a static variable so even when the script instance gets deleted the reference is still there.
I agree that having one object that lives throughout all LoadLevels is probably better, since you can keep other semi-permanent info too (e.g. window positions), but this way still works (i.e. you can let it get destroyed with the LoadLevel, as long as you have one in the level being loaded, too, since it will be Awake before any OnGUI), and I thought it would be easier to explain without having to worry about ensuring there is only one copy, or loading it manually from Resource, etc.
Thanks for the tip, I'll do it like that. It would still be great if you could change the Default GUISkin in the Project Settings for example...