The first thing you want to do is create a new scene. Then create a new javascript script(I usually call mine main menu) and attach it to the camera of the scene.
it will start out with just your basic functions. like so:
function Start () {
}
function Update () {
}
You will then want to add a new function called OnGUI()
On gui is where you draw all of your user interface. it is called repeatedly to track user button presses etc;
in OnGUI() we will put
if(GUI.Button(Rect(Screen.width * .25, Screen.height * .2, Screen.width * .5 , Screen.height *.2 ),"Play"))
{
}
this should put a button that covers half the screen and has the text "Play" in it.
inside the buttons braces we will place this bit of code
Application.LoadLevel ("myLevel");
it should now read
if(GUI.Button(Rect(Screen.width * .25, Screen.height * .2, Screen.width * .5 , Screen.height *.2 ),"Play"))
{
Application.LoadLevel ("myLevel");
}
if you play the game now the level will NOT load because you have to add it to the build settings for it to read. go to file>build settings and add the main menu scene as well as "my level" scene which should contain your first level or next menu.this should work fine.. but if you want to also add a loading texture that will be drawn over the scene to tell the user to wait for the level to load you will need a loading texture like so:
we add the loading texture to the project and to the MainMenu.js we add a new public variable called loadTex;
public var loadTex: Texture;
(This should go at the top just for form sake)
now in unity drag your loading texture to the public variable in the main menu script.
now we need to know when to draw the texture so in OnGUI() we write this line of code after our button
if(Application.isLoadingLevel)
{
GUI.DrawTexture(Rect(Screen.width/4,Screen.height/4,Screen.width/2,Screen.height/2),loadTex, ScaleMode.StretchToFill, true, 10.0f);
}
this will draw the loading texture over your screen while the application is loading the next level... if you want to add a loading function on other scenes all you need to do is attach this same bit of code to something in each scene.
here is the completed code... have fun.
public var loadTex: Texture;
function Start () {
}
function Update () {
}
function OnGUI(){
if(GUI.Button(Rect(Screen.width * .25, Screen.height * .2, Screen.width * .5 , Screen.height *.2 ),"Play"))
{
Application.LoadLevel ("myLevel");
}
if(Application.isLoadingLevel)
{
GUI.DrawTexture(Rect(Screen.width/4,Screen.height/4,Screen.width/2,Screen.height/2),loadTex, ScaleMode.StretchToFill, true, 10.0f);
}
}

hi. would you like to help me make my game in unity? in return i can do some of your animation or help teach you to animate better.
ReplyDeleteI honestly got a job doing this stuff and I'm neck deep in it right now so I don't really have the time. Thanks for stopping by though. =)
Delete