Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active October 8, 2022 09:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsubaki/d49cb32671d599b8061edc3107e2bb23 to your computer and use it in GitHub Desktop.
Save tsubaki/d49cb32671d599b8061edc3107e2bb23 to your computer and use it in GitHub Desktop.
using UnityEngine;
[RequireComponent(typeof(RectTransform))]
[ExecuteAlways]
public class SafeAreaPadding : MonoBehaviour
{
private DeviceOrientation postOrientation;
void Update()
{
if (Input.deviceOrientation != DeviceOrientation.Unknown && postOrientation == Input.deviceOrientation)
return;
postOrientation = Input.deviceOrientation;
var rect = GetComponent<RectTransform>();
var area = Screen.safeArea;
var resolition = Screen.currentResolution;
rect.sizeDelta = Vector2.zero;
rect.anchorMax = new Vector2(area.xMax / resolition.width, area.yMax / resolition.height);
rect.anchorMin = new Vector2(area.xMin / resolition.width, area.yMin / resolition.height);
}
}
@tsubaki
Copy link
Author

tsubaki commented Oct 30, 2019

anim (31)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment