Game Dev Cheat Sheet

Unity RectTransform Anchors and UI Anchoring

Visual reference for Unity RectTransform anchor presets with C# code and common UI layout patterns.

Unity's RectTransform anchor system controls how UI elements scale and position across different screen sizes. Getting anchors wrong causes UI to break on different resolutions. This visual guide shows every anchor preset with a preview and C# code.

Anchor Presets

Centre

anchorMin
(0.5, 0.5)
anchorMax
(0.5, 0.5)
pivot
(0.5, 0.5)

Fixed size, dead centre of the screen. Use for crosshairs, dialogue popups, loading spinners, or modal windows.

RectTransform rt = GetComponent<RectTransform>();
rt.anchorMin = new Vector2(0.5f, 0.5f);
rt.anchorMax = new Vector2(0.5f, 0.5f);
rt.pivot = new Vector2(0.5f, 0.5f);

Common Patterns

Practical anchor configurations for real game UI. Each includes the full C# setup code you can copy directly into your project.

Health Bar

HUD

Top-Stretch with a fixed height. Spans the full width at the top of the screen, commonly used for health or stamina bars in action games.

min
(0, 1)
max
(1, 1)
pivot
(0.5, 1)
RectTransform rt = GetComponent<RectTransform>();
rt.anchorMin = new Vector2(0f, 1f);
rt.anchorMax = new Vector2(1f, 1f);
rt.pivot = new Vector2(0.5f, 1f);
rt.sizeDelta = new Vector2(0, 40);
rt.anchoredPosition = new Vector2(0, -10);

Minimap

HUD

Top-Right with a fixed size. Stays pinned to the top-right corner, maintaining its dimensions across all screen sizes.

min
(1, 1)
max
(1, 1)
pivot
(1, 1)
RectTransform rt = GetComponent<RectTransform>();
rt.anchorMin = new Vector2(1f, 1f);
rt.anchorMax = new Vector2(1f, 1f);
rt.pivot = new Vector2(1f, 1f);
rt.sizeDelta = new Vector2(200, 200);
rt.anchoredPosition = new Vector2(-10, -10);

Full-Screen Overlay

Pause Menu

Stretch-Both to cover the entire screen. Use for pause menus, loading screens, or fade-to-black transitions.

min
(0, 0)
max
(1, 1)
pivot
(0.5, 0.5)
RectTransform rt = GetComponent<RectTransform>();
rt.anchorMin = new Vector2(0f, 0f);
rt.anchorMax = new Vector2(1f, 1f);
rt.pivot = new Vector2(0.5f, 0.5f);
rt.sizeDelta = Vector2.zero;
rt.anchoredPosition = Vector2.zero;

Centred Popup

Dialogue

Centre with a fixed size. Perfect for dialogue boxes, confirmation prompts, or reward screens that float in the middle of the screen.

min
(0.5, 0.5)
max
(0.5, 0.5)
pivot
(0.5, 0.5)
RectTransform rt = GetComponent<RectTransform>();
rt.anchorMin = new Vector2(0.5f, 0.5f);
rt.anchorMax = new Vector2(0.5f, 0.5f);
rt.pivot = new Vector2(0.5f, 0.5f);
rt.sizeDelta = new Vector2(400, 300);
rt.anchoredPosition = Vector2.zero;

Bottom Nav Bar

Mobile

Bottom-Stretch with a fixed height. Spans the full width at the bottom, commonly used for tab bars and navigation in mobile games.

min
(0, 0)
max
(1, 0)
pivot
(0.5, 0)
RectTransform rt = GetComponent<RectTransform>();
rt.anchorMin = new Vector2(0f, 0f);
rt.anchorMax = new Vector2(1f, 0f);
rt.pivot = new Vector2(0.5f, 0f);
rt.sizeDelta = new Vector2(0, 60);
rt.anchoredPosition = Vector2.zero;

Score Text

HUD

Top-Centre with a fixed size. Sits at the top-centre for score counters, level indicators, or round timers.

min
(0.5, 1)
max
(0.5, 1)
pivot
(0.5, 1)
RectTransform rt = GetComponent<RectTransform>();
rt.anchorMin = new Vector2(0.5f, 1f);
rt.anchorMax = new Vector2(0.5f, 1f);
rt.pivot = new Vector2(0.5f, 1f);
rt.sizeDelta = new Vector2(200, 50);
rt.anchoredPosition = new Vector2(0, -10);

CanvasScaler (The Part Everyone Forgets)

The most common reason UI breaks across resolutions is a misconfigured CanvasScaler.

ModeUse WhenKey Settings
Constant Pixel SizePrototypes onlyScale factor
Scale With Screen SizeAlmost alwaysReference resolution, Screen Match Mode
Constant Physical SizeAR/VR, accessibilityDPI fallback
// Typical mobile/multi-platform CanvasScaler config
canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
canvasScaler.referenceResolution = new Vector2(1920, 1080);
canvasScaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
canvasScaler.matchWidthOrHeight = 0.5f; // blend between width and height matching

Safe Area (Notched Phones)

The single most common cause of UI being hidden by notches on iOS and Android. Attach this to the root panel of any UI that needs to avoid the notch.

using UnityEngine;

public class SafeAreaPanel : MonoBehaviour
{
    void Awake()
    {
        Rect safeArea = Screen.safeArea;
        RectTransform rt = GetComponent<RectTransform>();

        Vector2 anchorMin = safeArea.position;
        Vector2 anchorMax = safeArea.position + safeArea.size;
        anchorMin.x /= Screen.width;
        anchorMin.y /= Screen.height;
        anchorMax.x /= Screen.width;
        anchorMax.y /= Screen.height;

        rt.anchorMin = anchorMin;
        rt.anchorMax = anchorMax;
    }
}

Frequently asked questions

How do I make a Unity UI element scale with screen size?
Set the Canvas to Screen Space Overlay or Screen Space Camera, add a CanvasScaler component, and set UI Scale Mode to Scale With Screen Size. Pick a reference resolution (1920x1080 is common) and set the Match value: 0.5 balances width and height.
What are RectTransform anchors actually doing?
Anchors define which points of the parent the element is pinned to. Min and max anchors set the parent corners. If they are the same point, the element is positioned relative to that point. If they are different points, the element stretches between them.
How do I handle the iPhone notch and safe area in Unity?
Read Screen.safeArea at runtime, then set the RectTransform of a top-level UI panel to fit inside it. Convert the safeArea Rect from screen space to RectTransform anchor space using the canvas size. Apply this in OnEnable and on screen orientation change.
Why does my UI look different in the Editor vs on device?
Most often a Canvas Scaler mismatch: the Editor Game view uses one resolution, the device another. Test in the Editor with the device aspect ratio set in the Game view dropdown. Also check Camera safe areas, DPI scaling, and any RectTransform that uses absolute pixel offsets instead of anchored positions.

Last updated: