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?
Making the Panel Slide Up and Down
To make the panel slide up and down, use the same technique you’ve already employed for buttons and the settings dialog.
Follow these steps:
- Select ContentPanel in the Hierarchy and open the Animation view.
- Create a new clip by clicking on the Create button.
- Name the animation SlidingMenuDown and save it to RW ▸ Animations.
- Click on the 1:00 mark in the timeline and 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.
- Set the Top to 192 in the Inspector and then stop recording.
- Open RW ▸ Animations in Project window and select SlidingMenuDown. Uncheck Loop Time in the Inspector.
- Select ContentPanel in the Hierarchy and open the Animator view. Copy and paste the SlidingMenuDown state to create a duplicate.
- Rename the duplicate SlidingMenuUp and set its Speed to -1 in the Inspector.
- Create two transitions: one from SlidingMenuUp to SlidingMenuDown, and one the opposite direction.
- Add a new Bool parameter named isHidden and set its default value to true.
- Select the transition from SlidingMenuUp to SlidingMenuDown, and in the list of conditions, set isHidden to true.
- Select the transition from SlidingMenuDown to SlidingMenuUp, and this time set conditions to be isHidden equals false.
- Next, right-click in the Animator, select Create State and then choose Empty.
- 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 SlidingMenuUp. Set the condition so that isHidden is equal to false.
- Select ContentPanel in the Hierarchy and open the Animation view. Create a new animation clip and call it Idle.
- In the first keyframe, set the Top to be 192. Then stop the recording.
That’s it! Save your work again. Unfortunately, when you run your game, nothing happens. You’ll make things move in code.
Adding Code to Toggle the Menu
Open the UIManagerScript in a code editor and add the following instance variable:
public Animator contentPanel;
After that, add the following method:
public void ToggleMenu()
{
bool isHidden = contentPanel.GetBool("isHidden");
contentPanel.SetBool("isHidden", !isHidden);
}
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 ContentPanel from the Hierarchy to the Content Panel field in the Inspector.
Now, select SlideMenuButton in the Hierarchy. In the Inspector, find a list of On Click () event handlers and add a new one by clicking the + button.
After that, drag UIManager from the Hierarchy to that new handler. Then, in the function selection dropdown menu, select UIManagerScript ▸ ToggleMenu ().
Save your work, run the scene and relish in your cool sliding menu.
Adding a Rotating Gear Icon
Don’t you think something is missing? Of course! The rotating gear icon on the opening button itself — the one shown in the animated GIF image at the start of this part.
Adding the Gear Image
First, add an image as a child object of SlideMenuButton, and set it to animate during the menu opening and closing animations.
Right-click on SlideMenuButton and select UI ▸ Image to create a new image as a child object.
After that, follow these steps:
- Rename the image GearImage.
- Open RW ▸ UI ▸ Menu in the Project window and drag the slide_menu_gear image to the Source Image field in the Inspector.
- Click Set Native Size.
Animating the Gear Image
By now, creating two animation states and a parameter to switch between them is probably second nature. You may be able to create a left-rotating gear and reverse the animation to make a right-rotating gear on your own.
Here are the need-to-know details:
- The Animation duration should be identical to the sliding panel animation. All animations in this tutorial are exactly one second long.
- The gear should rotate 360 degrees around the Z axis (Rotation Z).
- Use the same name isHidden for the parameter name and set its default value to true.
- Remember to disable looping and the Animator component.
If need more detailed directions, open the spoiler below.
[spoiler title=”Rotating the Gear”]
Select GearImage in the Hierarchy and open the Animation view. Create a new clip by clicking on the Create button and name it GearRotateOpen. Save it in RW ‣ Animations.
Then, click on the 1:00 mark in the timeline and enable recording. After that, in the Inspector, change Rotation Z to 360.
Stop recording by clicking the button with a red circle.
Now, open the Animations folder in the Project window and select GearRotateOpen. In the Inspector, uncheck Loop Time.
Now, set up states. Select GearImage in the Hierarchy, open the Animator view and do the following:
- Right-click the Animator and choose Create State. Then choose Empty.
- In the Inspector, name the state GearIdle.
- Right-click GearIdle and select Set as Layer Default State.
- Duplicate the GearRotateOpen state by copying and pasting it.
- Rename the duplicate to GearRotateClose and change its Speed to -1 in the Inspector.
- Add a new Bool parameter named isHidden, and set its default value to true.
- Create two transitions between states. Under conditions, set isHidden to true for the transition from GearRotateOpen to GearRotateClose, and isHidden to false for the reverse transition.
- Create a transition from GearIdle to GearRotateOpen. Set the isHidden condition to false.
[/spoiler]
Triggering the Gear Animation from Code
To complete the sliding menu control, you need to trigger the gear animation with code, but you only need to write a few lines.
Open the UIManagerScript in a code editor and add the following instance variable:
public Animator gearImage;
Then, scroll down and find ToggleMenu
. Add the following to the bottom of the method’s body:
public void ToggleMenu()
{
//..skipped..
gearImage.SetBool("isHidden", !isHidden);
}
This enables the Animator component and sets its isHidden
parameter to the same value as the contentPanel
animator’s isHidden
parameter.
Save the script file and switch back to Unity.
In Unity, select UIManager in the Hierarchy. Drag GearImage to the Gear Image field in the Inspector.
Save your work, run the scene and enjoy your fancy rotating gear icon.
Good job! The sliding menu is complete, and your scene is coming together.
For the purposes of this tutorial, you’re not going to handle clicks on the buttons in the menu. You should be familiar with handling UI events, and integrating Game Center would send this tutorial down a rabbit hole. Instead, you’ll update the old GUI-based RocketMouse scene so that it uses the new GUI system.