Lab Members
Welcome to Unity Labs.

Username


Password






Game Start Screen

Published on the 2nd November 2009 at 2:56pm

In this tutorial we will learn about user interfaces in Unity by creating a working game start screen. We recommend you have a basic knowledge of the Unity interface, creating scenes and have at least looked at scripting in Unity before undertaking this tutorial. However this tutorial can be undertaken by anyone.

Below is a screenshot of what we will be creating:

Screen With Buttons



Setting Up

Before we create our Game Start Screen we need to create a new project and set a few things up. So to begin, follow these steps:

  • Create a new project called "Game Start Screen Tutorial", do not include any asset packages.
  • Create the following folders in the Project Library: "Graphics", "Scenes", "Scripts", "Audio".
  • Create two scenes called "GameStart" and "Level_1" and put them in the Scenes folder.
  • Download our background image here.
  • Extract the archive somewhere.
  • Import the Background.jpg asset from the zip file and put it into the "Graphics" folder.

With those steps completed we are ready to begin, so click here to continue on to step 2 where we will create the game screen background.

Comments: 12
1   2   3   4   5  


Latest Comments
dracula
Richard

why do you need these lines of code in the start screen tutorial ?

var mainmenuSkin : GUISkin;

then latter:

var mainmenuSkin : GUISkin;

Having added the GUI texture from the GameObject menu havn't you effectively done this ?

Thanks

FizixRichard
Adding:

var mainmenuSkin : GUISkin;

At the start of the script sets the variable in the objects properties, so you can apply your skin to the variable.

The second declaration, which is:

GUI.skin = mainmenuSkin;

Sets the GUI Skin to whatever skin you defined in the variable... in short it allows you to apply the GUI Skin from the parent objects properties.

If you remove/comment out the line "var mainmenuSkin : GUISkin;" in the script and select the game object, you will see the variable where you set the GUI skin will have gone.

The GUI Texture added in the first step simply sets the background image, the GUI Skin is for formatting the buttons.

dracula
I was getting confused between GUI Texture and GUI Skin, it all makes sense now

Thanks

FizixRichard
Yeah, I was creating the background image (texture) independantly of the UI elements.

melbabez7
I am struggling with the code, i have wrote it as:

// Skin
var mainmenuSkin : GUISkin;

// GUI Area Width
var areaWidth : float;

// GUI Area Height
var areaHeight : float;
function OnGUI()
{
GUI.skin = mainmenuSkin;
}
var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
var ScreenY = ((Screen.height) - (areaHeight * 0.5));
GUILayout.BeginArea (Rect (ScreenX, ScreenY, areaWidth, areaHeight));
if(GUILayout.Button ("Play"))
{
Application.LoadLevel("Level_1");
}
if(GUILayout.Button ("Quit"))
{
Application.Quit();
} GUILayout.EndArea();


But get the error message:
ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI ()
UnityEngine.GUILayout.BeginArea (Rect screenRect, UnityEngine.GUIContent content, UnityEngine.GUIStyle style)
UnityEngine.GUILayout.BeginArea (Rect screenRect)
MainMenuUI.Main () (at Assets/2Scripts/MainMenuUI.js:15)

Any help would be greatly appreciated :)

FizixRichard
Easy one, everything you have after the OnGUI function wants to be inside the OnGUI function.

So everything from var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5)); wants to go inside the OnGUI function after "GUI.skin = mainmenuSkin; "

melbabez7
Thankyou ive got it working now, ive made the buttons larger but how do i make the text larger? thanks for the help

FizixRichard
There should be a text size and font option in the style settings; with size you play with the button padding and text size.

nebibonek
I have done everything correct as far as I can tell.

Here is the code I have attached to the Menu game object:

[CODE]// skin
var mainMenuSkin : GUISkin;

// GUI area width
var areaWidth : float;

// GUI area height
var areaHeight : float;

function OnGui ()
{
GUI.skin = mainMenuSkin;
var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
var ScreenY = ((Screen.height) - (areaHeight * 0.5));

// invisible container for the buttons
GUILayout.BeginArea (Rect(ScreenX, ScreenY, areaWidth, areaHeight));

// play button
if(GUILayout.Button("Play"))
{
Application.LoadLevel("Level_1");
}

// quit buttons
if(GUILayout.Button ("Quit"))
{
Application.Quit();
}

GUILayout.EndArea();

}[/CODE]

But when I run the game the buttons don't appear.

Any ideas?
Ben

nebibonek
So I copied and pasted your lines (as opposed to typing them manually) and even though I can't find any significant differences, your lines work and mine don't:

Yours:
[CODE]// Skin
var mainmenuSkin : GUISkin;

// GUI Area Width
var areaWidth : float;

// GUI Area Height
var areaHeight : float;

function OnGUI()
{
GUI.skin = mainmenuSkin;
var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
var ScreenY = ((Screen.height) - (areaHeight * 0.5));
GUILayout.BeginArea (Rect (ScreenX, ScreenY, areaWidth, areaHeight));
if(GUILayout.Button ("+1 Score"))
{
scores.score += 1;
print (scores.score);
// Application.LoadLevel("Level_1");
}
if(GUILayout.Button ("Quit"))
{
Application.Quit();
}
GUILayout.EndArea();
}[/CODE]

Mine:
[CODE]// skin
var mainMenuSkin : GUISkin;

// GUI area width
var areaWidth : float;

// GUI area height
var areaHeight : float;

function OnGui ()
{
GUI.skin = mainMenuSkin;
var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
var ScreenY = ((Screen.height) - (areaHeight * 0.5));

// invisible container for the buttons
GUILayout.BeginArea (Rect (ScreenX, ScreenY, areaWidth, areaHeight));

// play button
if(GUILayout.Button("Play"))
{
Application.LoadLevel("Level_1");
}

// quit buttons
if(GUILayout.Button ("Quit"))
{
Application.Quit();
}

GUILayout.EndArea();

}[/CODE]

Being a complete noob to scripting I figured your experienced eyes might pick up something mine do not.

So at least it's working however I have a new problem.

I have a counter on the same menu screen that displays a static variable called "score" in the script named "scores" attached to an empty game object.

[CODE]function Update () {

var prefix = "";
guiText.text = prefix + scores.score;

}[/CODE]

the Play button was altered to read:
[CODE]if(GUILayout.Button ("+1 Score"))
{
scores.score += 1;
print (scores.score);
// Application.LoadLevel("Level_1");
} [/CODE]

and it works fine... BUT the background is rendered in FRONT of the counter so you can't see it. If I hide the background, there it is, and I'm also printing to the debug log so even when you can't see the counter, I know it's still working.

Any ideas.
B

sobhan1991
Hi My Dear Friend I Add The Codes Below and I Got An Error!!!

// Skin
var mainmenuSkin : GUISkin;

// GUI Area Width
var areaWidth : float;

// GUI Area Height
var areaHeight : float;
function OnGUI()
{
GUI.skin = mainmenuSkin;
}
GUILayout.BeginArea (Rect (ScreenX, ScreenY, areaWidth, areaHeight));

var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
var ScreenY = ((Screen.height) - (areaHeight * 0.5));




if(GUILayout.Button ("Play"))
{
Application.LoadLevel("Level_1");
}
if(GUILayout.Button ("Quit"))
{
Application.Quit();
} GUILayout.EndArea();


THE ERROR IS:

ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI () (at C:\builds\unity-trunk\unity\Runtime\Export\Generated\GUIUtility.cs:378)
UnityEngine.GUILayout.BeginArea (Rect screenRect, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:\builds\unity-trunk\unity\Runtime\Export\Generated\GUILayout.cs:166)
UnityEngine.GUILayout.BeginArea (Rect screenRect) (at C:\builds\unity-trunk\unity\Runtime\Export\Generated\GUILayout.cs:158)
MainMenuUI.Main () (at Assets\MainMenuUI.js:13)

What Should I do? PLease Explain completely What am i suppose to do.....

thankx

derf649
[QUOTE=sobhan1991;243]Hi My Dear Friend I Add The Codes Below and I Got An Error!!!

// Skin
var mainmenuSkin : GUISkin;

// GUI Area Width
var areaWidth : float;

// GUI Area Height
var areaHeight : float;
function OnGUI()
{
GUI.skin = mainmenuSkin;
}
GUILayout.BeginArea (Rect (ScreenX, ScreenY, areaWidth, areaHeight));

var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
var ScreenY = ((Screen.height) - (areaHeight * 0.5));




if(GUILayout.Button ("Play"))
{
Application.LoadLevel("Level_1");
}
if(GUILayout.Button ("Quit"))
{
Application.Quit();
} GUILayout.EndArea();


THE ERROR IS:

ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI () (at C:\builds\unity-trunk\unity\Runtime\Export\Generated\GUIUtility.cs:378)
UnityEngine.GUILayout.BeginArea (Rect screenRect, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:\builds\unity-trunk\unity\Runtime\Export\Generated\GUILayout.cs:166)
UnityEngine.GUILayout.BeginArea (Rect screenRect) (at C:\builds\unity-trunk\unity\Runtime\Export\Generated\GUILayout.cs:158)
MainMenuUI.Main () (at Assets\MainMenuUI.js:13)

What Should I do? PLease Explain completely What am i suppose to do.....

thankx[/QUOTE]

I'll clean up what you have a little, but will not delete anything from the code.

[code]


[COLOR="Lime"]// Skin[/COLOR]
[COLOR="blue"]var mainmenuSkin : GUISkin;[/COLOR]

[COLOR="lime"]// GUI Area Width[/COLOR]
[COLOR="blue"]var areaWidth : float;[/COLOR]

[COLOR="lime"]// GUI Area Height[/COLOR]
[COLOR="Blue"]var areaHeight : float;

function OnGUI()
{
GUI.skin = mainmenuSkin; [/COLOR]
[COLOR="red"]//}[/COLOR]

[COLOR="blue"]var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
var ScreenY = ((Screen.height) - (areaHeight * 0.5));

GUILayout.BeginArea (Rect (ScreenX, ScreenY, areaWidth, areaHeight)); [/COLOR]

[COLOR="Red"]//var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
//var ScreenY = ((Screen.height) - (areaHeight * 0.5));[/COLOR]
[COLOR="blue"]if(GUILayout.Button ("Play"))
{
Application.LoadLevel("Level_1");
}

if(GUILayout.Button ("Quit"))
{
Application.Quit();
}

GUILayout.EndArea();

}[/COLOR]
[/code]

I have moved the variables to where they need to be and left them in red where you had them. I also commented out the closing of your function early on and put it in red. It now should work without errors. Hope this helps.

View All Comments And Create Comment