CHAPTER 08
Beginner
Unity UI System
Updated: May 16, 2026
25 min read
# CHAPTER 8
Unity UI System
1. Introduction
No matter how great your gameplay is, if the player doesn't know how much health they have, what their score is, or how to start the game, the experience fails. User Interface (UI) is the bridge between the game's hidden mathematics and the player's brain. In this chapter, we will master the Unity UI System. We will explore the Canvas, use TextMeshPro for crisp typography, create interactive Buttons, learn how to anchor elements to different screen sizes, and write C# code to update the UI dynamically during gameplay.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the 2D Canvas and how it overlays the 3D world.
- Create and manipulate TextMeshPro (TMP) elements.
- Configure UI Anchors for responsive design across different resolutions.
- Create interactive UI Buttons.
- Link C# scripts to the UI to update scores and health dynamically.
3. The Canvas
In Unity, UI elements do not exist in standard 3D space. They live on a Canvas.- Right-click the Hierarchy -> UI -> Canvas.
- The Canvas is essentially a massive invisible 2D movie screen projected directly over the player's camera.
- Rule: Every single UI element (Text, Image, Button) *must* be a child of a Canvas. If you drag text outside the Canvas, it will disappear.
4. UI Anchors (Responsive Design)
A PC monitor is widescreen (16:9). An iPad is boxy (4:3). A mobile phone is vertical. How do you ensure your "Score" text stays in the top-right corner on every device? Anchors.- Click your UI Text. In the Inspector, click the square Rect Transform anchor icon.
-
Hold
Shift + Altand click the Top-Right option.
- This pins the text to the top-right corner of the screen, regardless of the monitor's resolution or aspect ratio!
5. TextMeshPro (TMP)
Unity's old text system was blurry and pixelated. The modern standard is TextMeshPro.- Right-click Canvas -> UI -> Text - TextMeshPro. (Unity may ask you to "Import TMP Essentials"—always click yes).
- TMP uses advanced rendering to ensure your text is perfectly sharp whether it is size 10 or size 500.
- You can change the Font, Color, Outline, and Shadow directly in the Inspector.
6. Interactive Buttons
A Main Menu needs buttons.- Right-click Canvas -> UI -> Button - TextMeshPro.
- A Button is actually an Image with a script attached, plus a child Text object.
- In the Inspector, the Button component has an On Click () list. This is where you tell Unity exactly which C# function to run when the player clicks the button with their mouse.
7. Updating UI with C# Scripts
To make a score go up, your C# script needs to talk to the TextMeshPro component. You must addusing TMPro; at the top of your script.
csharp
8. Visual Learning: The UI Hierarchy
txt
9. Best Practices
-
Canvas Scaler: Click your Canvas object. In the Inspector, find the
Canvas Scalercomponent. Change the "UI Scale Mode" fromConstant Pixel SizetoScale With Screen Size. Set the Reference Resolution to1920 x 1080. This ensures your UI scales up perfectly on a 4K TV, and scales down perfectly on a 720p mobile phone without breaking your layout.
10. Common Mistakes
- Clicking the 2D Button: Editing a 2D UI inside a 3D perspective Scene View is maddening. When editing the Canvas, look at the very top of the Scene View window and click the small "2D" button. This flattens the camera, making it vastly easier to drag and align UI elements.
11. Mini Project: Build a Clicker Game
Objective: Create a button that increases a score counter on the screen.- 1. Create a Canvas. (Set it to Scale with Screen Size).
- 2. Add a TextMeshPro object. Name it "ScoreText". Anchor it to the Top Center. Change the text to "Score: 0".
- 3. Add a Button (TMP). Place it in the middle of the screen. Change its child text to "Click Me!".
-
4.
Create an empty GameObject in the Hierarchy named
GameManager.
-
5.
Create a script named
ClickerGameand attach it to theGameManager.
csharp
-
6.
Click the
GameManager, and drag the "ScoreText" UI object into the emptyscoreUIslot.
-
7.
Click the Button UI object. In the Inspector, scroll down to On Click (). Click the
+icon.
-
8.
Drag the
GameManagerobject into the empty slot.
-
9.
Click the dropdown that says
No Function, go toClickerGame, and selectOnButtonClick().
- 10. Press Play and click the button!
12. Practice Exercises
-
1.
What specific
usingnamespace must be added to the top of your C# script to manipulate modern Unity text?
- 2. What feature of the RectTransform ensures that a UI element stays in the correct corner of the screen regardless of the monitor's aspect ratio?
13. MCQs with Answers
Question 1
Every piece of UI (Buttons, Images, Text) MUST be a child of which specific Unity component to be rendered on the screen?
Question 2
When writing a custom C# function that you want to be triggered by a UI Button click, what access modifier MUST the function have?
14. Interview Questions
-
Q: Explain the purpose of the
Canvas Scalercomponent. Why is switching from "Constant Pixel Size" to "Scale With Screen Size" mandatory for multi-platform deployment?
- Q: Walk me through the exact workflow of connecting a visual UI Button to a C# script function. How do you pass the reference in the Unity Editor?
- Q: Why did Unity deprecate the legacy Text component in favor of TextMeshPro? What underlying rendering technology makes TMP superior?