0
0
Unityframework~20 mins

Panel and layout groups in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unity UI Layout 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 layout code?

Consider a Unity UI Canvas with a Vertical Layout Group component on a Panel. The Panel has three child buttons with heights 50, 60, and 70 respectively. The Vertical Layout Group has Child Force Expand Height set to false. What will be the height of each button after layout?

Unity
using UnityEngine;
using UnityEngine.UI;

public class LayoutTest : MonoBehaviour {
    public VerticalLayoutGroup layoutGroup;
    public RectTransform button1, button2, button3;

    void Start() {
        // Assume buttons have preferred heights 50, 60, 70
        Debug.Log($"Button1 height: {button1.rect.height}");
        Debug.Log($"Button2 height: {button2.rect.height}");
        Debug.Log($"Button3 height: {button3.rect.height}");
    }
}
AButton1 height: 180, Button2 height: 180, Button3 height: 180
BButton1 height: 60, Button2 height: 60, Button3 height: 60
CButton1 height: 50, Button2 height: 60, Button3 height: 70
DButton1 height: 0, Button2 height: 0, Button3 height: 0
Attempts:
2 left
💡 Hint

Think about what Child Force Expand Height does when set to false.

🧠 Conceptual
intermediate
1:00remaining
Which layout group automatically arranges children horizontally with spacing?

In Unity UI, you want to arrange child elements side by side with equal spacing. Which layout group component should you use?

AVertical Layout Group
BGrid Layout Group
CContent Size Fitter
DHorizontal Layout Group
Attempts:
2 left
💡 Hint

Think about the direction you want the children arranged.

🔧 Debug
advanced
2:00remaining
Why does the panel not resize with its children?

You have a Panel with a Vertical Layout Group and several child buttons. You want the Panel to resize vertically to fit all buttons. You added a Content Size Fitter to the Panel with Vertical Fit set to Preferred Size. But the Panel height stays fixed. What is the likely cause?

AContent Size Fitter does not work with Vertical Layout Group.
BThe Panel's RectTransform has a fixed height set, preventing resizing.
CChild Force Expand Width is set to true on the Vertical Layout Group.
DThe buttons have no Layout Element components.
Attempts:
2 left
💡 Hint

Check the RectTransform size settings on the Panel.

📝 Syntax
advanced
1:30remaining
Which code snippet correctly adds a Horizontal Layout Group to a GameObject in Unity?

You want to add a Horizontal Layout Group component to a GameObject named panel via script. Which code snippet is correct?

Unity
using UnityEngine;
using UnityEngine.UI;

public class AddLayout : MonoBehaviour {
    public GameObject panel;
    void Start() {
        // Add Horizontal Layout Group here
    }
}
Apanel.AddComponent<HorizontalLayoutGroup>();
Bpanel.AddComponent(new HorizontalLayoutGroup());
Cpanel.AddComponent<HorizontalLayoutGroup>;
Dpanel.GetComponent<HorizontalLayoutGroup>();
Attempts:
2 left
💡 Hint

Remember the syntax for adding components in Unity.

🚀 Application
expert
3:00remaining
How to create a responsive UI panel that adapts to different screen sizes?

You want to create a UI Panel in Unity that automatically adjusts its size and layout to look good on phones and tablets with different screen sizes. Which combination of components and settings is best?

AUse a Canvas Scaler set to Scale With Screen Size, add a Vertical Layout Group to the Panel, and Content Size Fitter with Vertical Fit set to Preferred Size.
BUse a Canvas Scaler set to Constant Physical Size, add no layout groups, and set fixed sizes for all UI elements.
CUse a Canvas Scaler set to Scale With Screen Size, add a Horizontal Layout Group to the Panel, and disable Content Size Fitter.
DUse a Canvas Scaler set to Constant Pixel Size, add a Grid Layout Group to the Panel, and fix the Panel size manually.
Attempts:
2 left
💡 Hint

Think about scaling and flexible layouts together.