Академический Документы
Профессиональный Документы
Культура Документы
3
In this final part of our three part tutorial series,
you’ll learn how to integrate Unity UI into a
working game.
Update 20th December 2016: This tutorial was updated to Unity 5.5
by Brian Moakley. Original post by Kirill Muzykov.
The third and final part of this series all about Unity’s UI.
Part 2 was all about animating buttons and actions to make your game
uber-interactive.
Now you’re going to build on these skills to round out your understanding
of Unity’s UI, but you’re not going to stop with a fancy new menu. You’ll
also migrate the RocketMouse game scene from the old legacy GUI system
to the new UI system!
Getting Started
Start off by opening the Unity project at the point where you left it at the
end of Part 2. If you tinkered with your old project file to a point beyond
recognition — or skipped the last part — you can download the starter
project for Part 3 here: Rocket Mouse part 3 Starter Project. Unpack it and
open it in Unity.
You should already know how to add a button after working through the
first two parts of this series, but if not, the following directions should be
ample for you to accomplish the task.
Select GameObject \ UI \ Button in the menu. Rename the newly
added button to Btn_Slide and remove the nested Text object, since the
button won’t need a label.
Select Btn_Slide in the Hierarchy and open the Menu folder in the
Project window. Drag the btn_9slice_normal image to the Source
Image field in the Inspector.
To create this control, you’re going to need two panels. One will define the
mask and the other will move within the mask.
Note: If you’re not entirely sure what a mask is, don’t sweat it. Just follow
the steps and you’ll see how it works in real-time. You’ll need to have both
panels to see it in action.
Select GameObject \ UI \ Panel to create the first panel that will be the
mask. This will add a Panel to the Hierarchy. Select it and follow these
steps:
1. Rename it to Pnl_Mask.
2. Drag it over Btn_Slide to add it as a child object.
3. Set Anchors to top-center.
4. Set Pivot to (0.5, 0)
5. Set both Pos X and Pos Y to 0.
6. Set Width to 64 and Height to 192.
7. Add the mask component by clicking Add Component button
and selecting UI \ Mask.
8. Uncheck Show Mask Graphic inside the mask component dialog.
Note: You don’t always have to add the panel with a mask as a child node
of the button. But when you do, you ensure that when the button moves,
the masking panel moves with it.
1. Rename it to Pnl_Content
2. Add it as a child of Pnl_Mask
Note: Did you notice that you can see only a small portion of the
white panel, although its size didn’t change? After adding it as a child
of the panel with a mask, you now only see the portion of pnl_content
that is inside the rect of pnl_mask.
Now it’s time to change the background image for the content panel.
Open the Menu folder in the Project window and select the
slide_menu_panel_9slice image. Open Sprite Editor in the
Inspector and set all Border values to 8. Click Apply!
On the following GIF, you can see both how the content panel should look
and how the mask component works. Now you see it, now you don’t
Note: As you can see, a mask works similarly to a window in a wall. If
someone is walking along a wall, you can only see him when he passes by a
window. Another way to think of it is as a cloaking device that allows only a
portion of the image to show through.
Adding Buttons
Set Anchors to top-center and Pivot to (0.5, 1). After that, set both
Pos X to 0 and Pos Y to 0.
Now it’s your turn to add two remaining buttons, all by yourself.
To make the panel slide up and down, you’re going to use the same
technique you’ve already employed for buttons and the settings dialog.
4. Click on the 1:00 mark in the timeline. This should also enable
recording in the Animation view. Turn it on by pressing the red circle
button, and then look for the playback controls to turn red.
5. Set the Top to 192 in the Inspector and then stop recording.
14. In the Inspector, name the state idle. Next, right click the state and
choose Set as Layer Default State. Create a transition between
idle to sliding_menu_up. Set the Condition as isHidden is equal to
false.
15. Select Pnl_Content in the Hierarchy and open the Animation View.
Create a new animation clip and call it idle.
16. In the first keyframe, set the Top to be 192. Then Stop the recording.
That’s it, 16 easy steps! :] Select Save Scenes to save your work so far.
Unfortunately, when you run your game, nothing happens.
Now it’s time to make things move and you’ll do this in code. Open the
UIManagerScript in a code editor and add following instance variable:
This enables the animator component when you open the sliding menu and
sets the correct isHidden parameter value.
Save the script and switch back to Unity. In Unity, select UIManager in
the Hierarchy and drag Pnl_Content from the Hierarchy to the Content
Panel field in the Inspector.
After that, drag UIManager from the Hierarchy to that new handler.
Then, in the function selection dropdown, select UIManagerScript \
ToggleMenu ().
Select Save Scenes to save your work, run the scene and relish in your
cool sliding-up-and-down menu.
To complete the sliding menu control, you need to trigger the gear
animation from code, but you only need to write a few lines.
Then scroll down and find the ToggleMenu method. Add the following to
the bottom of the method’s body:
gearImage.SetBool("isHidden", !isHidden);
}
This enables the Animator component and sets its isHidden parameter to
the same value as the content panel’s Animator isHidden parameter.
Good job! The sliding menu is complete and your scene is coming together.
You’re not going to handle clicks on the buttons in the menu, because you
should be already familiar with handling UI events and actually integrating
Game Center would send this tutorial down a rabbit hole. Instead, you’re
going to update the old GUI-based RocketMouse scene so that it uses the
new GUI system.
Switch to the RocketMouse scene and open the Scenes folder in the
Project window. Double-click on the RocketMouse scene to open it.
Select Text in the Hierarchy and make following changes in the Inspector:
1. Rename it to Txt_Points.
2. Set Anchors to top-left.
3. Set Pivot to (0, 0.5).
4. Set Pos X to 50 and Pos Y to -30.
5. Change Text to 0, since the player starts with zero points.
6. Open the Fonts folder in the Project window and drag TitanOne-
Regular to the Font field in the Inspector.
7. Set Font Size to 24.
8. Set Horizontal Overflow to Overflow to make sure the label can
display even the most outrageous scores.
Also, don’t forget to change the color of the text to be white.
Nowadays, simply displaying text to show the points is not enough. You
need to make sure that it’s perfectly clear what this text means from the
moment the player’s eyes see it.
Yes, players these days are spoiled by impressive UI on even the simplest
apps, so you need to add an icon to make the score perfectly crisp, clear
and well-defined.
1. Rename it to Img_Points
2. Drag it over Txt_Points to add it as a child, so that when you move
the label the icon moves, too.
3. Set Anchors to middle-left.
4. Set Pivot to (1, 0.5).
5. Set both Width and Height to 32.
6. Set Pos X to -5 and Pos Y to 0.
7. Open the Sprites folder in the Project window and drag the coin
image to the Source Image field in the Inspector.
Note: This time you do not click Set Native Size, because you’re going to
reuse the image for coins in the game, which will be a bit bigger than the
icon.
Note: Normally, I’d break this huge script into several smaller chunks, but
I don’t want you to waste your time on housekeeping, especially because
refactoring will take more time and will require a strong understanding of
existing code.
It’s better to work with it in a big ol’ block so you can just make the small
changes needed and move on with your life.
Open the Scripts folder in the Project window and double-click on the
MouseController script to open it in a code editor.
When the script loads, find and remove following methods, which use
the old GUI system:
onGUI
DisplayCoinsCount
DisplayRestartButton
After that, add the following instance variable to contain a reference to the
label:
Finally, add the following line at the end of CollectCoin(), which is called
every time the mouse collects a coin.
coinsLabel.text = coins.ToString();
Run the scene and send the mouse out to collect a few coins. You should
see the label update when he collects a coin.
Everything is looking good, but you might have noticed one rather
embarrassing problem. When you removed the old onGUI method you also
removed the button that displayed when the mouse dies, leaving the player
unable to restart the game. Doh!
Gotcha? :] Of course I’m not going to leave you hanging. If you’d like a
step-by-step, just open up the spoiler below.
You’re not going to animate the appearance of the dialog. Instead, you’ll
just hide the dialog at the start and show it when the player loses the game.
Open the MouseController script in a code editor and add the following
instance variable:
Then add following line of code toStart() to hide the dialog at the start:
restartDialog.SetActive(false);
restartDialog.SetActive(true);
Click + to add a new item. After that, drag Mouse from the Hierarchy to
the new item. In the function selection dropdown, select
MouseController \ RestartGame ().
Now, select Btn_Exit, and repeat the process, but this time select the
MouseController \ ExitToMenu () function.
Save the scene to save your work then run the scene and send your
mouse into the laser’s line of fire. You should see a dialog appear instantly
after he dies. If you press Restart, you’ll restart the game. If you press
Exit you’ll return to the main menu.
And once again I’m ending a tutorial on a high note — with an image of the
poor, lifeless mouse. Sorry, no kittens today, maybe in the next series. :]
I hope you like the UI system and are excited to create some cool user
interfaces in your games! Surely, the prospect of minimal coding alone
should have you feeling a bit gleeful.
If you create some awesome control using the UI system, we’d all love to
see your handiwork. Go ahead and post a screenshot, video or GIF
animation in comments to show the world your awesome new skills — and
maybe inspire a fellow dev or few (or make them jealous). :]
If you have any questions or comments, please leave them below. I will be
happy to answer!
Good luck!