Skip to content

Commit 7d1a350

Browse files
committed
Basic Rect-related extensions
1 parent e76c960 commit 7d1a350

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using UnityEngine;
2+
3+
namespace Toolbox
4+
{
5+
public static class RectExtensions
6+
{
7+
public static Rect AlignRight(this Rect rect, float newWidth)
8+
{
9+
rect.xMin = rect.xMax - newWidth;
10+
return rect;
11+
}
12+
13+
public static Rect AlignLeft(this Rect rect, float newWidth)
14+
{
15+
rect.xMax = rect.xMin + newWidth;
16+
return rect;
17+
}
18+
19+
public static Rect AlignButton(this Rect rect, float newHeight)
20+
{
21+
rect.yMin = rect.yMax - newHeight;
22+
return rect;
23+
}
24+
25+
public static Rect AlignTop(this Rect rect, float newHeight)
26+
{
27+
rect.yMax = rect.yMin + newHeight;
28+
return rect;
29+
}
30+
31+
public static Rect AlignCenterX(this Rect rect, float newWidth)
32+
{
33+
var offset = (rect.width - newWidth) / 2;
34+
rect.xMin += offset;
35+
rect.xMax -= offset;
36+
return rect;
37+
}
38+
39+
public static Rect AlignCenterY(this Rect rect, float newHeight)
40+
{
41+
var offset = (rect.width - newHeight) / 2;
42+
rect.yMin += offset;
43+
rect.yMax -= offset;
44+
return rect;
45+
}
46+
47+
public static Rect MoveByX(this Rect rect, float offset, float spacing = 0.0f)
48+
{
49+
rect.x += offset + spacing;
50+
return rect;
51+
}
52+
53+
public static Rect MoveByY(this Rect rect, float offset, float spacing = 0.0f)
54+
{
55+
rect.y += offset + spacing;
56+
return rect;
57+
}
58+
59+
public static Rect AddMax(this Rect rect, Vector2 range)
60+
{
61+
rect.xMax += range.x;
62+
rect.yMin += range.y;
63+
return rect;
64+
}
65+
66+
public static Rect AddMin(this Rect rect, Vector2 range)
67+
{
68+
rect.xMin += range.x;
69+
rect.yMin += range.y;
70+
return rect;
71+
}
72+
}
73+
}

Assets/Editor Toolbox/Runtime/Extensions/RectExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)