Skip to content

Commit a10851d

Browse files
committed
Add extension methods for RectTransform
1 parent b4af80c commit a10851d

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.UI
4+
{
5+
/// <summary>
6+
/// Extension methods for RectTransform.
7+
/// </summary>
8+
public static class RectTransformExtensions
9+
{
10+
/// <summary>
11+
/// Sets the width of the rect transform to the given value.
12+
/// </summary>
13+
/// <param name="rect">The rect transform to update.</param>
14+
/// <param name="width">The width value to set.</param>
15+
public static void SetWidth(this RectTransform rect, float width) =>
16+
rect.sizeDelta = new Vector2(width, rect.sizeDelta.y);
17+
18+
/// <summary>
19+
/// Sets the height of the rect transform to the given value.
20+
/// </summary>
21+
/// <param name="rect">The rect transform to update.</param>
22+
/// <param name="height">The height value to set.</param>
23+
public static void SetHeight(this RectTransform rect, float height) =>
24+
rect.sizeDelta = new Vector2(rect.sizeDelta.x, height);
25+
26+
/// <summary>
27+
/// Sets the left offset of the rect transform to the given value.
28+
/// </summary>
29+
/// <param name="rect">The rect transform to update.</param>
30+
/// <param name="left">The left offset value to set.</param>
31+
public static void SetLeft(this RectTransform rect, float left) =>
32+
rect.offsetMin = new Vector2(left, rect.offsetMin.y);
33+
34+
/// <summary>
35+
/// Sets the right offset of the rect transform to the given value.
36+
/// </summary>
37+
/// <param name="rect">The rect transform to update.</param>
38+
/// <param name="right">The right offset value to set.</param>
39+
public static void SetRight(this RectTransform rect, float right) =>
40+
rect.offsetMax = new Vector2(-right, rect.offsetMax.y);
41+
42+
/// <summary>
43+
/// Sets the top offset of the rect transform to the given value.
44+
/// </summary>
45+
/// <param name="rect">The rect transform to update.</param>
46+
/// <param name="top">The top offset value to set.</param>
47+
public static void SetTop(this RectTransform rect, float top) =>
48+
rect.offsetMax = new Vector2(rect.offsetMax.x, -top);
49+
50+
/// <summary>
51+
/// Sets the bottom offset of the rect transform to the given value.
52+
/// </summary>
53+
/// <param name="rect">The rect transform to update.</param>
54+
/// <param name="bottom">The bottom offset value to set.</param>
55+
public static void SetBottom(this RectTransform rect, float bottom) =>
56+
rect.offsetMin = new Vector2(rect.offsetMin.x, bottom);
57+
58+
/// <summary>
59+
/// Sets the left anchor of the rect transform to the given value.
60+
/// </summary>
61+
/// <param name="rect">The rect transform to update.</param>
62+
/// <param name="minX">The left anchor value to set.</param>
63+
public static void SetAnchorMinX(this RectTransform rect, float minX) =>
64+
rect.anchorMin = new Vector2(minX, rect.anchorMin.y);
65+
66+
/// <summary>
67+
/// Sets the bottom anchor of the rect transform to the given value.
68+
/// </summary>
69+
/// <param name="rect">The rect transform to update.</param>
70+
/// <param name="minY">The bottom anchor value to set.</param>
71+
public static void SetAnchorMinY(this RectTransform rect, float minY) =>
72+
rect.anchorMin = new Vector2(rect.anchorMin.x, minY);
73+
74+
/// <summary>
75+
/// Sets the right anchor of the rect transform to the given value.
76+
/// </summary>
77+
/// <param name="rect">The rect transform to update.</param>
78+
/// <param name="maxX">The right anchor value to set.</param>
79+
public static void SetAnchorMaxX(this RectTransform rect, float maxX) =>
80+
rect.anchorMax = new Vector2(maxX, rect.anchorMax.y);
81+
82+
/// <summary>
83+
/// Sets the top anchor of the rect transform to the given value.
84+
/// </summary>
85+
/// <param name="rect">The rect transform to update.</param>
86+
/// <param name="maxY">The top anchor value to set.</param>
87+
public static void SetAnchorMaxY(this RectTransform rect, float maxY) =>
88+
rect.anchorMax = new Vector2(rect.anchorMax.x, maxY);
89+
90+
}
91+
92+
}

Runtime/Extensions/RectTransformExtensions.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)