G.U.I. & Menus / Scripting the 3D Main Menu
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Continuing on with our 3D manual. We now need to add some script that will actually trigger the color changes when we hover our mouse over it. As well as, as soon as we click it, that it will load the other scenes. Now, understandably, the Main Menu is the first scene in the game that will actually be displayed so we need to make sure that the other scenes are accessible in scripts, also. To do this, we have to go to our Build Menu, go to File and then Build Settings, and as you can see, it gives us a little dialogue that wants to know which scenes we want to include in our Build. Now, you can imagine that if you're a game designer or developer, that you have a lot of scenes for testing purposes that you don't want to use and a couple of scenes that you do want to use. So, to load them in here, simply drag them in. I'm going to put the Main Menu first and then the Level second. In this way you can see it becomes zero and one and we'll use these numbers in scripting to refer to the right level. Now, of course, if you wanna change the order you can drag them up and down, but we want the Level to be the second one and the Main Menu to be the first one. Now, there's no need to build it yet because this will simply be remembered and now the scenes become accessible in Scripts. This is exactly what we'll do next. Right click our Script folder and Create another JavaScript and let's call this one, Item or MenuItem. Now, we will attach one MenuItem script to both of these so that we don't have to retype it for every single one of them. So let's open it up. Get rid of the Update function because in this case we don't want to do things every frame, we want to do things based on mouse behavior. So, let's start with typing OnMouseEnter and this script will be triggered or it's function will be triggered once the mouse enters over our text. Now, this only works when we have a box collider or any other type of collider on our text. It will not work if it's not there and second, it will also not work if there's geometry in place. So, if, for example, your Menu here, is behind the wall, it will still be visible in Unity, but it won't be clickable. It will always be rendered because it's a 3D text so it basically decides it wants to be there, but, it won't be clickable. So, make sure the text is outside of the actual wall. OK, I notice also that I have rotated them a little sloppy so let's change that a little bit to be more aligned with the wall, same with our Quit, and there that works just fine. Alright, back into our script. Let's change the color of our renderer because they have a Mesh Renderer attached to them. So, type renderer.material.color equals Color.red. Now, notice that I'm using a capital C for the the second one because here we are referencing color as a class and here it's only part of a variable, this name space. Alright, so, there's that. Now, let's Copy and Paste these and type OnMouseExit to make sure we also reset our color back to white. Now, let's test our game here and it is not working yet, because we have not attached our script to our objects. So, let's do that right now. Drop them right on top of them, play and now as you can see, we have a Main Menu with colors. Pretty nice? I think it looks pretty good. OK, now, next what we need to do is basically trigger what happens when we click them. So, let's add a variable and say, isQuitBtn and set it to false by default, save it and only for the quit one we will check it to be a quit button. For the other one, we will not. OK. Let's go back into our MenuItem. Let's add another function that will register our click which would be OnMouseUp. You could, of course, also use OnMouseDown but mouse up is better for the feedback for the actual user and then we will here write the script that we need to quit the level if quit was selected, or to load the next level. So, let's do an if statement, if isQuitBtn. So if this one is set to true, we type Application with a capital A .Quit with a capital Q and this will quit our application or else, if it's not a Quit button, we will type Application.LoadLevel with two capital L's and then the name of our level, or the number which is one. Let's see if this works. Hit the Quit button. Now, of course it doesn't quit because right now, it's in the player, but as soon as we save our script or export our game, it will and then the Play Game brings us to our first level. Really nice. This works fine and that concludes building our 3D Menu. So, next, let's move on and get some action going and make the turret actually shoot at us and turn this into a real first person shooter.
Tutorial Information
| Course: | Introduction to Game Development Using Unity 3D |
| Author: | Efraim Meulenberg |
| SKU: | 34092 |
| ISBN: | 1-936334-03-8 |
| Release Date: | 2010-02-23 |
| Duration: | 5.5 hrs / 75 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 