mirror of
https://udrimavric.com/MAVRIC/Stratasys-Fortus-450mc.git
synced 2025-01-22 07:09:08 -05:00
Ported the UISafeArea component from the netcode sample project
This commit is contained in:
parent
1bfdaa4316
commit
31a46d107c
8
Assets/Scripts/UI.meta
Normal file
8
Assets/Scripts/UI.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 185abe3ead7ed39458ac3c3db107c922
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
3
Assets/Scripts/UI/Utilities.meta
Normal file
3
Assets/Scripts/UI/Utilities.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e46dbb60c9a4ccca21c3c40e02dec39
|
||||
timeCreated: 1690370576
|
86
Assets/Scripts/UI/Utilities/UISafeArea.cs
Normal file
86
Assets/Scripts/UI/Utilities/UISafeArea.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/UI/Utilities/UISafeArea.cs.meta
Normal file
11
Assets/Scripts/UI/Utilities/UISafeArea.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 168fc1abe5906ee408df7120f6e4124a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user