Skip to main content

UISafeAreaAdapter

Important Attention Reminder

Coding Style wiki


UISafeAreaAdapter is a UI safe area adaptation component that automatically adjusts the anchors (anchorMin / anchorMax) of a target panel (RectTransform) based on Screen.safeArea, fitting notches, punch holes, and irregular screens. It re-applies automatically on resolution changes, and refreshAlways can be enabled for per-frame refresh (e.g. for screen rotation).

NamespaceOxGKit.Utilities.Adapter
Typepublic class UISafeAreaAdapter : MonoBehaviour
SourceUISafeAreaAdapter.cs
using OxGKit.Utilities.Adapter;

Reminder Add the component via AddComponentMenu: OxGKit/Utilities/Adapter/UISafeAreaAdapter (one per object, DisallowMultipleComponent).

Quick Start

  1. Attach UISafeAreaAdapter to the UI panel to be adapted.
  2. When panel is not assigned, Awake automatically uses the object's own RectTransform.
  3. The first adaptation runs in Start; it re-adapts automatically on resolution changes.
using OxGKit.Utilities.Adapter;
using UnityEngine;

public class SafeAreaDemo : MonoBehaviour
{
public UISafeAreaAdapter adapter;

private void Start()
{
// Enable per-frame refresh when handling screen rotation
this.adapter.refreshAlways = true;

// Or refresh manually at specific moments
this.adapter.RefreshViewSize();
}
}

General Rules

Refresh Timing

  • Start: performs the first adaptation automatically.
  • LateUpdate: adapts automatically when a resolution change is detected; with refreshAlways = true, it adapts every frame.
  • Manual: call RefreshViewSize.

Reminder The safe area and resolution logs are emitted only when the values actually change (avoiding per-frame string allocations); logs go through OxGKit.LoggingSystem.


Members

MemberDescription
public bool refreshAlwaysWhether to refresh the adaptation every frame (default false), for scenarios such as screen rotation.
public RectTransform panelThe target panel to adapt; defaults to the object's own RectTransform when unassigned.

Methods

RefreshViewSize

public void RefreshViewSize()

Recomputes and applies the panel's anchorMin and anchorMax based on the current Screen.safeArea (normalized by the screen width and height).

adapter.RefreshViewSize();