0
0
Unityframework~20 mins

Anchoring and responsive UI in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UI Anchoring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity UI anchoring code?
Consider a UI Button anchored to the top-right corner of the Canvas. The Canvas size is 800x600 pixels. The Button's RectTransform has anchors set to (1,1) for both min and max, and its anchoredPosition is (-50, -30). What will be the Button's position relative to the Canvas?
Unity
RectTransform buttonRect = button.GetComponent<RectTransform>();
Vector2 anchorMin = buttonRect.anchorMin; // (1,1)
Vector2 anchorMax = buttonRect.anchorMax; // (1,1)
Vector2 anchoredPos = buttonRect.anchoredPosition; // (-50, -30)
Vector2 canvasSize = new Vector2(800, 600);
Vector2 buttonPos = new Vector2(canvasSize.x * anchorMin.x + anchoredPos.x, canvasSize.y * anchorMin.y + anchoredPos.y);
Debug.Log($"Button position: {buttonPos}");
AButton position: (50, 30)
BButton position: (750, 570)
CButton position: (850, 630)
DButton position: (-50, -30)
Attempts:
2 left
💡 Hint
Remember that anchor (1,1) means top-right corner, so position is relative to top-right.
🧠 Conceptual
intermediate
1:30remaining
Which anchor setup makes a UI element stretch horizontally but stay fixed vertically?
You want a UI Panel to stretch across the full width of the Canvas but keep a fixed height and vertical position. Which anchorMin and anchorMax values achieve this?
AanchorMin = (0, 0), anchorMax = (0, 1)
BanchorMin = (0.5, 0), anchorMax = (0.5, 1)
CanchorMin = (0, 0.5), anchorMax = (1, 0.5)
DanchorMin = (0, 0), anchorMax = (1, 1)
Attempts:
2 left
💡 Hint
Stretch horizontally means anchors span full width (0 to 1) on x-axis, but fixed vertical means anchors share the same y value.
🔧 Debug
advanced
2:00remaining
Why does this UI element not resize with the Canvas?
A developer sets a UI Image's anchors to (0.5, 0.5) for both min and max, and sets its sizeDelta to (200, 100). When the Canvas size changes, the Image stays the same size and position. Why?
Unity
RectTransform imageRect = image.GetComponent<RectTransform>();
imageRect.anchorMin = new Vector2(0.5f, 0.5f);
imageRect.anchorMax = new Vector2(0.5f, 0.5f);
imageRect.sizeDelta = new Vector2(200, 100);
ABecause anchors are fixed at center, sizeDelta controls size, so it won't resize with Canvas.
BBecause sizeDelta is ignored when anchors are equal, so the element has zero size.
CBecause anchors must be set to (0,0) and (1,1) to enable resizing.
DBecause the Canvas Scaler component is missing, so resizing is disabled.
Attempts:
2 left
💡 Hint
Anchors at the same point fix the element's position and size relative to that point.
📝 Syntax
advanced
1:30remaining
Which code correctly sets a UI element to stretch fully in both directions?
Select the code snippet that correctly sets a RectTransform to stretch horizontally and vertically inside its parent Canvas.
A
rect.anchorMin = new Vector2(0.5f, 0.5f);
rect.anchorMax = new Vector2(0.5f, 0.5f);
rect.sizeDelta = Vector2.zero;
B
rect.anchorMin = new Vector2(0, 0);
rect.anchorMax = new Vector2(0, 0);
rect.sizeDelta = Vector2.one;
C
rect.anchorMin = new Vector2(1, 1);
rect.anchorMax = new Vector2(0, 0);
rect.sizeDelta = Vector2.zero;
D
rect.anchorMin = new Vector2(0, 0);
rect.anchorMax = new Vector2(1, 1);
rect.sizeDelta = Vector2.zero;
Attempts:
2 left
💡 Hint
Stretching means anchors at opposite corners and sizeDelta zero.
🚀 Application
expert
2:30remaining
How to keep a UI element anchored to bottom-left but scale with Canvas size?
You want a UI Text element to stay anchored to the bottom-left corner of the Canvas but also scale its size proportionally when the Canvas size changes. Which approach achieves this?
ASet anchorMin and anchorMax to (0,0), use sizeDelta for base size, and add a script to scale sizeDelta based on Canvas size.
BSet anchorMin to (0,0), anchorMax to (1,1), and set pivot to (0,0) to stretch and scale automatically.
CSet anchorMin and anchorMax to (0,0), set sizeDelta to zero, and rely on Canvas Scaler to scale automatically.
DSet anchorMin and anchorMax to (0.5,0.5), and use layout groups to control scaling.
Attempts:
2 left
💡 Hint
Anchors at (0,0) fix position; scaling size requires manual adjustment or layout control.