Skip to content

Commit bb248f8

Browse files
committed
Add GetWorldRect extension method
1 parent c34a0c9 commit bb248f8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Runtime/Extensions/RectTransformExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ namespace Zigurous.UI
77
/// </summary>
88
public static class RectTransformExtensions
99
{
10+
private static Vector3[] corners = new Vector3[4];
11+
12+
/// <summary>
13+
/// Calculates the world-space rect of the transform.
14+
/// <summary>
15+
/// <param name="transform">The transform to calculate the world rect of.</param>
16+
/// <param name="ignoreScale">Ignores the scale of the transform when setting the rect size (default=false).</param>
17+
/// <returns>The world rect of the transform.</returns>
18+
public static Rect GetWorldRect(this RectTransform transform, bool ignoreScale = false)
19+
{
20+
transform.GetWorldCorners(corners);
21+
22+
Vector3 topLeft = corners[0];
23+
Vector3 size = transform.rect.size;
24+
25+
if (!ignoreScale) {
26+
size.Scale(transform.lossyScale);
27+
}
28+
29+
return new Rect(topLeft, size);
30+
}
31+
1032
/// <summary>
1133
/// Sets the width of the rect transform to the given value.
1234
/// </summary>

0 commit comments

Comments
 (0)