0
0
UnityHow-ToBeginner ยท 3 min read

How to Create a Panel in Unity UI Quickly

In Unity, create a panel by adding a UI Panel object inside a Canvas. You can do this via the GameObject menu or by scripting with GameObject and RectTransform components to control size and position.
๐Ÿ“

Syntax

To create a panel in Unity, you typically add a GameObject with a RectTransform and an Image component inside a Canvas. The RectTransform controls the panel's size and position, while the Image gives it a visible background.

Key parts:

  • GameObject panel = new GameObject("Panel"): creates the panel object.
  • panel.transform.SetParent(canvas.transform): places the panel inside the canvas.
  • RectTransform: sets size and position.
  • Image: adds background color or sprite.
csharp
GameObject panel = new GameObject("Panel");
panel.transform.SetParent(canvas.transform, false);
RectTransform rect = panel.AddComponent<RectTransform>();
rect.sizeDelta = new Vector2(200, 200);
Image img = panel.AddComponent<Image>();
img.color = Color.gray;
๐Ÿ’ป

Example

This example creates a simple gray panel inside an existing Canvas in the scene. It sets the panel size to 300x150 pixels and centers it.

csharp
using UnityEngine;
using UnityEngine.UI;

public class CreatePanelExample : MonoBehaviour
{
    void Start()
    {
        Canvas canvas = FindObjectOfType<Canvas>();
        if (canvas == null)
        {
            Debug.LogError("No Canvas found in the scene.");
            return;
        }

        GameObject panel = new GameObject("Panel");
        panel.transform.SetParent(canvas.transform, false);

        RectTransform rect = panel.AddComponent<RectTransform>();
        rect.sizeDelta = new Vector2(300, 150);
        rect.anchoredPosition = Vector2.zero;

        Image img = panel.AddComponent<Image>();
        img.color = new Color(0.5f, 0.5f, 0.5f, 0.8f); // semi-transparent gray
    }
}
Output
A gray rectangular panel of size 300x150 pixels appears centered inside the Canvas when the scene runs.
โš ๏ธ

Common Pitfalls

Common mistakes when creating panels in Unity include:

  • Not having a Canvas in the scene, so the panel won't display.
  • Forgetting to set the panel's parent to the canvas, causing it to not appear or be misplaced.
  • Not setting RectTransform size, resulting in a panel with zero size and invisible.
  • Not adding an Image component, so the panel has no visible background.

Always check these to ensure your panel shows up correctly.

csharp
/* Wrong way: panel not parented to canvas and no size set */
GameObject panel = new GameObject("Panel");
// Missing: panel.transform.SetParent(canvas.transform);
// Missing: rect.sizeDelta = new Vector2(200, 200);

/* Right way: */
panel.transform.SetParent(canvas.transform, false);
RectTransform rect = panel.AddComponent<RectTransform>();
rect.sizeDelta = new Vector2(200, 200);
๐Ÿ“Š

Quick Reference

Tips for creating panels in Unity UI:

  • Always create panels as children of a Canvas.
  • Use RectTransform to control size and position.
  • Add an Image component for visible background.
  • Set anchoredPosition to position panel relative to parent.
  • Use SetParent with worldPositionStays=false to keep local positioning.
โœ…

Key Takeaways

Create panels as GameObjects with RectTransform and Image inside a Canvas.
Always set the panel's parent to the Canvas to ensure it displays correctly.
Set the RectTransform sizeDelta to define panel size; otherwise, it will be invisible.
Add an Image component to give the panel a visible background color or sprite.
Use anchoredPosition to control where the panel appears inside the Canvas.