Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Between levels and GUI we've now covered enough to be able to make a start menu. So I've gone ahead and made a simple one for us to use. Let's import the StartMenu package, right-click > Import Package > Custom Package and 13-07 > Assets, and we're going to select StartScreen, click Open and Import. This one adds a new scene directly to our scenes folder.
Let's go ahead and double- click on it to load it. And we're looking for StartMenu, and we can save the scene. As you've probably guessed, the start screen exists only in scripting. Let's go ahead and select the main camera where our script resides and then open our StartScreen script. The main thing of interest here is the use of backslash N in the text string for instructions.
It gives us an easy way to control the line feeds without adding more controls. We're also using GUI.DrawTexture. We'll see GUI.DrawTexture in another video. With the introduction of Unity GUI, it's no longer feasible to use GUI texture objects as cursors. Unity GUI controls are always drawn on top of everything else. So to make a custom cursor, we're going to need to use the Unity GUI. We'll do that in the StartScreen script.
First we'll need to tell it what texture to use, so let's create a variable, var pointer of type texture. Give myself a little bit of room and var pointer of type Texture. Next we're going to need a start function to make sure we can turn the operating system cursor off. I'm going to get that one from the snippets file, Exercise Files13/13-07/Snippets.
And I'll just copy the entire start function here, right-click, Copy, and I'm going to put it right below the variable declarations. So that's going to turn our screen cursor off. Now we'll need to know where the operating system cursor's position is. To do that we're going to use Event.current.mousePosition. Let's test it first. Let's go ahead and get the code from Snippets again. And this is the one we want.
We're going to test it with a print statement first, right-click and Copy. And let's go ahead and add it at the bottom of the OnGUI function, Ctrl+V to Paste, let's save this script now and head back to the editor. Let's start by turning on Maximize on Play, and then we can go ahead and click Play and move the mouse around. It's position will be reported in the console. Let's turn on Maximize on Play, and also, let's change from Free Aspect to our Standalone size, 1024x768.
Now we can click Play, and here is our little splash screen. It's a little bit off because we aren't a full 1024x768 yet, but we can get the idea. Let's move our cursor around. In the bottom left you can see that the position is being reported in the console, and let's exit Play mode. So now, let's convert that position to a variable that we can get the X and Y from. I'll get that code from Snippets as well. And we're going to get these two lines, right-click and Copy, back to the Script editor, and we're going to put them below our print line and comment out our print line.
Now we can get the X and Y position using mousePos.x and mousePos.y that we get from this variable we've added here. So let's save our script and head back to the editor, and we need to find a texture for our pointer. Let's select the Textures folder, right- click and Import New Asset, and here we have GamePointerSm, and that's the one we want. Select it and Import.
It's a little different than the operating cursor system. Let's select the Main Camera now and drag our new GamePointerSm onto the pointer field, and let's click Play. Now we have a custom cursor, and let's exit Play mode. We're going to try one more thing here. Let's change the color of our cursor. To change the color of a GUI control, we need to use GUI.color.
Every control after that line will inherit the color. So if we needed to clear the color, we would use GUI.color = color.white after the colored control. Let's go back to the Script editor and add a variable for our color. So up at the top I'm going to add var cursorColor of type color, and we'll go ahead and initialize it to Color.green, and in the on GUI function above the mouse pointer code, we're going to add GUI.color = cursorColor.
Let's save our script and head back to the editor. And now we can see our new variable. It's a color swatch. Maybe green isn't going to be the best choice, so I'm going to go ahead and click on it now, and I want to change mine to kind of a reddish-orange. Let's click Play and test the new cursor. And there it is, our newly-colored cursor. Let's exit Play mode and then save the scene, so we can add it to the build.
File > Save Scene, and now we're going to use Build Settings. Add Current, and this one needs to be at the top, so we'll just drag it up and drop it, and we'll click Build. Let's just use the same file and save, and yes we want replace it. And we'll close that window, then we'll the close Build Settings window. And finally, remember the crosshair we turned off a while back? We can now replace it with a simple script with the GUI control.
Let's go ahead and get back to the main level for a minute. Double-click to open the main level, we're going to use Import New Asset to bring in the GUI's crosshair script, and we want to bring it into the game scripts folder, so I'm going to select that, right-click > Import New Asset, going to go out to my Desktop, Exercise Files13/13-07/Assets, and here's the script we want, select it and Import.
Now we can open up the first-person controller and the Main Camera and the Weapon Handler and drop our script on the Proxy Weapon. Since this script is a child of the object, it gets deactivated when the object does, so there's only one thing we need to do now, and that is select the Proxy Weapon and assign the crosshair texture. And here it is right here, and we'll drag and drop it into the crosshair field.
I'm going to turn off Maximize on Play, click Play, and I'm going to turn towards a darker area, and now when I press 0 on my keypad, our crosshair comes and goes with the Proxy Weapon, and let's exit Play mode. I'll press Escape and exit. With the addition of a start screen, we should be in good shape to play through our game.
Get unlimited access to all courses for just $25/month.Become a member
107 Video lessons · 34013 Viewers
94 Video lessons · 23751 Viewers
100 Video lessons · 4201 Viewers
83 Video lessons · 9265 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.