Transcript of Rect Transform - Unity Official Tutorials
Resource: A formatted transcript of Unity3D's video tutorial Rect Transform - Unity Official Tutorials. The Unity team packed a lot of information in 14 minutes.
(00:00) Introduction to the Rect Transform
Editing in Global and Local Space
(04:03) Position and Size
(07:19) Resize Relative to the Parent
(08:58) Left, Right, Top, and Bottom
(10:58) Pivot, Rotation, and Scale
(13:31) Values Driven by Another UI Component
Introduction to the Rect Transform
(00:00) The Rect Transform is a new component that replaces the usual Transform component on all new UI elements. Whereas the Transform component represented the position, rotation, and scale of a 3D object in the scene volume, the Rect Transform represents a 2D rectangle specified by a width and height related to a pivot point. Even though the Rect Transform represents a 2D rectangle, the component does include a position along the z-axis as well as rotation and scale, so the element can be manipulated in the scene volume as a 3D object.
One important difference between a Rect Transform and the traditional Transform component is the concept of anchors. A Rect Transform can be anchored to its parent if that parent also has a Rect Transform component. Anchoring allows the UI element to move or stretch based on the position and size of the parent UI element's Rect Transform.
All UI elements must ultimately be a child of a canvas. Canvases themselves have a Rect Transform, so in almost all circumstances the parent of UI element will also have a Rect Transform and UI element will be anchored in some way. See more [transcribed] information on the canvas here.
(01:36) In the Scene view, it is best to use the Rect Tool to manipulate a UI element. The Rect Tool can be selected by clicking on the Rect Tool button in the toolbar or by using the shortcut key 'T'. With the Rect tool selected, it is possible to move, resize, and rotate any UI element with this one tool.
Using the Rect Tool on the selected UI element, it can be moved by clicking anywhere within the rectangle and dragging.
The UI element can be resized by clicking on any of the edges or corners and dragging. The cursor changes when hovering over an edge or corner from the usual pointer to a double-arrow aligned with the direction the UI element will resize when dragged. Holding the Shift key will constrain the UI element to be resized proportionately.
The UI element can be rotated by clicking near any of the corners and dragging outside of the rectangle. The cursor will change to indicate when the tool can rotate the UI element.
Resizing or rotating a UI element is done in relation to its pivot point. The pivot point is indicated by a blue ring, which by default starts in the centre of the UI element. The pivot point can be moved by clicking and dragging on the pivot point indicator in the scene view. The pivot point does not need to be inside the rectangle of the UI element. Placing the pivot point outside of the UI element's rectangle can be done for specialized behavour.
Editing in Global and Local Space
Another difference between the Transform component and the Rect Transform is the effective global and local space on editing. When editing UI elements in local space, the rectangle and handles are aligned with the UI element. In global space, the rectangles and handles are aligned globally and the rectangle indicates the bounds of the uI element. This is most notable when the UI element is rotated.
(04:03) When looking at the Rect Transform in the Inspector, it is clearly different from the Transform component. The Position and Size of the UI element are shown in the upper right of the inspector for the Rect Transform component.
Positions X and Y show the distances in pixels from the anchor point to the pivot along the x- and y-axis respectively. The Position Z is the distance along the local z-axis and is often left at a value of 0.
Size fields Width and Height indicate the dimensions of the UI element in pixels.
The Position and Size fields are dynamic and context-sensitive. They relate directly to the position of the anchors.
(04:46) The Rect Transform component includes a powerful anchoring mechanism. Rect transforms can be anchored to their parent if the parent game object also has a Rect Transform component. In most cases, every UI element will be anchored to its parent in some way.
In the scene view, the anchors are represented by four small triangles. By default, these are grouped together as a single anchor point in the centre of the UI element's parent Rect Transform but can be ungrouped by moving them. Each triangle represents one anchor and each anchor is associated with one corner of the rect transform. The upper left anchor is associated with the upper left corner on the parent's Rect Transform. This is the same for each anchor and each corner. Anchors are positioned within the bounds of the parent's rectangle, not the UI element's rectangle. Anchors cannot be positioned outside of the bounds of the parent's rectangle.
Anchors have a fixed relationship to their UI element's Rect Transforms, which they are anchoring. Anchors have a flexible relationship to their parent's Rect Transform, which they are anchoring to.
An individual anchor can be moved by clicking and dragging on its triangle gizmo. Clicking in the centre of a group of triangles will move the triangles as a group.
When setting an anchor, the distance between the anchor and the associated corner of the element's Rect Transform is a fixed value. The anchor's position within the parent's Rect Transform however is relative to the parent's size and is affected by changes to the size in the parent's Rect Transform. The anchor's position relative to the parent is a precentage of the parent's size along the x- and y-axis and its absolute position can change when the parent's Rect Transform changes. This percentage is represented by a normalised value from 0 to 1 along each axis where (0, 0) is the lower left of the parent's Rect Transform and (1, 1) is the upper right.
(06:56) Practically, this means if all four anchors are grouped together around a single point, the UI element will be anchored to that point. If the parent Rect Transform changes in size, the UI element will move along with the anchor. The UI element will not stretch or resize, as the distance from the anchors to their associated Rect Transform corners is fixed.
Resize Relative to the Parent
(07:19) If however, the anchors are separated in any way, the UI element will stretch along with the parent, as the anchors and the space between them are relative to the parent's Rect Transform and will change in size. How much the UI element stretches compared to its parent depends on the placement of the anchors. A few common cases are:
positioning the anchors on the corners of the UI element, in which case the UI element stretches to maintain the same percentage area of the element.
positioning the anchors at the bounds of the parent, in which case the space between the edge of the UI element and the bounds of the parent will remain the same, creating a fixed border.
positioning a pair of anchors grouped together at the edges of the parent's Rect Transform, in which case the UI element will stretch only on one axis (ex. left anchors on parent's left bound and right anchors on parent's right bound will set UI element to stretch horizontally but not vertically).
The stretching behaviour of an anchored UI element can be tested by grabbing the edge of the parent's rectangle and dragging it. The preview label appears when dragging and the parent's rectangle snaps back to its original state when the dragging ends.
To make editing easier when positioning anchors by hand, holding the Shift key will change the size of the UI element as well.
Left, Right, Top, and Bottom
(08:58) It is important to note a change in the way the Rect Transform displays the Position and Size information when the anchors are not grouped together and no longer define a single point as an anchor.
When all four anchors are grouped together, they define a single anchor point and the Rect Transform displays the positional offset from the pivot as well as the width and height of the UI element.
When the anchors are split apart, the Rect Transform displays the fixed values of the Left, Right, Top, and Bottom. These are the fixed offsets along the x- and y-axis of the anchors to their associated corners.
If any two anchors are grouped together there will be no stretching along that axis and the Rect Transform will continue to display the Position and Width/Height for that axis.
(09:52) When setting the pivot and anchors for a UI and element there are a number of common configurations. To make setting these quick and easy, the Rect Transform includes the Anchor Presets window. This can be accessed by clicking on the Anchor Presets button.
The Anchor Presets button displays the current anchoring choice of the Rect Transform. In the Anchor Presets window, the common configurations are arranged in a matrix based on the position of the anchors, with fixed anchor points in the middle and stretch options along the outer edge. The anchor position can be set by clicking on the desired configuration.
Additionally, the pivot and/or the position of the UI element can be set at the same time as the anchors by holding down the Shift and/or Alt keys when clicking on the desired configuration. These optional keys are noted at the top of the Anchor Presets window.
Pivot, Rotation, and Scale
(10:58) Below the Anchor section in the Inspector, the Pivot fields define the pivot point of the UI element using a normalised value of 0 to 1 along the x- and y-axis, with (0, 0) in the lower left and (1, 1) in the upper right.
(11:10) The Rotation fields define rotation of the UI element. In the most common of cases, rotation will only need to be done along the z-axis, keeping the UI element in 2D splace. Rotation is allowed on all three axes however, and the element can be viewed in 3D space.
(11:28) Before discussing Scale, it is important to understand the difference between scaling and size.
When adjusting a UI element's size in the scene view or adjusting the Width and Height in the Inspector, the UI element is being resized not scaled. Resizing changes the Rect Transform's width and/or height but does not scale the UI element; this will not affect the font-sizes, the border on sliced images, or other items related to the UI element.
When the UI element is scaled the entire element is scaled, including items like fonts and borders.
For layout purposes, it is generally recommended to resize a UI element rather than scale it. Scaling can be useful for animated affects or other special purposes.
A UI element will become invisible if given a negative size. Negative scaling however is supported and can be used for effects, including flipping the UI element with a scale of -1.
(12:35) The Rect Transform component includes two optional editing modes, Blueprint mode and Raw Edit mode.
Blueprint mode allows for the editing of Rect Transforms as if they were not rotated or scaled. The UI element cannot be rotated with the Rect Tool when on Blueprint mode.
Normally when editing the Pivot or Anchor values from within the Rect Transform, the Pivot or Anchor will move and the UI element will remain in place, just like editing these items in the Scene view. When in Raw Edit mode however, the pivot or anchor will state in place and the UI element will move relative to that fixed item.
Values Driven by Another UI Component
(13:31) There are times when a Rect Transform will have some or all of its values driven by a parent Rect Transform or other UI component, such as a layout group. In these cases, some or all of the properties of the Rect Transform will be greyed out and uneditable; there will be a note at the top of the Rect Transform indicating that some values are being driven by another component.
For more information on the components driving the Rect Transform, see the Unity documentation or the associated [transcribed] lesson for that component.