Default style for GUI

Simple question, is there a way to set the default Unity GUI style to a GUIStyle object.

This below:

public Texture2D background;
GUIStyle style = new GUIStyle();
void Start(){
   if(background != null){
       style.normal.background = background;
   }
}    
void OnGUI(){
    if(background != null){
        if(GUI.Button (new Rect(200,200,100,100),"Button",style)){}
    }
    else if (background == null){
        if(GUI.Button (new Rect(200,200,100,100),"Button")){}
    }
}

In this case for the same button I need two versions if the user has passed a texture or not. If I get dozens of buttons then it is all doubled. If I simply do:

public Texture2D background;
GUIStyle style = new GUIStyle();
void Start(){
   if(background != null){
       style.normal.background = background;
   }
}    
void OnGUI(){
    if(GUI.Button (new Rect(200,200,100,100),"Button",style)){ }
}

And nothing is passed the button displays no default button shape. Is there a way to pass default values to the GUI style if none is provided by the user or am I doing wrong?

Cheers

What you want is something like

GUIStyle style = new GUIStyle("button");
// or
GUIStyle style = new GUIStyle(GUI.skin.button);

Also keep in mind you should work with GUIStyles only inside OnGUI.

public Texture2D background;
private GUIStyle style = null;
private void InitStyle()
{
if (style != null) return;
style = new GUIStyle(GUI.skin.button);
if (background != null) style.normal.background = background;
}
void OnGUI()
{
InitStyle();
if (GUI.Button (new Rect(200,200,100,100),“Button”,style)) {}
}

// if background can become null for whatever reason during runtime
// then you might want to do it this way
private void InitStyle()
{
	if (style != null) return;
	style = new GUIStyle(GUI.skin.button);
}
void OnGUI()
{
	InitStyle();
	if (background != null) style.normal.background = background;
	else style.normal.background = GUI.skin.button.normal; // reset to normal button back
	if (GUI.Button(new Rect(200, 200, 100, 100), "Button", style)) { }
}

[edit] You might also want to change hover, active, focused and onNormal, onHover, onActive, and onFocused