In this chapter, we are going to take the scenes that we created for our game loop in the previous chapter and move our focus on to text, imagery, and animation through various customizations.
A requirement of the Unity Programmer Exam is not only to be confident with your C# programming skills but also to be familiar with what the Unity Editor offers in terms of its components and tools. Therefore, in this chapter, we will do no programming and, instead, focus on our User Interface (UI), which consists of Image and Text components. It's also worth mentioning that we will make our UI expand and contract with the screen's ratio size, which isn't possible with 3D assets alone (please refer to the previous chapter for more details). We will also import and apply our own custom font while we learn about our Text component. Finally, we will animate the UI with Animator and make use of the Animator Controller, which involves creating our own states.
The following screenshot shows what our title screen should look like by the end of the chapter:
We will cover the following topics in this chapter:
By the end of this chapter, you will feel more confident about combining Text and Image components together, along with animating the UI.
Programming core interactions:
Working in the art pipeline:
Developing application systems:
The project content for this chapter can be found at https://github.com/PacktPublishing/Unity-Certified-Programmer-Exam-Guide-Second-Edition/tree/main/Chapter_08.
You can download the entirety of each chapter's project files at https://github.com/PacktPublishing/Unity-Certified-Programmer-Exam-Guide-Second-Edition.
All content for this chapter is held in the chapter's unitypackage file, including a Complete folder that contains all of the work that we'll carry out in this chapter.
Check out the following video to see the Code in Action: https://bit.ly/3kqIi8k.
The purpose of the Canvas is to typically hold images and text in a 2D world. Its primary purpose is to allow the user to interact with things, such as clicking on buttons, pushing volume sliders, and turning knobs, which is more commonly known as the UI.
Unity (quite confusingly) makes it such that the 2D Canvas also shares the same space as it's a 3D world. Therefore, in our scene, we will typically have a large canvas area with the UI; then, further down in the bottom left of the screen, we will have our 3D world.
The following screenshot shows an example of a Unity scene with an implemented Canvas component, along with a cube and a UI button:
As you can see in the preceding screenshot, on the right-hand side, we have the Game view showing a 3D cube and UI Button. On the left-hand side, we have the Scene view showing the same cube but with the Button missing. This is because, in the Scene window, the Canvas that holds the UI button is located in its own 2D space. To resolve this issue, we need to zoom out of the Scene view, and we will see where the UI button is located. Additionally, we will see the outline of a large white rectangle that represents the screen ratio.
Note that because we have zoomed out so much, the 3D cube is really small. In the following screenshot, we can't even see the cube, on the left-hand side marked with a circle outline. It's a little complicated to understand at first, but consider it like two projects sharing the same space:
With that brief example of how the Canvas shares space with the 3D space, let's move on to the next section, where we'll start using the Canvas and add some text and images.
In this section, we are going to change the following scenes:
We'll replace those scenes with the following:
As mentioned in the introduction, the benefit of doing this is that the text will remain the same size, no matter the ratio or resolution of the screen.
The following screenshot shows the current BootUp scene on the left and what it should look like after making the changes on the right:
If you haven't already opened the scene in the Unity Editor, go to the Project window and open the bootUp scene from Assets/Scene.
Let's start by changing the background color from gray to black. If you have forgotten how to do this, follow these steps:
The Game window will now change from gray to black, and then we will remove the BootUp text and replace it with our new 2D text.
We are now going to add the Canvas and 2D text to the scene.
Because we have added 2D text, Unity helps us out by automatically adding the Canvas and EventSystem to the scene. The following screenshot shows the bootUp Hierarchy window containing the Canvas game object along with its child, the Text game object:
With presented still selected in the Hierarchy window, pay attention to the Inspector window because we need to update its Text component.
Let's continue modifying the presented game object's Text component settings.
We will change the font from the typical Arial style to something more fitting for our game. In this chapter's project files, there should be a font in the Project window, which is located in Assets/Font/ethnocentric rg it. We can select this font from our presented game object.
At this point, our font will have disappeared from the Scene window. This is because the Rect Tool isn't big enough and we need to resize it. We will do this next.
Information Note
The Rect Tool is an area for the images or text to sit in. Consider it as a similar tool to the Transform component, where we enter the Vector3 values for the Position, Rotation, and Scale of our game's objects, which we have been altering from Chapter 2, Adding and Manipulating Objects, onward.
Now that we have the Rect Tool spacing set up, we need to set the Anchors so that the text remains the correct size no matter what the screen ratio or resolution is.
In the center of the Canvas screen, we should be able to see four arrows pointing toward each other (the left-hand side of the following screenshot has these four arrows circled in red).
To set the Anchors in the same location as the four blue circles, perform the following steps:
Now that our Anchors are roughly sitting on top of the Rect Tool, which is denoted by blue circles, we can make it so that the Position and Anchors are both aligned.
Now, we can continue with the Text component in the Inspector window in order to set the color and position of the text.
We have now had a full run with the Canvas and Text components, and we have set our own custom 2D text up. Additionally, this text will be able to adjust to the screen ratios compared to the bootUp TextMesh that we had before.
The following screenshot shows our custom text, color, size, and alignment:
As you can imagine, we haven't quite finished yet because we need to have the name or company's name appear underneath the PRESENTED BY text. Thankfully, we only need to repeat about a quarter of the work we've just done. And, as you've probably guessed – yes – we can copy and paste this text.
To set our own name or company name underneath PRESENTED BY, perform the following steps:
So, our new text is in place. All we need to do now is to change what has been typed in.
We have covered the fundamentals of a Canvas component and how to apply 2D text. Next, we will be repeating a similar procedure and using the Image component. This is equivalent to the Sprite Renderer that we used for our shop scene buttons in Chapter 5, Creating a Shop Scene for Our Game. However, here, the Image component is for a 2D space.
From this point to the end of the chapter, we will go through a series of subsections in order to polish and animate our scenes. We will cover the following:
Let's continue with polishing our title scene.
In this section, we will repeat the procedure that we already learned in the previous section without going into too much depth, as we already know how to create a Canvas, add custom text, and perform duplicating. In this section, we will also make use of Unity's Image component.
The following screenshot shows the transformation we will be undergoing, starting with our current title scene on the left and using the same techniques we applied in the previous section, along with adding Image components, to create the red stripe on the right:
As mentioned earlier, we won't be going into all of the details; however, if you do struggle at any point, then please refer back to the previous section to guide you through what you should already know. Let's get started:
We are now going to create an empty game object. Inside this game object, we are going to store our Text and Image components:
Typically, when a new game object is created, it will automatically be given a Transform component that holds the game object's Position, Rotation, and Scale for a 3D space. In this section, our focus is on 2D space, so we need to change this game object from a Transform component into a Rect Transform component.
To change the Title game object from Transform to Rect Transform, follow these steps:
We are now going to set our Title game object's Anchor sizes. This is so any game objects that become a child to it will be restricted within the Title game object's Anchors.
I have set my Title game object's Rect Transform component to the following settings:
As you can see, in the previous screenshot, the Anchors for the Title game object are centered in the canvas's white box outline. Also, note that the Rect Transform component's Left, Top, Pos Z, Right, and Bottom positions are all set to the value of 0.
The next step will be to add a red transparent stripe within the Title game object. To add an Image component, follow these instructions:
We have now added an Image component to our mainCol image.
Further Information
With the Image component, if we added a sprite to the parameter instead of just changing the color alone, we would also have the ability to alter its Image Type. One of the types that can be used is called Filled. This can give you the impression that the sprite is filling up, which would be useful for a loading bar or a time limit that is counting down.
If you would like to know more about the Image component and its other uses, view the documentation at https://docs.unity3d.com/2017.3/Documentation/Manual/script-Image.html.
Next, we will add a strip to the top of the image we've just made with another game object containing an Image component. To do that, we will repeat our earlier methodology but with a tighter, thinner strip. Follow these steps:
For our trim set, we don't need to change the color as it duplicates from the mainCol game object. We now need to repeat this process for the bottom part of the mainCol image.
Here are the steps that we need to accomplish to copy another trim game object:
Now it's time to enter our main title text, KILLER WAVE, by following these instructions:
This will fill our TitleText Rect Transform settings to the same size as the parent game object (Title).
The final steps for our TitleText game object are to give it a Text component and set its values in the Inspector window:
Our title is set. The last thing we need to do in this scene is to set a message at the bottom of the screen to prompt the player to start the game.
Similar to what we did in the bootUp scene, we can duplicate our TitleText game object. However, this time, we are going to move the duplicated game object outside of the Rect Transform restrictions of its parent. The final text we will display will be a message to prompt the player to tap on the screen or shoot to begin playing the game.
To enter the SHOOT TO START text, follow these instructions:
As mentioned earlier, we are going to move the Text selection from its current area to outside its parent.
Zero out the Left, Top, Pos Z, Right, and Bottom Rect Transform properties. This will move our SHOOT TO START text down and into the location where the white arrows are. The following screenshot shows the placement of the text along with its Rect Transform property values:
We don't need to change any of the functionality of the scene as we have already set this up.
The following screenshot shows what our title scene now looks like:
So far, we have gone further with our text by duplicating it, altering it, and moving it outside of its parent Rect Tool game object. We have also introduced images and used them in a similar way to our 2D text.
We will now continue and work on the next scene: gameOver.
In this section, we will improve the gameOver scene from its gray background and blocky white text and replace it with the same images and text from the title scene. However, this time, we won't be repeating the same steps from the previous sections to recreate the same outcome.
We will copy, paste, and tweak the game objects to save time and effort rather than repeat what we have already achieved with the title scene.
As an overview, here is what our gameOver UI game objects will contain and do:
Thankfully, we don't really need to worry too much about what their roles are because we have already established this in the previous section. To duplicate our game objects and move them from the title scene to gameOver, perform these steps:
To confirm what we have done so far, the following screenshot shows the Hierarchy window for the gameOver scene:
This is what our gameOver scene should look like:
In this section, we discovered that we can simply copy and paste game objects from one scene to another as long as we work within the same Unity project. This saves time and effort and keeps our game looking uniform in accordance with the rest of the scenes.
In the next section, we will learn how to animate our UI game objects.
In this section, we will use a number of techniques that we have already covered, so we won't be going into the same level of detail. Once we have duplicated and changed the game objects, we will also be adding animation elements to make our 2D visuals less static.
We will be using a similar methodology to the gameOver scene by copying our previous scene's Canvas with its child game objects (not the Main Camera game object). The most suitable scene for this would be the gameOver scene as it has the basic elements we need. This only requires a couple of amendments before moving on to the animation phase.
To set up the level1 scene, perform the following steps:
That's all that we need to do to our Canvas game object in the Hierarchy window. We can now move on to changing the main text itself from GAME OVER to LEVEL 1.
With the Level game object still selected, remove Game Over from the Text component's Text field and replace it with LEVEL 1, as shown in the following screenshot:
We are now ready to start animating the UI 2D text and its image. All the game objects that we will be animating sit within the LevelTitle game object.
To set up our animation, we need to do the following:
Our LevelTitle game object now has an Animator component. We now need to give it an Animator Controller to manage when to animate the contents of the LevelTitle game object. To do that, follow these steps:
We now need to attach the new LevelTitle Animator Controller to our Animator component.
The following screenshot shows the LevelTitle game object with the LevelTitle controller selected:
Next, we need to create an animation so that we can add it to the Animator Controller:
Let's now open the Animator Controller and add the levelTitle_A clip to it.
This will open the Animator window.
The following screenshot shows the Animator window with its three default states and also a reference to the location of the Animator Controller that is selected:
Before we drop the animation clip in, it will benefit us to have a small time delay before the clip is played; otherwise, the animation might play too soon. In order to fix this, we can make an empty state that has a time limit. We can set this idle state to play at any speed or point we want before we play the intended animation clip (levelTitle_A).
To create an idle state and hook it up to the intended animation clip, follow these steps:
The following screenshot indicates what our states should look like now:
It will not be necessary to use the Animator window after the animation is complete, and we may need to tweak the delay. However, for this, we will need to use the Animation window, so it's ideal to have this at the bottom of the screen. To do that, perform these steps:
The following screenshot is in accordance with the preceding numbered bullets:
In this section, we have brought in the GAME OVER UI art and replaced the text from its Text component; we also brought over its sprites (banner and trim) and Image component to give us a representation of which level we are on. We then prepared the game objects to be animated with the Animator Controller and its states, followed by creating a blank Animation clip.
We can now start animating the entrance and exit for our LEVEL 1 UI art in the next section.
We are going to animate two game objects: the level title and the main strip bar in the level1 scene. In the previous section, we set up the Animation window at the bottom of the Unity Editor. The following screenshot shows our current setup for the placement of the windows, which may be helpful for reference purposes:
With regard to the animation itself, we will be animating the following:
The following screenshots show this:
So, the four main elements to animate are the Level game object, which contains the 2D Text component. The other three to be animated will be the mainCol, trim00, and trim01 game objects that contain the Image component's color values. Let's start by animating the Level game object first.
In this section, we are going to animate the text from the left to the center. It will pause so the player has a chance to read it. Then, it will move out of the screen:
With our animation track line scrubbed (note that scrub is an animation term for dragging our timeline indicator) all the way back to 0:00, which is its default, let's move our Level game object from the center of the Canvas to the left in the Scene view by doing the following.
Now that our LEVEL 1 2D text is pushed out of the way, we can scrub the animation line forward.
The fields will turn red to show the change has been recorded. The animation timeline in the Animation window will gain keyframes from the movement of the 2D text.
The following screenshot shows the changes made to the timeline:
We obviously want the LEVEL 1 text to stay where it is for a few seconds before it leaves the screen again. To make the text pause in the center before moving, follow these steps:
The following screenshot shows the timeline is at 1:25, with new keyframes added while the record button is clicked on:
Now we have come to the stage where we want to move the UI text from its center position to out of view of the camera.
This will push the LEVEL 1 text out of the camera view, as shown in the following screenshot:
We have made a start by animating the Level 1 text within the Canvas with Unity's Animation system.
Our UI text starts on the far left (out of view of the camera), animates into the center, pauses, and then animates out of view.
Now we can continue to animate the UI and move our focus from positioning to changing our UI's color (R, G, B, A) to a glowing red in the next section. This will show that our animation isn't just applied to one component but is shared through a series of components. We will animate the Image component next.
The second part of the animation phase is to have the center strip for the level title glow red and then disappear. To do this, all animation for the mainCol, trim00, and trim01 game objects will be manipulated in the Inspector window through their Image component's Color settings.
Let's start animating the Image components for all three game objects:
Our Animation window will update to indicate that the three changes have been made. In the following screenshot, we can see the properties have been changed for the mainCol, trim00, and trim01 Image component's alpha color properties (note that alpha is the A from R, G, B, and A). The Alpha setting will alter the transparency of the image:
In basic terms, the three game objects are invisible at the start of the animation in the Scene window. Next, we need to make the images come out of the transparency phase and glow red. To do that, we now need to move our timeline indicator over to 0:55 and perform the following steps:
Our three game objects have now become visible again in the Scene view. The final part of the animation is to make the three game objects turn invisible again. Instead of going back into the Color value settings, we can simply copy and paste the keys we created in the timeline at 0:00. To copy our keyframes, do the following:
When our Level 1 scene starts, we will see the title and the red bar before the animation, which we don't want. Therefore, we need to set the Level 1 text and red bar to the same values as the first frame of our animation:
The following screenshot shows the default position and alpha settings of the previous steps:
Let's remind ourselves briefly of what we have covered so far before we move on to the final section. We took our mainCol, trim00, and trim01 game objects and changed their Image component's Color alpha values over a series of keyframes using the animation timeline.
Let's now move on to the next section where we will copy our art, text, and, in some cases, animation into other scenes. From there, we will tailor the components to each scene.
Finally, we can copy all of the hard work of our level1 scene (including its animation) and paste it into the level2 and level3 scenes and amend each level number. To do that, follow these steps:
Well done! Another big chapter has been conquered. We have started to make our game shine with some sweet art that we made ourselves. Let's recap what we have achieved.
We took some plain gray scenes and made them more presentable and fit with our sci-fi game. This was all thanks to the Unity Editor, as we were able to achieve this with no scripting. The main components we covered were as follows:
Finally, we didn't need to repeat the process followed for the UI we created at the beginning of the chapter by adding the UI's text, images, animation all over again. We took the majority of these game objects and components from one scene (killer wave title screen) and pasted the game objects to the existing scenes (level title and game over) to act as a template for the game's theme. Once these components were in place, we simply modified the text field (changing the text from killer wave to level 1 and so on).
This makes working with Unity fast and user-friendly. We didn't need to start over again each time for a new UI scene from scratch. With our UI visually improved, you should hopefully start to feel like our project is turning more into a polished-looking prototype.
This chapter was about taking our game project and polishing its current content with regard to the existing UI. It is also required for your Unity Programmer Exam to understand what tools and components we have to help us create our game with regard to the Working in the art pipeline core exam skill.
We also took our Text and Image components and created one piece of animation from multiple game objects. These animations were called from the Animator Controller state machine.
In your future projects, you will have the option to keep your UI presentable while putting your game loop together.
In the next chapter, we will extend our current UI skills by making our shop scene more flexible with a range of screen ratios. Additionally, we will create a UI to sit at the bottom of our in-game levels.