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?
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}"); } }
Think about what Child Force Expand Height does when set to false.
When Child Force Expand Height is false, each child keeps its preferred height. So the buttons keep their original heights 50, 60, and 70.
In Unity UI, you want to arrange child elements side by side with equal spacing. Which layout group component should you use?
Think about the direction you want the children arranged.
The Horizontal Layout Group arranges children side by side horizontally with spacing and alignment options.
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?
Check the RectTransform size settings on the Panel.
If the Panel's RectTransform has a fixed height (e.g., set sizeDelta.y), the Content Size Fitter cannot resize it. You must allow the height to be flexible (e.g., set to 0 or use anchors) for Content Size Fitter to work.
You want to add a Horizontal Layout Group component to a GameObject named panel via script. Which code snippet is correct?
using UnityEngine;
using UnityEngine.UI;
public class AddLayout : MonoBehaviour {
public GameObject panel;
void Start() {
// Add Horizontal Layout Group here
}
}Remember the syntax for adding components in Unity.
The correct syntax to add a component is gameObject.AddComponent<ComponentType>(). Option A uses this correctly.
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?
Think about scaling and flexible layouts together.
Using Canvas Scaler with Scale With Screen Size ensures UI scales on different devices. Vertical Layout Group arranges children nicely, and Content Size Fitter lets the Panel resize to fit content dynamically.