Ported the UISafeArea component from the netcode sample project

This commit is contained in:
Brown, Caleb M 2023-07-26 12:43:23 -04:00
parent 1bfdaa4316
commit 31a46d107c
4 changed files with 108 additions and 0 deletions

8
Assets/Scripts/UI.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 185abe3ead7ed39458ac3c3db107c922
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7e46dbb60c9a4ccca21c3c40e02dec39
timeCreated: 1690370576

View File

@ -0,0 +1,86 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
namespace MAVRIC.UI.Utilies
{
public class UISafeArea : MonoBehaviour
{
public bool ignoreLeft, ignoreRight, ignoreTop, ignoreBottom;
[SerializeField, ReadOnly] private RectTransform rectTransform;
private Rect currentArea;
private void Awake()
{
if (this.rectTransform is null)
{
GetComponents();
}
}
private void OnEnable()
{
UpdateUI(true);
}
private void Update()
{
UpdateUI();
}
private void GetComponents()
{
TryGetComponent(out this.rectTransform);
}
public void UpdateUI(bool forceUpdate = false)
{
UpdateRectSize(Screen.safeArea, forceUpdate);
}
private void UpdateRectSize(Rect area, bool forceUpdate = false)
{
if (this.rectTransform is null) return;
if (forceUpdate is false && currentArea == area) return;
var anchorMin = area.position;
var anchorMax = area.position + area.size;
anchorMin.x /= Screen.width;
anchorMin.y /= Screen.height;
anchorMax.x /= Screen.width;
anchorMax.y /= Screen.height;
if (ignoreLeft)
{
anchorMin.x = 0f;
}
if (ignoreRight)
{
anchorMax.x = 1f;
}
if (ignoreTop)
{
anchorMax.y = 1f;
}
if (ignoreBottom)
{
anchorMin.y = 0f;
}
rectTransform.anchoredPosition = Vector2.zero;
rectTransform.sizeDelta = Vector2.zero;
rectTransform.anchorMin = anchorMin;
rectTransform.anchorMax = anchorMax;
currentArea = area;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 168fc1abe5906ee408df7120f6e4124a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: