Tanoda
UICornerCut.cs
Go to the documentation of this file.
1
18
20{
21 [AddComponentMenu("UI/Extensions/Primitives/Cut Corners")]
23 {
24 public Vector2 cornerSize = new Vector2(16, 16);
25
26 [Header("Corners to cut")]
27 [SerializeField]
28 private bool m_cutUL = true;
29 [SerializeField]
30 private bool m_cutUR;
31 [SerializeField]
32 private bool m_cutLL;
33 [SerializeField]
34 private bool m_cutLR;
35
36 [Tooltip("Up-Down colors become Left-Right colors")]
37 [SerializeField]
38 private bool m_makeColumns;
39
40 [Header("Color the cut bars differently")]
41 [SerializeField]
42 private bool m_useColorUp;
43 [SerializeField]
44 private Color32 m_colorUp;
45 [SerializeField]
46 private bool m_useColorDown;
47 [SerializeField]
48 private Color32 m_colorDown;
49
50 public bool CutUL
51 {
52 get { return m_cutUL; }
53 set { m_cutUL = value; SetAllDirty(); }
54 }
55
56 public bool CutUR
57 {
58 get { return m_cutUR; }
59 set { m_cutUR = value; SetAllDirty(); }
60 }
61
62 public bool CutLL
63 {
64 get { return m_cutLL; }
65 set { m_cutLL = value; SetAllDirty(); }
66 }
67
68 public bool CutLR
69 {
70 get { return m_cutLR; }
71 set { m_cutLR = value; SetAllDirty(); }
72 }
73
74 public bool MakeColumns
75 {
76 get { return m_makeColumns; }
77 set { m_makeColumns = value; SetAllDirty(); }
78 }
79
80 public bool UseColorUp
81 {
82 get { return m_useColorUp; }
83 set { m_useColorUp = value; }
84 }
85
86 public Color32 ColorUp
87 {
88 get { return m_colorUp; }
89 set { m_colorUp = value; }
90 }
91
92 public bool UseColorDown
93 {
94 get { return m_useColorDown; }
95 set { m_useColorDown = value; }
96 }
97
98 public Color32 ColorDown
99 {
100 get { return m_colorDown; }
101 set { m_colorDown = value; }
102 }
103
104 protected override void OnPopulateMesh(VertexHelper vh)
105 {
106 var rect = rectTransform.rect;
107 var rectNew = rect;
108
109 Color32 color32 = color;
110 bool up = m_cutUL | m_cutUR;
111 bool down = m_cutLL | m_cutLR;
112 bool left = m_cutLL | m_cutUL;
113 bool right = m_cutLR | m_cutUR;
114 bool any = up | down;
115
116 if (any && cornerSize.sqrMagnitude > 0)
117 {
118
119 //nibble off the sides
120 vh.Clear();
121 if (left)
122 rectNew.xMin += cornerSize.x;
123 if (down)
124 rectNew.yMin += cornerSize.y;
125 if (up)
126 rectNew.yMax -= cornerSize.y;
127 if (right)
128 rectNew.xMax -= cornerSize.x;
129
130 //add two squares to the main square
131 Vector2 ul, ur, ll, lr;
132
133 if (m_makeColumns)
134 {
135 ul = new Vector2(rect.xMin, m_cutUL ? rectNew.yMax : rect.yMax);
136 ur = new Vector2(rect.xMax, m_cutUR ? rectNew.yMax : rect.yMax);
137 ll = new Vector2(rect.xMin, m_cutLL ? rectNew.yMin : rect.yMin);
138 lr = new Vector2(rect.xMax, m_cutLR ? rectNew.yMin : rect.yMin);
139
140 if (left)
141 AddSquare(
142 ll, ul,
143 new Vector2(rectNew.xMin, rect.yMax),
144 new Vector2(rectNew.xMin, rect.yMin),
145 rect, m_useColorUp ? m_colorUp : color32, vh);
146 if (right)
147 AddSquare(
148 ur, lr,
149 new Vector2(rectNew.xMax, rect.yMin),
150 new Vector2(rectNew.xMax, rect.yMax),
151 rect, m_useColorDown ? m_colorDown : color32, vh);
152 }
153 else
154 {
155 ul = new Vector2(m_cutUL ? rectNew.xMin : rect.xMin, rect.yMax);
156 ur = new Vector2(m_cutUR ? rectNew.xMax : rect.xMax, rect.yMax);
157 ll = new Vector2(m_cutLL ? rectNew.xMin : rect.xMin, rect.yMin);
158 lr = new Vector2(m_cutLR ? rectNew.xMax : rect.xMax, rect.yMin);
159 if (down)
160 AddSquare(
161 lr, ll,
162 new Vector2(rect.xMin, rectNew.yMin),
163 new Vector2(rect.xMax, rectNew.yMin),
164 rect, m_useColorDown ? m_colorDown : color32, vh);
165 if (up)
166 AddSquare(
167 ul, ur,
168 new Vector2(rect.xMax, rectNew.yMax),
169 new Vector2(rect.xMin, rectNew.yMax),
170 rect, m_useColorUp ? m_colorUp : color32, vh);
171 }
172
173 //center
174 if (m_makeColumns)
175 AddSquare(new Rect(rectNew.xMin, rect.yMin, rectNew.width, rect.height), rect, color32, vh);
176 else
177 AddSquare(new Rect(rect.xMin, rectNew.yMin, rect.width, rectNew.height), rect, color32, vh);
178
179 }
180 }
181
182 private static void AddSquare(Rect rect, Rect rectUV, Color32 color32, VertexHelper vh) {
183 int v0 = AddVert(rect.xMin, rect.yMin, rectUV, color32, vh);
184 int v1 = AddVert(rect.xMin, rect.yMax, rectUV, color32, vh);
185 int v2 = AddVert(rect.xMax, rect.yMax, rectUV, color32, vh);
186 int v3 = AddVert(rect.xMax, rect.yMin, rectUV, color32, vh);
187
188 vh.AddTriangle(v0, v1, v2);
189 vh.AddTriangle(v2, v3, v0);
190 }
191
192 private static void AddSquare(Vector2 a, Vector2 b, Vector2 c, Vector2 d, Rect rectUV, Color32 color32, VertexHelper vh) {
193 int v0 = AddVert(a.x, a.y, rectUV, color32, vh);
194 int v1 = AddVert(b.x, b.y, rectUV, color32, vh);
195 int v2 = AddVert(c.x, c.y, rectUV, color32, vh);
196 int v3 = AddVert(d.x, d.y, rectUV, color32, vh);
197
198 vh.AddTriangle(v0, v1, v2);
199 vh.AddTriangle(v2, v3, v0);
200 }
201
210 private static int AddVert(float x, float y, Rect area, Color32 color32, VertexHelper vh) {
211 var uv = new Vector2(
212 Mathf.InverseLerp(area.xMin, area.xMax, x),
213 Mathf.InverseLerp(area.yMin, area.yMax, y)
214 );
215 vh.AddVert(new Vector3(x, y), color32, uv);
216 return vh.currentVertCount - 1;
217 }
218 }
219}
override void OnPopulateMesh(VertexHelper vh)
Definition: UICornerCut.cs:104
Credit Erdener Gonenc - @PixelEnvision.