47 Vector3[] corners =
new Vector3[4];
48 rt.GetWorldCorners(corners);
49 Vector2 size =
new Vector2(corners[2].x - corners[1].x, corners[1].y - corners[0].y);
50 return new Rect(
new Vector2(corners[1].x, -corners[1].y), size);
59 return new MinMax01(rt.anchorMin, rt.anchorMax);
68 rt.anchorMin = anchors.
min;
69 rt.anchorMax = anchors.
max;
76 public static RectTransform
GetParent(
this RectTransform rt)
78 return rt.parent as RectTransform;
85 public static float GetWidth(
this RectTransform rt)
94 public static float GetHeight(
this RectTransform rt)
96 return rt.rect.height;
103 public static Vector2
GetSize(
this RectTransform rt)
105 return new Vector2(rt.GetWidth(), rt.GetHeight());
111 public static void SetWidth(
this RectTransform rt,
float width)
113 rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
119 public static void SetHeight(
this RectTransform rt,
float height)
121 rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
129 public static void SetSize(
this RectTransform rt,
float width,
float height)
131 rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
132 rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
139 public static void SetSize(
this RectTransform rt, Vector2 size)
141 rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x);
142 rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y);
149 public static Vector2
GetLeft(
this RectTransform rt)
151 return new Vector2(rt.offsetMin.x, rt.anchoredPosition.y);
158 public static Vector2
GetRight(
this RectTransform rt)
160 return new Vector2(rt.offsetMax.x, rt.anchoredPosition.y);
167 public static Vector2
GetTop(
this RectTransform rt)
169 return new Vector2(rt.anchoredPosition.x, rt.offsetMax.y);
178 return new Vector2(rt.anchoredPosition.x, rt.offsetMin.y);
214 public static void SetLeft(
this RectTransform rt,
float distance)
216 float xmin = rt.GetParent().rect.xMin;
217 float anchorFactor = rt.anchorMin.x * 2 - 1;
219 rt.offsetMin =
new Vector2(xmin + (xmin * anchorFactor) + distance, rt.offsetMin.y);
225 public static void SetRight(
this RectTransform rt,
float distance)
227 float xmax = rt.GetParent().rect.xMax;
228 float anchorFactor = rt.anchorMax.x * 2 - 1;
230 rt.offsetMax =
new Vector2(xmax - (xmax * anchorFactor) + distance, rt.offsetMax.y);
236 public static void SetTop(
this RectTransform rt,
float top)
238 float ymax = rt.GetParent().rect.yMax;
239 float anchorFactor = rt.anchorMax.y * 2 - 1;
241 rt.offsetMax =
new Vector2(rt.offsetMax.x, ymax - (ymax * anchorFactor) + top);
247 public static void SetBottom(
this RectTransform rt,
float distance)
249 float ymin = rt.GetParent().rect.yMin;
250 float anchorFactor = rt.anchorMin.y * 2 - 1;
252 rt.offsetMin =
new Vector2(rt.offsetMin.x, ymin + (ymin * anchorFactor) + distance);
262 public static void Left(
this RectTransform rt,
float distance)
264 rt.SetLeft(distance);
270 public static void Right(
this RectTransform rt,
float distance)
272 rt.SetRight(-distance);
278 public static void Top(
this RectTransform rt,
float distance)
280 rt.SetTop(-distance);
286 public static void Bottom(
this RectTransform rt,
float distance)
288 rt.SetRight(distance);
299 Vector2 origin = rt.AnchorToParentSpace(anchor.
min - rt.anchorMin);
301 rt.offsetMin =
new Vector2(origin.x + distance, rt.offsetMin.y);
310 Vector2 origin = rt.AnchorToParentSpace(anchor.
max - rt.anchorMax);
312 rt.offsetMax =
new Vector2(origin.x + distance, rt.offsetMax.y);
321 Vector2 origin = rt.AnchorToParentSpace(anchor.
max - rt.anchorMax);
323 rt.offsetMax =
new Vector2(rt.offsetMax.x, origin.y + distance);
332 Vector2 origin = rt.AnchorToParentSpace(anchor.
min - rt.anchorMin);
334 rt.offsetMin =
new Vector2(rt.offsetMin.x, origin.y + distance);
347 rt.offsetMin =
new Vector2(rt.anchoredPosition.x + distance, rt.offsetMin.y);
357 rt.offsetMax =
new Vector2(rt.anchoredPosition.x + distance, rt.offsetMax.y);
367 rt.offsetMax =
new Vector2(rt.offsetMax.x, rt.anchoredPosition.y + distance);
377 rt.offsetMin =
new Vector2(rt.offsetMin.x, rt.anchoredPosition.y + distance);
395 public static void MoveLeft(
this RectTransform rt,
float left = 0)
397 float xmin = rt.GetParent().rect.xMin;
398 float center = rt.anchorMax.x - rt.anchorMin.x;
399 float anchorFactor = rt.anchorMax.x * 2 - 1;
400 rt.anchoredPosition =
new Vector2(xmin + (xmin * anchorFactor) + left - (center * xmin), rt.anchoredPosition.y);
406 public static void MoveRight(
this RectTransform rt,
float right = 0)
408 float xmax = rt.GetParent().rect.xMax;
409 float center = rt.anchorMax.x - rt.anchorMin.x;
410 float anchorFactor = rt.anchorMax.x * 2 - 1;
411 rt.anchoredPosition =
new Vector2(xmax - (xmax * anchorFactor) - right + (center * xmax), rt.anchoredPosition.y);
417 public static void MoveTop(
this RectTransform rt,
float top = 0)
419 float ymax = rt.GetParent().rect.yMax;
420 float center = rt.anchorMax.y - rt.anchorMin.y;
421 float anchorFactor = rt.anchorMax.y * 2 - 1;
422 rt.anchoredPosition =
new Vector2(rt.anchoredPosition.x, ymax - (ymax * anchorFactor) - top + (center * ymax));
428 public static void MoveBottom(
this RectTransform rt,
float bottom = 0)
430 float ymin = rt.GetParent().rect.yMin;
431 float center = rt.anchorMax.y - rt.anchorMin.y;
432 float anchorFactor = rt.anchorMax.y * 2 - 1;
433 rt.anchoredPosition =
new Vector2(rt.anchoredPosition.x, ymin + (ymin * anchorFactor) + bottom - (center * ymin));
454 rt.MoveLeft(distance + rt.GetWidth() / 2);
462 rt.MoveRight(distance + rt.GetWidth() / 2);
470 rt.MoveTop(distance + rt.GetHeight() / 2);
478 rt.MoveBottom(distance + rt.GetHeight() / 2);
499 rt.MoveLeft(distance - rt.GetWidth() / 2);
507 rt.MoveRight(distance - rt.GetWidth() / 2);
515 rt.MoveTop(distance - rt.GetHeight() / 2);
523 rt.MoveBottom(distance - rt.GetHeight() / 2);
532 public static void Move(
this RectTransform rt,
float x,
float y)
541 public static void Move(
this RectTransform rt, Vector2 point)
543 rt.MoveLeft(point.x);
544 rt.MoveBottom(point.y);
554 public static void MoveInside(
this RectTransform rt,
float x,
float y)
556 rt.MoveLeftInside(x);
557 rt.MoveBottomInside(y);
564 public static void MoveInside(
this RectTransform rt, Vector2 point)
566 rt.MoveLeftInside(point.x);
567 rt.MoveBottomInside(point.y);
577 public static void MoveOutside(
this RectTransform rt,
float x,
float y)
579 rt.MoveLeftOutside(x);
580 rt.MoveBottomOutside(y);
587 public static void MoveOutside(
this RectTransform rt, Vector2 point)
589 rt.MoveLeftOutside(point.x);
590 rt.MoveBottomOutside(point.y);
599 rt.MoveFrom(anchor, point.x, point.y);
607 Vector2 origin = rt.AnchorToParentSpace(
AnchorOrigin(anchor) - rt.AnchorOrigin());
608 rt.anchoredPosition =
new Vector2(origin.x + x, origin.y + y);
619 return rt.ParentToChildSpace(point.x, point.y);
629 float xmin = rt.GetParent().rect.xMin;
630 float ymin = rt.GetParent().rect.yMin;
631 float anchorFactorX = rt.anchorMin.x * 2 - 1;
632 float anchorFactorY = rt.anchorMin.y * 2 - 1;
633 return new Vector2(xmin + (xmin * anchorFactorX) + x, ymin + (ymin * anchorFactorY) + y);
645 return rt.AnchorOriginParent() +
new Vector2(x, y);
654 return rt.AnchorOriginParent() + point;
665 return rt.ParentToAnchorSpace(point.x, point.y);
675 Rect parent = rt.GetParent().rect;
676 if (parent.width != 0)
681 if (parent.height != 0)
686 return new Vector2(x, y);
698 return new Vector2(x * rt.GetParent().rect.width, y * rt.GetParent().rect.height);
707 return new Vector2(point.x * rt.GetParent().rect.width, point.y * rt.GetParent().rect.height);
726 float x = anchor.
min.x + (anchor.
max.x - anchor.
min.x) / 2;
727 float y = anchor.
min.y + (anchor.
max.y - anchor.
min.y) / 2;
729 return new Vector2(x, y);
738 return Vector2.Scale(rt.AnchorOrigin(),
new Vector2(rt.GetParent().rect.width, rt.GetParent().rect.height));
747 Canvas rootCanvas = rt.GetComponentInParent<Canvas>();
749 while (!rootCanvas.isRootCanvas)
750 rootCanvas = rootCanvas.transform.parent.GetComponentInParent<Canvas>();
759 public static void SetProperties(
this RectTransform rectTrans, RectTransform properties)
761 rectTrans.SetProperties(properties.anchoredPosition, properties.anchoredPosition3D, properties.anchorMax,
762 properties.anchorMin, properties.offsetMax, properties.offsetMin, properties.pivot, properties.sizeDelta, properties.transform);
777 public static void SetProperties(
this RectTransform rectTrans, Vector2 anchoredPosition, Vector3 anchoredPosition3D,
778 Vector2 anchorMax, Vector2 anchorMin, Vector2 offsetMax, Vector2 offsetMin, Vector2 pivot, Vector2 sizeDelta, Transform trans =
null)
780 rectTrans.anchoredPosition = anchoredPosition;
781 rectTrans.anchoredPosition3D = anchoredPosition3D;
782 rectTrans.anchorMax = anchorMax;
783 rectTrans.anchorMin = anchorMin;
784 rectTrans.offsetMax = offsetMax;
785 rectTrans.offsetMin = offsetMin;
786 rectTrans.pivot = pivot;
787 rectTrans.sizeDelta = sizeDelta;
788 if (trans) rectTrans.transform.SetProperties(trans);
795 public static void SetLerp(
this RectTransform
self, RectTransform a, RectTransform b,
float t)
798 Vector2.Lerp(a.anchoredPosition, b.anchoredPosition, t),
799 Vector3.Lerp(a.anchoredPosition3D, b.anchoredPosition3D, t),
800 Vector2.Lerp(a.anchorMax, b.anchorMax, t),
801 Vector2.Lerp(a.anchorMin, b.anchorMin, t),
802 Vector2.Lerp(a.offsetMax, b.offsetMax, t),
803 Vector2.Lerp(a.offsetMin, b.offsetMin, t),
804 Vector2.Lerp(a.pivot, b.pivot, t),
805 Vector2.Lerp(a.sizeDelta, b.sizeDelta, t)
807 self.transform.SetLerp(a.transform, b.transform, t);
Class containing a minimum and max Vectors with its components between 0 and 1
Extensions for the RectTransform component
static void Move(this RectTransform rt, float x, float y)
Moves the RectTransform to the given point in parent space, considering (0, 0) to be the parent's low...
static void SetRightFrom(this RectTransform rt, MinMax01 anchor, float distance)
Repositions the requested edge relative to the passed anchor.
static void SetWidth(this RectTransform rt, float width)
Sets the width of the RectTransform keeping the current anchors.
static MinMax01 GetAnchors(this RectTransform rt)
The normalized position in the parent RectTransform that the corners is anchored to.
static void MoveLeft(this RectTransform rt, float left=0)
Sets the position of the RectTransform relative to the parent's Left side, regardless of anchor setti...
static float GetHeight(this RectTransform rt)
The height of the RectTransform, measured from the Y position.
static void MoveFrom(this RectTransform rt, MinMax01 anchor, float x, float y)
Moves the RectTransform relative to an arbitrary anchor point. This is effectively like setting the a...
static void SetLerp(this RectTransform self, RectTransform a, RectTransform b, float t)
Linearly interpolates between two RectTransforms.
static float GetWidth(this RectTransform rt)
The width of the RectTransform, measured from the X position.
static void Right(this RectTransform rt, float distance)
Moves the edge to a distance towards the center from the same edge of the parent.
static void MoveRightOutside(this RectTransform rt, float distance=0)
Moves the RectTransform to align the left edge with the parent right edge.
static void SetTopFrom(this RectTransform rt, MinMax01 anchor, float distance)
Repositions the requested edge relative to the passed anchor.
static Vector2 AnchorOrigin(this RectTransform rt)
The center of the rectangle the two anchors represent, which is the origin that a RectTransform's anc...
static void SetTop(this RectTransform rt, float top)
Sets the distance of the distance from the top edge to the parent's top edge regardless of the anchor...
static void MoveOutside(this RectTransform rt, float x, float y)
Moves the RectTransform relative to the parent's lower-left corner, respecting the RT's width and hei...
static void SetLeftFrom(this RectTransform rt, MinMax01 anchor, float distance)
Repositions the requested edge relative to the passed anchor.
static Vector2 ChildToParentSpace(this RectTransform rt, float x, float y)
Translates a point (presumably the RectTransform's anchoredPosition) into the same point on the paren...
static void MoveTopOutside(this RectTransform rt, float distance=0)
Moves the RectTransform to align the bottom edge with the parent top edge.
static Vector2 ChildToParentSpace(this RectTransform rt, Vector2 point)
Translates a point (presumably the RectTransform's anchoredPosition) into the same point on the paren...
static void SetProperties(this RectTransform rectTrans, RectTransform properties)
Sets the RectTransform position, rotation and scale to the same values in the given RectTransform.
static Vector2 GetLeft(this RectTransform rt)
The center of the left edge.
static void SetAnchors(this RectTransform rt, MinMax01 anchors)
Sets the normalized position in the parent RectTransform that the corners is anchored to.
static void Left(this RectTransform rt, float distance)
Moves the edge to a distance towards the center from the same edge of the parent.
static void SetRelativeLeft(this RectTransform rt, float distance)
Moves the edge to the requested position relative to the current position.
static Vector2 AnchorOriginParent(this RectTransform rt)
Translates a RectTransform's anchor origin into Parent space.
static void MoveFrom(this RectTransform rt, MinMax01 anchor, Vector2 point)
Moves the RectTransform relative to an arbitrary anchor point. This is effectively like setting the a...
static void SetLeft(this RectTransform rt, float distance)
Sets the distance of the distance from the left edge to the parent's left edge regardless of the anch...
static void SetHeight(this RectTransform rt, float height)
Sets the height of the RectTransform keeping the current anchors.
static void MoveLeftOutside(this RectTransform rt, float distance=0)
Moves the RectTransform to align the right edge with the parent left edge.
static void MoveTopInside(this RectTransform rt, float distance=0)
Moves the RectTransform to align the child left edge with the parent left top.
static void SetBottomFrom(this RectTransform rt, MinMax01 anchor, float distance)
Repositions the requested edge relative to the passed anchor.
static void Top(this RectTransform rt, float distance)
Moves the edge to a distance towards the center from the same edge of the parent.
static void SetBottom(this RectTransform rt, float distance)
Sets the distance of the distance from the bottom edge to the parent's bottom edge regardless of the ...
static Vector2 ParentToChildSpace(this RectTransform rt, float x, float y)
Translates a point on the parent's frame of reference, with (0, 0) being the parent's lower-left hand...
static void SetRelativeTop(this RectTransform rt, float distance)
Moves the edge to the requested position relative to the current position.
static void Bottom(this RectTransform rt, float distance)
Moves the edge to a distance towards the center from the same edge of the parent.
static void SetRelativeRight(this RectTransform rt, float distance)
Moves the edge to the requested position relative to the current position.
static Vector2 GetRight(this RectTransform rt)
The center of the right edge.
static RectTransform GetParent(this RectTransform rt)
The RecTransform parent of the RectTransform.
static void MoveRightInside(this RectTransform rt, float distance=0)
Moves the RectTransform to align the child left edge with the parent right edge.
static Vector2 ParentToAnchorSpace(this RectTransform rt, float x, float y)
Normalizes a point associated with the parent object into "Anchor Space", which is to say,...
static void MoveInside(this RectTransform rt, float x, float y)
Moves the RectTransform relative to the parent's lower-le1ft corner, respecting the RT's width and he...
static Vector2 AnchorToParentSpace(this RectTransform rt, float x, float y)
Translates a normalized "Anchor Space" coordinate into a real point on the parent's reference system.
static void MoveBottom(this RectTransform rt, float bottom=0)
Sets the position of the RectTransform relative to the parent's Bottom side, regardless of anchor set...
static Vector2 GetBottom(this RectTransform rt)
The center of the bottom edge.
static Vector2 AnchorOrigin(MinMax01 anchor)
The center of the rectangle the two anchors represent, which is the origin that a RectTransform's anc...
static void MoveOutside(this RectTransform rt, Vector2 point)
Moves the RectTransform relative to the parent's lower-left corner, respecting the RT's width and hei...
static void MoveInside(this RectTransform rt, Vector2 point)
Moves the RectTransform relative to the parent's lower-le1ft corner, respecting the RT's width and he...
static void SetRight(this RectTransform rt, float distance)
Sets the distance of the distance from the left edge to the parent's left edge regardless of the anch...
static void MoveBottomInside(this RectTransform rt, float distance=0)
Moves the RectTransform to align the child left edge with the parent bottom edge.
static Vector2 GetSize(this RectTransform rt)
The width and the height of the RectTransform, measured from the (X,Y) position.
static void SetRelativeBottom(this RectTransform rt, float distance)
Moves the edge to the requested position relative to the current position.
static void MoveLeftInside(this RectTransform rt, float distance=0)
Moves the RectTransform to align the child left edge with the parent left edge.
static void SetSize(this RectTransform rt, float width, float height)
Sets the width and the height of the RectTransform keeping the current anchors.
static void MoveRight(this RectTransform rt, float right=0)
Sets the position of the RectTransform relative to the parent's Right side, regardless of anchor sett...
static void MoveTop(this RectTransform rt, float top=0)
Sets the position of the RectTransform relative to the parent's Top side, regardless of anchor settin...
static Vector2 GetTop(this RectTransform rt)
The center of the top edge.
static Rect GetWorldRect(this RectTransform rt)
Get the corners of the RectTransform in world space.
static Vector2 AnchorToParentSpace(this RectTransform rt, Vector2 point)
Translates a normalized "Anchor Space" coordinate into a real point on the parent's reference system.
static Vector2 ParentToAnchorSpace(this RectTransform rt, Vector2 point)
Normalizes a point associated with the parent object into "Anchor Space", which is to say,...
static void SetProperties(this RectTransform rectTrans, Vector2 anchoredPosition, Vector3 anchoredPosition3D, Vector2 anchorMax, Vector2 anchorMin, Vector2 offsetMax, Vector2 offsetMin, Vector2 pivot, Vector2 sizeDelta, Transform trans=null)
Sets the transform properties to the given values.
static void MoveBottomOutside(this RectTransform rt, float distance=0)
Moves the RectTransform to align the top edge with the parent bottom edge.
static void Move(this RectTransform rt, Vector2 point)
Moves the RectTransform to the given point in parent space, considering (0, 0) to be the parent's low...
static void SetSize(this RectTransform rt, Vector2 size)
Sets the width and the height of the RectTransform keeping the current anchors.
static Vector2 ParentToChildSpace(this RectTransform rt, Vector2 point)
Translates a point on the parent's frame of reference, with (0, 0) being the parent's lower-left hand...
static Canvas GetRootCanvas(this RectTransform rt)
Returns the top-most-level canvas that this RectTransform is a child of.