forked from ChemiKhazi/UnityToolbag
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DrawTitleSafeArea.cs
114 lines (96 loc) · 3.69 KB
/
DrawTitleSafeArea.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using UnityEngine;
namespace UnityToolbag
{
public enum TitleSafeSizeMode
{
Pixels,
Percentage
}
[ExecuteInEditMode]
[AddComponentMenu("UnityToolbag/Draw Title Safe Area")]
public class DrawTitleSafeArea : MonoBehaviour
{
#if UNITY_EDITOR
private Texture2D _blank;
#endif
[SerializeField]
private Color _innerColor = new Color(1, 1, 0, 0.15f);
[SerializeField]
private Color _outerColor = new Color(1, 0, 0, 0.15f);
[SerializeField]
private TitleSafeSizeMode _sizeMode = TitleSafeSizeMode.Percentage;
[SerializeField]
private int _sizeX = 5;
[SerializeField]
private int _sizeY = 5;
#if UNITY_EDITOR
void OnValidate()
{
if (_sizeX < 0) {
_sizeX = 0;
}
if (_sizeY < 0) {
_sizeY = 0;
}
if (_sizeMode == TitleSafeSizeMode.Percentage) {
if (_sizeX > 25) {
_sizeX = 25;
}
if (_sizeY > 25) {
_sizeY = 25;
}
}
}
void OnDestroy()
{
if (_blank)
{
DestroyImmediate(_blank);
_blank = null;
}
}
void OnGUI()
{
var camera = GetComponent<Camera>();
if (!camera) {
return;
}
if (!_blank) {
_blank = new Texture2D(1, 1);
_blank.SetPixel(0, 0, Color.white);
_blank.hideFlags = HideFlags.HideAndDontSave;
}
float w = camera.pixelWidth;
float h = camera.pixelHeight;
// Compute the actual sizes based on the size mode and our sizes
float wMargin = 0;
float hMargin = 0;
switch (_sizeMode) {
case TitleSafeSizeMode.Percentage: {
wMargin = w * (_sizeX / 100.0f);
hMargin = h * (_sizeY / 100.0f);
break;
}
case TitleSafeSizeMode.Pixels: {
// Clamp to 1/4 the screen size so we never overlap the other side
wMargin = Mathf.Clamp(_sizeX, 0, w / 4);
hMargin = Mathf.Clamp(_sizeY, 0, h / 4);
break;
}
}
// Draw the outer region first
GUI.color = _outerColor;
GUI.DrawTexture(new Rect(0, 0, w, hMargin), _blank, ScaleMode.StretchToFill, true);
GUI.DrawTexture(new Rect(0, h - hMargin, w, hMargin), _blank, ScaleMode.StretchToFill, true);
GUI.DrawTexture(new Rect(0, hMargin, wMargin, h - hMargin * 2), _blank, ScaleMode.StretchToFill, true);
GUI.DrawTexture(new Rect(w - wMargin, hMargin, wMargin, h - hMargin * 2), _blank, ScaleMode.StretchToFill, true);
// Then the inner region
GUI.color = _innerColor;
GUI.DrawTexture(new Rect(wMargin, hMargin, w - wMargin * 2, hMargin), _blank, ScaleMode.StretchToFill, true);
GUI.DrawTexture(new Rect(wMargin, h - hMargin * 2, w - wMargin * 2, hMargin), _blank, ScaleMode.StretchToFill, true);
GUI.DrawTexture(new Rect(wMargin, hMargin * 2, wMargin, h - hMargin * 4), _blank, ScaleMode.StretchToFill, true);
GUI.DrawTexture(new Rect(w - wMargin * 2, hMargin * 2, wMargin, h - hMargin * 4), _blank, ScaleMode.StretchToFill, true);
}
#endif // UNITY_EDITOR
}
}