Introduction to Unity UI – Part 3
In the final part of our three-part tutorial series, you’ll learn how to integrate Unity UI into a working game. By Ben MacKinnon.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Introduction to Unity UI – Part 3
25 mins
- Getting Started
- Creating a Sliding Menu
- Adding an Open Button
- Adding the Masking Panel
- Adding the Content Panel
- Adding Buttons
- Making the Panel Slide Up and Down
- Adding Code to Toggle the Menu
- Adding a Rotating Gear Icon
- Adding the Gear Image
- Animating the Gear Image
- Triggering the Gear Animation from Code
- Updating the RocketMouse Scene to use Unity’s UI
- Adding the Points Label
- Adding a Points Icon
- Updating the Points Label
- Adding a Restart Dialog
- Displaying the Restart Dialog
- Where to Go From Here?
Update February 2019: This tutorial was updated to Unity 2018.3 by Ben MacKinnon. Original post by Kirill Muzykov.
Welcome to the third and final part of this series about Unity’s UI.
In Part 1, you used anchors, pivot and other cool tricks to create a menu scene complete with an adaptive background and neatly controlled graphical elements.
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. First, you’ll make a fancy new menu. Then, you’ll migrate the RocketMouse game scene from the old legacy GUI system to the new UI system!
Getting Started
Start 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, you can find the starter project by clicking the Download Materials button at the top or bottom of this tutorial. Open it in Unity.
Strap yourself in. This tutorial is about to get real.
Creating a Sliding Menu
Often, you want to provide users easy access to game options or features without wasting screen space, especially on mobile devices. This is a job for a sliding menu.
You’ve seen these before. They’re a control comprised of a small button that is always visible and a menu that slides out to reveal options. The first step is adding that button.
Adding an Open Button
You learned to add a button during the first two parts of this series, but follow these directions if you need a refresher.
Select GameObject ▸ UI ▸ Button in the menu. Rename the newly added button to SlideMenuButton and delete the nested Text object since the button won’t need a label.
Select SlideMenuButton in the Hierarchy. Open RW ▸ UI ▸ Menu in the Project window. Drag the btn_9slice_normal image to the Source Image field in the Inspector.
Set the button position and size as follows:
- Set Anchors to bottom-left.
- Set both Pos X and Pos Y to 80.
- Set Width and Height to 64.
Nice work! That’s step one.
Adding the Masking Panel
To create this control, you’ll need two panels. One will define the mask, and the other will move within the mask.
Select GameObject ▸ UI ▸ Panel to create the first panel. This will add a Panel, which will be the mask, to the Hierarchy. Select it and follow these steps:
- Rename it MaskPanel.
- Drag it over SlideMenuButton to add it as a child object.
- Set Anchors to top-center.
- Set Pivot to (X:0.5, Y:0)
- Set both Pos X and Pos Y to 0.
- Set Width to 64 and Height to 192.
- Add the mask component by clicking the Add Component button and selecting UI ▸ Mask.
- Uncheck Show Mask Graphic inside the mask component dialog.
Adding the Content Panel
Add another panel by selecting GameObject ▸ UI ▸ Panel and following these steps:
- Rename it ContentPanel.
- Add it as a child of MaskPanel.
- Set the Anchors to stretch-stretch.
- Set Left, Top, Right and Bottom to 0.
- Set Pivot to (X:0.5, Y:1)
Now, change the background image for the content panel.
Open RW ▸ UI ▸ Menu 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!
After that, select ContentPanel in the Hierarchy, and then drag slide_menu_panel_9slice from the Project window to the Source Image field in the Inspector.
In the following GIF, you can see both the content panel should look and how the mask component works. Now you see it, now you don’t!
Adding Buttons
Next, you’ll add three buttons to the sliding menu.
To create the first button, select GameObject ▸ UI ▸ Button. Rename it to AboutButton and delete the Text child.
Drag the AboutButton button onto ContentPanel in the Hierarchy to add it as a child. Open RW ▸ UI ▸ Menu in the Project window and drag slide_menu_btn_about to the Source Image in the Inspector. Click Set Native Size. Don’t worry about the position for now.
Right-click on AboutButton and select Duplicate (Ctrl/Cmd + D) to create another button. rename the duplicate to AchievementsButton and use the slide_menu_btn_achievements from RW ▸ UI ▸ Menu in the Project window as Source Image.
After that, duplicate the button again. Name it LeaderboardsButton and use slide_menu_btn_leaderboards as the source image.
Great! Now, you have three buttons, but what about positioning them? Time to use a shortcut!
Select ContentPanel in the Hierarchy. Add a Vertical Layout Group component and voilà! You should see three buttons stacked neatly in the space. :]
Here’s what it should look like: