Unity RectTransform.API

  1. Anchor Point: An anchor point is a point used to control the position and size of UI elements on the screen. In Unity, anchors are commonly used to control the layout and scaling of UI elements. The position of the anchor point is determined relative to the parent object or the canvas, and the position and size of the UI element can be changed by modifying the properties of the anchor point.

  2. Pivot Point: The pivot point is the point used to control the rotation and scaling of the object. In Unity, pivot points are often used to control the rotation and scaling of 3D models. The position of the pivot point is determined relative to the local coordinate system of the object, and the rotation and scaling of the object can be changed by modifying the properties of the pivot point.

The relationship between Anchor and Pivot and position?

Use the anchor point to calculate the position: the position attribute of the RectTransform can be calculated by the anchor point (Anchor Min) in the lower left corner and the anchor point (Anchor Max) in the upper right corner. These two anchors define the position and size of the UI element relative to its parent object or canvas. Therefore, the position of the UI element can be determined by calculating the position and size of the anchor point, and the offset relative to the anchor point. The specific calculation formula is as follows:

position = parent_position + 
(anchor_min + anchor_max) * 0.5f * parent_size + pivot * size + offset

Among them, parent_position represents the position of the parent object, parent_size represents the size of the parent object, pivot represents the position of the pivot point, size represents the size of the UI element, and offset represents the offset relative to the anchor point.

Use the pivot point to calculate the position: The position property of RectTransform can also be calculated by its pivot point (Pivot) and the offset relative to the pivot point. The pivot point defines the center of rotation and scaling of the object, so the position of the object can be determined by calculating the position of the pivot point and the offset relative to the pivot point. The specific calculation formula is as follows:

position = parent_position + pivot * parent_size + offset

Among them, parent_position represents the position of the parent object, parent_size represents the size of the parent object, pivot represents the position of the pivot point, and offset represents the offset relative to the pivot point.

If a RectTransform's anchor point and pivot point are set to default values ​​(that is, the anchor point and pivot point are both lower-left corners), then its RectTransform.position property is equal to its Transform.position property. Therefore, the position in the above formula can be replaced by transform.position to obtain the following calculation formula:

transform.position = parent_position + 
(anchor_min + anchor_max) * 0.5f * parent_size + pivot * size + offset

Guess you like

Origin blog.csdn.net/weixin_40695640/article/details/129470005