Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Hopping around the playable area and collecting flies would be more meaningful to the player if we show them how many they've collected. Let's do that by adding a score counter to the game.
Unity Documentation
- Unity UI - This section of the Unity Manual provides lots of detail about Unity's UI system.
-
0:00
[MUSIC]
-
0:04
Hopping around the playable area and collecting flies would be more meaningful
-
0:09
to the player if we showed them how many they've collected.
-
0:12
Let's do that by adding a score counter to the game.
-
0:17
Unity has a robust user interface system that allows us to create screen
-
0:22
overlays like score counters, menus, loading bars, and more.
-
0:27
The user interface elements,
-
0:29
or UI elements, are just like any other object in the hierarchy.
-
0:34
The difference is that they can exist in World Space,
-
0:39
just like other 3-D objects in the scene, or in Screen Space,
-
0:44
where they're an overlay on the screen, no matter how the camera is oriented.
-
0:51
Let's create the score counter now, so you can see what I mean.
-
0:55
In the Hierarchy window,
-
0:59
go to the Create Menu > UI > Text.
-
1:05
This will create a few game objects.
-
1:07
A Canvas with a child Text game object and an EventSystem.
-
1:16
The EventSystem is for more advanced UI input
-
1:20
when working with menus that require input from a mouse or from touch controls.
-
1:26
For this game, we will simply delete it.
-
1:29
Right-click on the EventSystem > Delete.
-
1:34
Let's click the Canvas game object first.
-
1:37
A Canvas is a game object with a Canvas component attached,
-
1:44
which helps to group and position UI elements.
-
1:47
We'll learn more about Unity's UI system in future lessons.
-
1:51
But one thing I want to draw your attention to now
-
1:54
is the Canvas component in the Inspector.
-
1:57
Here, you'll see a Render mode drop down with the choices
-
2:03
Screen Space- Overlay, Screen Space- Camera, and World Space.
-
2:08
For our game, we'll keep the default setting of Screen Space- Overlay,
-
2:13
since we want the score counter to always appear in the same place on the screen.
-
2:19
After all, it would be pretty distracting if the score counter moved
-
2:22
as the camera moved to follow our frog.
-
2:26
But you can also use this menu to tell the canvas to overlay elements
-
2:32
on the game camera to display a user interface that moves with the camera.
-
2:38
Or to make user interface elements exist in 3-D space with other game objects.
-
2:44
An example of World Space UI elements might be floating speech bubbles
-
2:49
that appear above a character's head In a role playing game.
-
2:54
Or in our game, perhaps we could add different colored flies that give you more
-
2:58
points, and the number of points could be displayed above each fly.
-
3:03
Just below the Render Mode drop down, check the box that says Pixel Perfect.
-
3:10
This option makes UI elements that include a lot of text align better with the pixels
-
3:16
of the screen, so that the edges of the letters appear sharp and easier to read.
-
3:22
Now, click the Text game object that's parented to the Canvas.
-
3:28
In the Inspector, rename this game object to Score.
-
3:35
Then turn your attention to the Text component.
-
3:40
Here we can modify elements of the text that you're probably familiar with from
-
3:44
other programs like word processors or graphic design programs.
-
3:48
You can change the font, the size of the text and, of course, the text itself.
-
3:54
Our text is actually being displayed on the screen, but it's very small.
-
3:59
We need to do a few things to make it stand out.
-
4:02
First, change the font size to a value of 80.
-
4:07
Then click the Color Picker and make the color
-
4:14
completely white, just by dragging this circle to the upper left corner.
-
4:19
And then close the Color Picker.
-
4:21
This will help the text contrast with the darker background.
-
4:26
Now turn your attention to the first component, the Rect Transform component.
-
4:33
The Rect Transform component shares some similarities to a transform.
-
4:37
But it's specifically designed for
-
4:40
anchoring UI elements to a particular areas of the Canvas.
-
4:43
You will use this component to make sure text
-
4:46
stays at a particular area of the screen, and will be in the correct
-
4:50
place on different screens with different resolutions and aspect ratios.
-
4:56
Our text needs to have a proper width and height.
-
5:00
And then we need to position it.
-
5:02
At the bottom of the Inspector,
-
5:05
there is a Layout Properties window with a lot of information inside of it.
-
5:09
This window displays the preferred width and
-
5:12
height of the text, based on our font size.
-
5:16
And the text we've typed inside of the text component.
-
5:20
Always set the width and height values of the Rect Transform component to be
-
5:24
slightly larger than the preferred width and preferred height values.
-
5:30
These values will give room for
-
5:32
text that changes as the game is played, like the numbers in a score counter.
-
5:37
So in the Rect Transform component,
-
5:42
set the width to a value of 400 and
-
5:46
set the height value to 300.
-
5:50
Now we can see the text.
-
5:51
And next we need to position the text properly.
-
5:55
Click on the illustration of a box with
-
5:58
red lines over it inside of the Rect Transform component.
-
6:02
This brings up the Anchor Presets window,
-
6:05
which lets us quickly set the anchor point of our Score element on the screen.
-
6:10
We want the Score element to be anchored to the upper right.
-
6:14
But in addition to the anchor point,
-
6:16
we also want it to be positioned in the upper right.
-
6:21
So with the Anchor Presets window open, hold down the Alt key on your
-
6:26
keyboard and then choose the box in the upper right
-
6:32
to move the anchor point and position the element.
-
6:38
This is almost what we want,
-
6:40
but we can tweak this to make it look a little better.
-
6:44
Click in any blank area to exit the Anchor Presets window.
-
6:49
And then in the Text component, change the horizontal alignment so
-
6:54
that the text is right aligned to its container.
-
6:59
Now, let's pick a font that's more suited to the playful nature of the game.
-
7:04
We could go to the Project window and
-
7:07
drag one of the included fonts over to the Font field.
-
7:11
But instead, I'd like to show you another way of searching for
-
7:14
files in your project.
-
7:17
Click on the small circle just to the right of the Font field.
-
7:22
This brings up an Asset Selection window, that only shows files
-
7:27
in our project that fit the type that the field is looking for.
-
7:32
This field is requesting a font.
-
7:35
So the window only shows fonts in our project and nothing else.
-
7:40
Double-click the Verela Round font to select it.
-
7:46
Finally, let's move the text so
-
7:48
that it's not right up against the edge of the screen.
-
7:52
In the Rect Transform component change the position X value to -220 so
-
8:00
that that text has a little bit of space.
-
8:06
We can use the text area in the Text component to get a preview of
-
8:10
what a score might look like.
-
8:13
I'll type 0 Flies as a placeholder.
-
8:19
The score counter looks nice, but it won't actually do anything yet.
-
8:22
In the next video, we'll write a script to
-
8:25
update the score counter as the player collects the pick up items.
You need to sign up for Treehouse in order to download course files.
Sign up