Last active
October 8, 2022 09:18
-
-
Save tsubaki/d49cb32671d599b8061edc3107e2bb23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Author
tsubaki
commented
Oct 30, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment