Introduction to Unity UI – Part 1
In this first part of a three-part tutorial series, you’ll get acquainted with the Unity UI, enabling you to add custom user interfaces to your games. 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 1
40 mins
- Getting Started
- Creating MenuScene
- Importing Images and Fonts
- Adding Your First UI Element
- Setting Up the Menu Background Image
- Using the Canvas Scaler
- Adding a Header Image
- Rect Transform, Anchors, Pivot and You
- Anchors
- Custom Anchors
- Splitting Anchors
- Rect Transform Depends on the Current Anchors Setting
- Pivot
- Placing a Header Image
- Adding the Start Button
- Positioning the Button
- 9-Slice Scaling
- Preparing Button Images
- Setting Button Images
- Setting a Custom Font for the Button
- Adding the Settings Button
- Starting the Game
- Adding Scenes to Build
- Creating UIManager
- Calling StartGame When the Button is Clicked
- Where to Go From Here?
Placing a Header Image
Phew! That was a lot of information about Rect Transform, Anchors and Pivot. Believe me, you’ll be grateful you spent the time working through the exercise, as these concepts are essential to creating awesome UI in your games.
Going forward, you’ll concentrate on creating the menu scene. The rest of this tutorial will go by in the blink of an eye!
All those manipulations exhausted the poor little header. It’s time to place it where it should be and leave it alone to recover.
Before you continue, re-enable Background if you disabled it to see the Canvas border.
Select Header in the Hierarchy and set its properties in the Inspector as follows:
- Click Set Native Size to reset the size, as you probably messed with it while playing around with the pivot.
- Set Anchors to top-center.
- Set Pos X to 0 and Pos Y to -100.
You should see something like this in your Scene view:
That’s it! Now, leave the header image alone. It’s a little tired, too. :]
Now that you know about anchors and pivots, go back to the background image. Did you notice that when you added the aspect ratio fitter, some of the floor texture is now cropped off screen? We can use the Aspect Ratio Fitter in combination with the pivot to fix this:
- Disable the Aspect Ratio Fitter
- Set the Pivot to (X:0.5, Y:0)
- Re-enable the Aspect Ratio Fitter
Now, not only does the background always fit the size of the phone, the floor part will always stay in view!
Adding the Start Button
Now, that your game has a nice background with a label, it’s time to add some buttons.
From the top bar, choose GameObject ‣ UI ‣ Button. This will add a Button object to the scene. You should see it in the Hierarchy. If you expand it in the Hierarchy, you’ll see that the button contains a Text child — you’ll learn about these later.
Look at the button in the Inspector, and you’ll see it has the same Image component you used to add the background and the header label.
Additionally, there’s a Button component. In other words, a button is just an image with a child Text element and an attached button script.
Positioning the Button
Now it’s all about positioning and resizing the button. Follow these steps:
- Select Button in the Hierarchy view and rename it to StartButton.
- Set its Anchors to bottom-stretch, since you want it to stretch horizontally if the screen size changes. Tip: Hold Alt/Cmd and Shift when selecting the anchor to also set the position and pivot.
- To further customize the anchors, set the X to (Min:0.2, Max:0.8)
- Set both Left and Right to 123.
- Set the Pivot to (X:0.5, Y:0)
- Set Height to 80.
- Set Pos Y to 300.
Then select the nested Text element and set its Text to Start Game. Change the Font Size to 32 to make the text of the button larger.
This is what you should see in the Scene view:
Now you have a button, but it needs a facelift! To make the button look good, you’ll set an image as its background and then use a fancier font.
9-Slice Scaling
You set the image for buttons and images in the same way. After all, they use the same component. However, images rarely scale, especially non-uniformly. Buttons, on the other hand, often come in different sizes.
You could create a background image for every single button size in your game, but why waste all that space? You’ll use a technique called 9-Slice scaling, which allows you to provide one small image that scales to fit all sizes.
There’s no magic involved here. This technique works by creating different images for each of nine zones, all of which scale differently.
This ensures the image will look good at any scale.
Preparing Button Images
Before you can use a sliced image, you need to set its nine zones. To do this, open the Menu folder in the Project window and select btn_9slice_normal image.
In the Inspector’s Import Settings, set Texture Type to Sprite (2D and UI) and apply the change. Next, click on the Sprite Editor button to open the Sprite Editor view.
In the Sprite Editor, set the Border values to L:14, R:14, B:16, T:16. Remember to click Apply!
Repeat the same process for the btn_9slice_highlighted and btn_9slice_pressed images, which you’ll use for different button states.
Setting Button Images
After preparing all your images, drag them to the corresponding fields in the Inspector. Select StartButton in the Hierarchy and follow these steps:
- Change Image Type to Sliced in the Image component.
- Change the Transition property in the Button component to SpriteSwap.
- Drag btn_9slice_normal to Source Image in the Image component.
- Drag btn_9slice_highlighted to Highlighted Sprite in the Button component.
- Drag btn_9slice_pressed to Pressed Sprite in the Button component.
Before running the scene and enjoying your cool buttons, you’re going to change the font used by the nested Text label. This will make the button mega-awesome.
Setting a Custom Font for the Button
Using a custom font is easy. Remember the Fonts folder in the package you downloaded and added to the project? Now it’s time to break it out and use one of those fonts.
Select the Text element nested within StartButton in the Hierarchy. Then open the Fonts folder in the Project window and drag the TitanOne-Regular font into the Font field. Also set the Color to white.
Now run the scene and enjoy your new mega-awesome button! :]
Adding the Settings Button
There are a few things left to do. First, add the Settings button.
You can probably do this yourself, so you’re only getting the size and position of the button to start. The rest is almost identical to they way you created the Start Game button.
These properties of the Settings button are different:
- Name: SettingsButton
- Rect Transform: Left and Right are 400, Height is 70 and Pos Y is 180
- Text: Settings
- Fontsize: 24
[spoiler title=”Solution Inside: Need help creating the Settings button?”]
If you couldn’t quite do it, just follow these steps:
- From the top bar, select GameObject ► UI ► Button. This will create a Button object in the scene.
- Select Button in the Hierarchy view and rename it to SettingsButton.
- Set the button Anchors to bottom-stretch. Then update the X to (Min:0.2, Max:0.8)
- Set the Pivot to (X:0.5, Y:0)
- Set both Left and Right in Rect Transform to 173.
- Set Height to 70 and Pos Y to 180.
- Set Transition in the Button component to Sprite Swap.
- Make sure to set Image Type in the Image component to Sliced.
- Open the Menu folder in the Project Browser and drag btn_9slice_normal to Source Image.
- Then drag btn_9slice_highlighted to Highlighted Sprite.
- And finally, drag btn_9slice_pressed to Pressed Sprite.
- Double-click on Color inside the Image component and check if A is set to 255 to remove any transparency.
- Select the nested Text label.
- Change Text to Settings.
- Set Font Size to 24.
- Change Color to White.
- Open the Fonts folder in the Project window and drag the TitanOne-Regular font into the Font field in the Inspector view.
[/spoiler]
This is what you should see in the Scene view after adding the Settings button:
Now Save your scene.