-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnity_UI-RespectMobileSafeArea
More file actions
43 lines (35 loc) · 1.08 KB
/
Copy pathUnity_UI-RespectMobileSafeArea
File metadata and controls
43 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//reference: https://youtu.be/VprqsEsFb5w
//adapted and optimized by me: https://github.com/Luanrobs
//simple and elegant script that has the sole purpose of making the child objects respect the safeArea of each cell model
//it is possible to better visualize it in action using simulator, especially useful in models with notch
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(RectTransform))]
public class SafeArea : MonoBehaviour
{
[SerializeField][HideInInspector] RectTransform reactTransform;
Rect safeArea;
Vector2 minAnchor;
Vector2 maxAnchor;
private IEnumerator Start()
{
yield return new WaitForEndOfFrame();
AdjustSafeArea();
}
private void Reset()
{
reactTransform = this.GetComponent<RectTransform>();
}
public void AdjustSafeArea()
{
safeArea = Screen.safeArea;
minAnchor = safeArea.position;
maxAnchor = minAnchor + safeArea.size;
minAnchor.x /= Screen.width;
minAnchor.y /= Screen.height;
maxAnchor.x /= Screen.width;
maxAnchor.y /= Screen.height;
reactTransform.anchorMin = minAnchor;
reactTransform.anchorMax = maxAnchor;
}
}