Placing texture buttons in c#

Hi all,

I’m having a bit of grief with coding a unityGUI in C#. I’ve got most things down but am struggling a bit with placing a textured button.

I have a two graphics (normal and hover) which are imported into the library.

I then have a script for the GUI, which displays the GUI and will manage actions.

The script looks like this:

using UnityEngine;
using System.Collections;


public class GSBootstrap : MonoBehaviour 
{
	public GUISkin gsmenuSkin;
	
	private float ScreenX;	 // Width of the screen
	private float ScreenY;	 // Height of the screem
	public Texture2D bgImage; // Main background image
	public Texture2D newGameN; // New Game Button Normal
	public Texture2D newGameH; // New Game Button Hover

	
	void Start () 
	{
	
	}

	
	void drawBackgroundImage()
	{
		GUILayout.BeginArea(new Rect((ScreenX / 2) - 512, (ScreenY / 2) - 384, 1024, 768), bgImage);  
		GUILayout.EndArea();
	}
	
	
	void drawNavigationArea()
	{
		GUILayout.BeginArea(new Rect((ScreenX / 2) -50, (ScreenY / 2) + 230, 100, 30));  			  
		if(GUILayout.Button("New Game"))
		{
			Application.LoadLevel("level1");
		} 
		GUILayout.EndArea(); 
	}
	
	
	void OnGUI ()
	{
		GUI.skin = gsmenuSkin;
		
		ScreenX = Screen.width;
		ScreenY = Screen.height;
		
		// Draw the main container		
		GUILayout.BeginArea(new Rect(0, 0, ScreenX, ScreenY)); 
			drawBackgroundImage();
			drawNavigationArea();
		GUILayout.EndArea();
	} 
}

The code might be a bit dodgy as I’m experimenting, but what I’m doing is placing a texture (1024x768) in the middle of the screen (the menu background image). Then I have a “new game” button which is a standard unity button.

Now if I update the button code to this:

if(GUILayout.Button(newGameN))
{			
   Application.LoadLevel("level1");
}

I get a regular unityGUI button with the image in it instead of text; which isn’t what I want to do.

What I want is for there to be a button (which is they graphic on its own) but also want to swap the newGameN with newGameH when the user hovers their mouse over it.

I have no idea what the syntax to do this would be, so could anyone help me out with that?

Thanks in advance.

You need to use GUISkin for that purpose.

And change GUIStyleState accordingly, in your case that would be GUIStyle.normal.background and GUIStyle.hover.background.

So in short:

  1. Create GUISkin in Assets
  2. Change button GUIStyle accordingly
  3. In a script where you’re drawing gui apply GUISkin
GUI.skin = myUltraGameSkin;