Skip to content

Instantly share code, notes, and snippets.

@wakeup5
Last active May 2, 2022 07:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakeup5/9ee2cd26f788acc10cbfc0e931caf09a to your computer and use it in GitHub Desktop.
Save wakeup5/9ee2cd26f788acc10cbfc0e931caf09a to your computer and use it in GitHub Desktop.
A component that can control the Safe Area by edge in Unity.
using System;
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
public class SafeAreaContainer : MonoBehaviour
{
[Flags]
public enum Edge
{
Top = 1,
Bottom = 2,
Left = 4,
Right = 8
}
[SerializeField] private Edge edge = Edge.Top | Edge.Bottom | Edge.Left | Edge.Right;
[SerializeField] private bool lockOffset;
private Rect lastSafeArea;
private RectTransform parentRectTransform;
private DrivenRectTransformTracker tracker = new DrivenRectTransformTracker();
private RectTransform rectTransform;
private void Awake()
{
rectTransform = transform as RectTransform;
parentRectTransform = this.GetComponentInParent<RectTransform>();
}
private void Update()
{
#if UNITY_EDITOR
ApplySafeArea();
#else
if (lastSafeArea != Screen.safeArea)
{
ApplySafeArea();
}
#endif
}
private void ApplySafeArea()
{
Rect safeArea = Screen.safeArea;
var min = safeArea.position;
var max = min + safeArea.size;
min.x /= Screen.width;
min.y /= Screen.height;
max.x /= Screen.width;
max.y /= Screen.height;
// Min
var anchorMin = rectTransform.anchorMin;
var offsetMin = rectTransform.offsetMin;
if (edge.HasFlag(Edge.Left))
{
anchorMin.x = min.x;
offsetMin.x = 0f;
}
if (edge.HasFlag(Edge.Bottom))
{
anchorMin.y = min.y;
offsetMin.y = 0f;
}
var anchorMax = rectTransform.anchorMax;
var offsetMax = rectTransform.offsetMax;
// Max
if (edge.HasFlag(Edge.Right))
{
anchorMax.x = max.x;
offsetMax.x = 0f;
}
if (edge.HasFlag(Edge.Top))
{
anchorMax.y = max.y;
offsetMax.y = 0f;
}
rectTransform.anchorMin = anchorMin;
rectTransform.anchorMax = anchorMax;
if (lockOffset)
{
rectTransform.offsetMin = offsetMin;
rectTransform.offsetMax = offsetMax;
}
// rectTransform.pivot = new Vector2(0.5f, 0.5f);
rectTransform.localScale = Vector3.one;
lastSafeArea = Screen.safeArea;
var properties =
// DrivenTransformProperties.Pivot |
DrivenTransformProperties.AnchoredPositionZ |
DrivenTransformProperties.Scale;
if (edge.HasFlag(Edge.Top)) properties |= DrivenTransformProperties.AnchorMaxY;// | DrivenTransformProperties.AnchoredPositionY;
if (edge.HasFlag(Edge.Bottom)) properties |= DrivenTransformProperties.AnchorMinY;// | DrivenTransformProperties.ScaleY;
if (edge.HasFlag(Edge.Left)) properties |= DrivenTransformProperties.AnchorMinX;// | DrivenTransformProperties.AnchoredPositionX;
if (edge.HasFlag(Edge.Right)) properties |= DrivenTransformProperties.AnchorMaxX;// | DrivenTransformProperties.ScaleX;
tracker.Clear();
tracker.Add(this, transform as RectTransform, properties);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment