UISafeAreaAdapter
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).
| Namespace | OxGKit.Utilities.Adapter |
| Type | public class UISafeAreaAdapter : MonoBehaviour |
| Source | UISafeAreaAdapter.cs |
using OxGKit.Utilities.Adapter;
Reminder Add the component via AddComponentMenu: OxGKit/Utilities/Adapter/UISafeAreaAdapter (one per object, DisallowMultipleComponent).
Quick Start
- Attach
UISafeAreaAdapterto the UI panel to be adapted. - When
panelis not assigned,Awakeautomatically uses the object's ownRectTransform. - 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
| Member | Description |
|---|---|
public bool refreshAlways | Whether to refresh the adaptation every frame (default false), for scenarios such as screen rotation. |
public RectTransform panel | The 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();