I have a UI Canvas with render mode world space set. For all UI elements that belong to this canvas, I am seeing 'left', 'right', 'top' and 'bottom' variables in the RectTransform component in the editor. Any ways to access these variables via code?
The Anchored Position is the position of the pivot of the RectTransform taking into consideration the anchor reference point. The anchor reference point is the position of the anchors. If the anchors are not together, Unity estimates the four anchor positions using the pivot placement as a reference.
click the highest level 3 dots for the whole window and select "Debug" You can now play with the object that has the RectTransform and see how the member variables in the debug window (which are the same variables accessible in script) show your changes.
RectTransforms are used for GUI but can also be used for other things. It's used to store and manipulate the position, size, and anchoring of a rectangle and supports various forms of scaling based on a parent RectTransform.
Those would be
RectTransform rectTransform; /*Left*/ rectTransform.offsetMin.x; /*Right*/ rectTransform.offsetMax.x; /*Top*/ rectTransform.offsetMax.y; /*Bottom*/ rectTransform.offsetMin.y;
RectTransform rt = GetComponent<RectTransform>(); float left = rt.offsetMin.x; float right = -rt.offsetMax.x; float top = -rt.offsetMax.y; float bottom = rt.offsetMin.y;
TL;DR
From the values displayed in the Inspector, my analysis is that Left
, Right
, Top
and Bottom
are positive if the corresponding bounds are in the rectangle shaped by the anchors of the RectTransform
.
offsetMin.x
as Left
and offsetMin.y
as Bottom
always fulfill this assessment however offsetMax.x
as Right
and offsetMax.y
as Top
does not.
I simply took the opposite values of offsetMax
to make it compliant (basic space modification).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With