How to Access Unity Default Skin

Is there a way I can get Unity’s default skin?
I want what GUI.skin returns if not set, or set to null. I would just use GUI.skin, but I want to access it outside of OnGui() calls. I want to set up custom GUIStyles in Start() rather than recreating them several times every frame in OnGUI().

Here is my C# property.

public GUISkin m_skin;
public GUISkin skin {
	get {
		if (m_skin != null) {
			return m_skin; 
		} else {
			//return GUI.skin;
			return null; //TODO return the default unity skin -- where do I get that from?
		}
	}
}

So… How do I get the default skin?

Thanks!

Create a GUISkin variable that is public to the class and in the OnGUI function assign that variable.

void OnGUI()
{
if(defaultSkin == null)
defaultSkin = GUI.skin;
}

Im not sure why you want it though if you just want to create a GUISkin at start up and assign it to the gui just do that. I doubt a simple call as shown below is gonna be your performance bottleneck, if that is what you worry about.

void OnGUI()
{
   GUI.skin = mySkinInitedInStart;
}

I am providing an editor-exposed-serialized skin reference called m_skin in a GUI thing.

I want it so using the property “skin” that I provided code for above, will never return null. Then, in the case that a designer forgot to specify a skin-reference, the GUI thing will just look odd, rather than cause a crash. I know there are many many many other ways to avoid a crash here, but as I wrote this (and read what GUI.skin returns when uninitialized) I thought… “Hmmm… a default unity skin? I’d like to just return that if one isn’t specified… Oh, I need to be in an OnGUI() scope? Well gosh, there must be another way to get at that default unity skin that GUI.skin returns when one isn’t yet specified.” and then I posted this forum topic.

So… I am asking out of curiosity. It just feels like something I should be able to get at, and often these things are obtainable in a different way.

Does anyone know that way?

… I don’t really want a hack where I save what an uninitialized GUI.skin returns as a local variable. I want something that just gives me this default skin.

Thanks!

1 Like

If you dont set GUI.skin each OnGUI function will be called with Unity’s default skin. Are you trying to override the default skin?

And you probably cant call GUI.skin outside (or should) as things might not have been initialized by the engine yet.

Oh and if you read the documentation for GUI.skin you will see that simply setting it to null will revert to unity’s default skin.

to expand on what I am doing, here is some more code.

[System.Serializable]
class GUIThingWindow : MonoBehaviour {
	private GUIStyle m_specialStyle;
	public Color m_textColor; //exposed to and specified via editor
	public GUISkin m_skin; //exposed to and specified via editor
	public GUISkin skin {
		get {
			if (m_skin != null) { 
				return m_skin; 
			} else {
				return null; //TODO return the default skin	
			}	
		}	
	}
	
	private void SetupGuiStyles() { 
		m_specialStyle = new GUIStyle(skin.GetStyle("BaseCustomStyle")); //uses the property, which may be null (which is what I am trying to avoid)
		m_specialStyle.normal.textColor = m_textColor; 
	}
	
	private void Start() {
		SetupGuiStyles(); // My Preferred way to do this so I don't call "new GUIStyle()" several times a frame
	}
	
	private void OnGUI() {
		GUI.skin = m_skin;
		//I don't want to call SetupGuiStyles() here because OnGUI() is executed 
		//several times each frame, and these styles will never change 
		//after their initial set up.
		// and no... I'd rather not use a "doOnce" boolean.
	}
}

You could do something like

if(skin != null)
{
   // Call your setup code
}
else
{
   // Create a default GUISkin from code
}

I have explored the option of creating a new skin using GUISkin.Instantiate(). But I’d rather have a way to get at the default unity skin. The one it uses when you just say GUI.Button(). Where is that and how do I get at it?

Is it just plain not accessible? Is it something that is generated by unity each frame before each OnGUI() call?

Assign null to GUI.skin, then read it back :slight_smile:

You probably have to do it from OnGUI, though - so I’d just have a piece of code that goes something like this:

void OnGUI () {
    if (m_MyStyle == null) {
       Initialize ();
    }
}

GUIStyle m_MyStyle;
void Initialize () {
    GUI.skin = NULL;
    m_MyStyle = GUI.skin.GetStyle ("Button");
}