-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathUtilsExtensions.cs
473 lines (368 loc) · 13.2 KB
/
UtilsExtensions.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using UnityEngine;
using UnityEngine.UI;
namespace Assets.Scripts.Utils
{
public static class UtilsExtensions
{
#region Invoke
private static string GetMethodName(Expression<Action> expr)
{
return ((MethodCallExpression)expr.Body).Method.Name;
}
public static void Invoke(this MonoBehaviour monoBehaviour, Expression<Action> expr, float time)
{
if (((MethodCallExpression) expr.Body).Arguments.Count > 0)
throw new ArgumentException($"Can't invoke method '{GetMethodName(expr)}' with parameters.'");
monoBehaviour.Invoke(GetMethodName(expr), time);
}
public static bool IsInvoking(this MonoBehaviour monoBehaviour, Expression<Action> expr)
{
return monoBehaviour.IsInvoking(GetMethodName(expr));
}
public static void CancelInvoke(this MonoBehaviour monoBehaviour, Expression<Action> expr)
{
monoBehaviour.CancelInvoke(GetMethodName(expr));
}
public static void InvokeRepeating(this MonoBehaviour monoBehaviour, Expression<Action> expr, float time, float repeatRate)
{
monoBehaviour.InvokeRepeating(GetMethodName(expr), time, repeatRate);
}
#endregion
#region Vector2
public static Vector2 MakePixelPerfect(this Vector2 position)
{
return new Vector2((int)position.x, (int)position.y);
}
public static Vector2 Rotate(this Vector2 vector, float angle)
{
return Quaternion.AngleAxis(angle, Vector3.forward) * vector;
}
public static float Angle(this Vector2 direction)
{
return direction.y > 0
? Vector2.Angle(new Vector2(1, 0), direction)
: -Vector2.Angle(new Vector2(1, 0), direction);
}
#endregion
#region Color
public static Color SetAlpha(this Color color, float alpha)
{
return new Color(color.r, color.g, color.b, alpha);
}
public static void SetAlpha(this Graphic graphic, float alpha)
{
graphic.color = new Color(graphic.color.r, graphic.color.g, graphic.color.b, alpha);
}
public static void SetAlpha(this SpriteRenderer spriteRenderer, float alpha)
{
spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, alpha);
}
#endregion
#region Transform.position
public static void SetXYZ(this Transform transform, float x, float y, float z)
{
transform.position = new Vector3(x, y, z);
}
public static void SetLocalXYZ(this Transform transform, float x, float y, float z)
{
transform.localPosition = new Vector3(x, y, z);
}
public static void SetXY(this Transform transform, float x, float y)
{
transform.position = new Vector3(x, y, transform.position.z);
}
public static void SetXY(this Transform transform, Vector3 pos)
{
transform.position = new Vector3(pos.x, pos.y, transform.position.z);
}
public static void SetLocalXY(this Transform transform, float x, float y)
{
transform.localPosition = new Vector3(x, y, transform.localPosition.z);
}
public static void SetLocalXY(this Transform transform, Vector3 pos)
{
transform.localPosition = new Vector3(pos.x, pos.y, transform.localPosition.z);
}
public static void SetXZ(this Transform transform, float x, float z)
{
transform.position = new Vector3(x, transform.position.y, z);
}
public static void SetXZ(this Transform transform, Vector3 pos)
{
transform.position = new Vector3(pos.x, transform.position.y, pos.z);
}
public static void SetLocalXZ(this Transform transform, float x, float z)
{
transform.localPosition = new Vector3(x, transform.localPosition.y, z);
}
public static void SetLocalXZ(this Transform transform, Vector3 pos)
{
transform.localPosition = new Vector3(pos.x, transform.localPosition.y, pos.z);
}
public static void SetYZ(this Transform transform, float y, float z)
{
transform.position = new Vector3(transform.position.x, y, z);
}
public static void SetYZ(this Transform transform, Vector3 pos)
{
transform.position = new Vector3(transform.position.x, pos.y, pos.z);
}
public static void SetLocalYZ(this Transform transform, float y, float z)
{
transform.localPosition = new Vector3(transform.localPosition.x, y, z);
}
public static void SetLocalYZ(this Transform transform, Vector3 pos)
{
transform.localPosition = new Vector3(transform.localPosition.x, pos.y, pos.z);
}
public static void SetX(this Transform transform, float x)
{
transform.position = new Vector3(x, transform.position.y, transform.position.z);
}
public static void SetLocalX(this Transform transform, float x)
{
transform.localPosition = new Vector3(x, transform.localPosition.y, transform.localPosition.z);
}
public static void SetY(this Transform transform, float y)
{
transform.position = new Vector3(transform.position.x, y, transform.position.z);
}
public static void SetLocalY(this Transform transform, float y)
{
transform.localPosition = new Vector3(transform.localPosition.x, y, transform.localPosition.z);
}
public static void SetZ(this Transform transform, float z)
{
transform.position = new Vector3(transform.position.x, transform.position.y, z);
}
public static void SetLocalZ(this Transform transform, float z)
{
transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, z);
}
public static void IncX(this Transform transform, float dx)
{
SetX(transform, transform.position.x + dx);
}
public static void IncLocalX(this Transform transform, float dx)
{
SetLocalX(transform, transform.localPosition.x + dx);
}
public static void IncY(this Transform transform, float dy)
{
SetY(transform, transform.position.y + dy);
}
public static void IncLocalY(this Transform transform, float dy)
{
SetLocalY(transform, transform.localPosition.y + dy);
}
public static void IncZ(this Transform transform, float dz)
{
SetZ(transform, transform.position.z + dz);
}
public static void IncLocalZ(this Transform transform, float dz)
{
SetLocalZ(transform, transform.localPosition.z + dz);
}
#endregion
#region Transform.localScale
public static void SetScale(this Transform transform, float scale)
{
transform.localScale = Vector3.one * scale;
}
public static void SetScaleX(this Transform transform, float scaleX)
{
transform.localScale = new Vector3(scaleX, transform.localScale.y, transform.localScale.z);
}
public static void SetScaleY(this Transform transform, float scaleY)
{
transform.localScale = new Vector3(transform.localScale.x, scaleY, transform.localScale.z);
}
public static void SetScaleZ(this Transform transform, float scaleZ)
{
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, scaleZ);
}
#endregion
#region Transform.rotation
public static void SetRotation(this Transform transform, float angle)
{
transform.rotation = new Quaternion();
transform.Rotate(Vector3.forward, angle);
}
#endregion
#region RectTransform
#region Left, Right, Top, Bottom
public static void SetLeft(this RectTransform rectTransform, float left)
{
rectTransform.offsetMin = new Vector2(left, rectTransform.offsetMin.y);
}
public static float GetLeft(this RectTransform rectTransform)
{
return rectTransform.offsetMin.x;
}
public static void SetRight(this RectTransform rectTransform, float right)
{
rectTransform.offsetMax = new Vector2(-right, rectTransform.offsetMax.y);
}
public static float GetRight(this RectTransform rectTransform)
{
return -rectTransform.offsetMax.x;
}
public static void SetTop(this RectTransform rectTransform, float top)
{
rectTransform.offsetMax = new Vector2(rectTransform.offsetMax.x, -top);
}
public static float GetTop(this RectTransform rectTransform)
{
return -rectTransform.offsetMax.y;
}
public static void SetBottom(this RectTransform rectTransform, float bottom)
{
rectTransform.offsetMin = new Vector2(rectTransform.offsetMin.x, bottom);
}
public static float GetBottom(this RectTransform rectTransform)
{
return rectTransform.offsetMin.y;
}
public static void SetLeftTopRightBottom(this RectTransform rectTransform, float left, float top, float right, float bottom)
{
rectTransform.offsetMin = new Vector2(left, bottom);
rectTransform.offsetMax = new Vector2(-right, -top);
}
#endregion
#region PosX, PosY, Width, Height
public static void SetPosX(this RectTransform rectTransform, float posX)
{
rectTransform.anchoredPosition = new Vector2(posX, rectTransform.anchoredPosition.y);
}
public static void SetPosY(this RectTransform rectTransform, float posY)
{
rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, posY);
}
public static void SetPosXY(this RectTransform rectTransform, float posX, float posY)
{
rectTransform.anchoredPosition = new Vector2(posX, posY);
}
public static void SetWidth(this RectTransform rectTransform, float width)
{
rectTransform.sizeDelta = new Vector2(width, rectTransform.sizeDelta.y);
}
public static float GetWidth(this RectTransform rectTransform)
{
return rectTransform.rect.width;
}
public static void SetHeight(this RectTransform rectTransform, float height)
{
rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, height);
}
public static float GetHeight(this RectTransform rectTransform)
{
return rectTransform.rect.height;
}
public static void SetWidthHeight(this RectTransform rectTransform, float width, float height)
{
rectTransform.sizeDelta = new Vector2(width, height);
}
public static void SetPosAndSize(this RectTransform rectTransform, float posX, float posY, float width, float height)
{
rectTransform.anchoredPosition = new Vector2(posX, posY);
rectTransform.sizeDelta = new Vector2(width, height);
}
#endregion
#region Anchor Offset
public static void SetLeftAnchorOffset(this RectTransform rectTransform, float leftPercent)
{
rectTransform.anchorMin = new Vector2(leftPercent, rectTransform.anchorMin.y);
}
public static void SetRightAnchorOffset(this RectTransform rectTransform, float rightPercent)
{
rectTransform.anchorMax = new Vector2(1f - rightPercent, rectTransform.anchorMax.y);
}
public static void SetTopAnchorOffset(this RectTransform rectTransform, float topPercent)
{
rectTransform.anchorMax = new Vector2(rectTransform.anchorMax.x, 1f - topPercent);
}
public static void SetBottomAnchorOffset(this RectTransform rectTransform, float bottomPercent)
{
rectTransform.anchorMin = new Vector2(rectTransform.anchorMin.x, bottomPercent);
}
public static void SetAnchorOffset(this RectTransform rectTransform, float left, float top, float right, float bottom)
{
rectTransform.anchorMin = new Vector2(left, bottom);
rectTransform.anchorMax = new Vector2(1f - right, 1f - top);
}
#endregion
#region World positions
private static readonly Vector3[] _fourCorners = new Vector3[4];//start bottom left and clockwise
public static Vector2 GetWorldCenter(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return new Vector2((_fourCorners[0].x + _fourCorners[3].x) / 2f, (_fourCorners[0].y + _fourCorners[1].y) / 2f);
}
public static float GetWorldLeft(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return _fourCorners[0].x;
}
public static float GetWorldRight(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return _fourCorners[2].x;
}
public static float GetWorldTop(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return _fourCorners[1].y;
}
public static float GetWorldBottom(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return _fourCorners[0].y;
}
public static Vector2 GetWorldTopLeft(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return new Vector2(_fourCorners[0].x, _fourCorners[1].y);
}
public static Vector2 GetWorldTopRight(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return new Vector2(_fourCorners[2].x, _fourCorners[1].y);
}
public static Vector2 GetWorldBottomLeft(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return new Vector2(_fourCorners[0].x, _fourCorners[0].y);
}
public static Vector2 GetWorldBottomRight(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return new Vector2(_fourCorners[2].x, _fourCorners[0].y);
}
public static Rect GetWorldRect(this RectTransform rectTransform)
{
rectTransform.GetWorldCorners(_fourCorners);
return new Rect(_fourCorners[0].x, _fourCorners[0].y, Mathf.Abs(_fourCorners[3].x - _fourCorners[0].x), Mathf.Abs(_fourCorners[1].y - _fourCorners[0].y));
}
#endregion
#endregion
#region ParticleSystem
public static IEnumerator PlayAndWaitForFinish(this ParticleSystem particleSystem)
{
particleSystem.Play();
while (particleSystem.isPlaying)
yield return null;
}
#endregion
#region Dot Net
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (T item in items)
action(item);
}
#endregion
}
}