Unity_ugui Translations (2) Basic layout (basic layouts)

Source: Internet
Author: User

Basic layout (base layouts)

In this section we'll look at how can position UI elements relative to the Canvas and each of the other. If you want to test yourself while reading, you can create an image using the menu gameobject > UI > Image.

In this section, we'll try to lay out the UI elements so that they can be associated with canvas and between them. If you want to try it out in the process of reading, you can test it by creating an image in Menu Gameobject > UI > Image.

The Rect tool (Rectangle tools)

Every UI element is represented as a rectangle for layout purposes. This rectangle can is manipulated in the Scene View using the Rect Tool in the toolbar. The Rect Tool is used both for Unity's 2D features and for UI, and in fact can being used even for 3D objects as well.

Each UI element is displayed as a rectangle in the view. You can change the rectangle that represents the UI element in the scene view by using the Rectangle tool in the toolbar. This rectangle tool is also suitable for unity2d features and UI elements, and in fact it can also be applied to 3D objects

Toolbar buttons with Rect tool selected (Rectangle tools selected in Toolbar)

The Rect Tool can be used-move UI element around, resize them, and rotate them, all in one. Once You has selected a UI element, you can move it by clicking anywhere inside the rectangle and dragging. You can resize it by clicking on the edges or corners and dragging. The element can be rotated by hovering the cursor slightly away from the corners until the mouse cursor indicates a rotati On symbol. You can then click and drag in either direction to rotate.

The Rect tool can be used to move the UI at the same time, changing the UI size and rotating the UI. Once you have selected a UI element, You can click anywhere within the UI rectangle to drag it elsewhere. You can place the mouse over the border of the UI rectangle or the corners to change the size of the UI by dragging. If you want to rotate the UI element, move the mouse over a corner until the mouse is displayed as a rotation icon. You can then rotate the UI by clicking Add and drag.

Just like the other tools, the Rect Tool uses the current pivot mode and space, set in the toolbar. When working with UI it's usually a good idea to keep those set to Pivot and Local.

Just like other tools, the Rect tool can also use the current center point (pivot) mode and the spatial mode with the toolbar settings. When you manipulate the UI, setting the toolbar to pivot and Local is a good choice.

Toolbar buttons set to pivot and local (set as pivot and local in toolbar)

Rect Transform

The Rect Transform is a new Transform component that's used for all UI elements instead of the regular TRANSFORMC Omponent.

Rect transform is a new transform component that replaces the regular transform component when it operates on all UI elements

Rect Transforms had position, rotation, and scale just like regular Transforms, but it also had a width and height, used To specify the dimensions of the rectangle.

Rect transform component has position, angle, size these properties are like normal transform components, but it also has width, height these properties, which specify the dimensions of the UI rectangle

resizing Versus scaling (choice between resizing and scaling)

When the Rect Tool was used to change the size of a object, normally for Sprites in the 2D system and for 3D objects it WI ll change the local scale of the object. However, when it's used on an object with a Rect Transform on it, it'll instead change the width and the height, keeping The local scale unchanged. This resizing won't affect font sizes, border on sliced images, and so on.

For 2D elf Systems and 3D objects, changing the size of the object when using scale changes the size of the object, however, when the Rect component is resized, it will change the width and length of the object, and its size will not change. This will not change the size of the font, The borders of the trimmed pictures, and so on.

After the experiment 3 text UI: Are all Hello world text, the bottom is the control group, width100 height100 Scale:x=1,y=1, you can see the missing letter "D", the top is the Rect tool to change width to 200, Can see all the text of Hello World, and the size of the text has not changed, the middle of the scale, the value of the X is changed to 2, you can see the letter "D" or not see, but the size of the font is changed

Pivot (pivot;Center point, hub)

rotations, size, and scale modifications occur around the pivot so the position of the pivot affects the outcome of a rota tion, resizing, or scaling. When the toolbar Pivot button was set to Pivot mode, the pivot of a Rect Transform can being moved in the Scene View.

rotation, size, and scale changes are made around pivot, so the position change of pivot also affects the effect of rotation, size and scale changes, and when you select Pivot mode in the toolbar, You can change the position of pivot in scene view by using the Rect tool.

Anchors (anchor Point)

Rect Transforms include a layout concept called anchors. Anchors was shown as four small triangular handles in the Scene View and anchor information was also shown in the Inspector .

The Rect component also includes a layout concept called an anchor point. The anchor point is represented in scene scenes by four small triangular tools, and information about the anchor point can also be seen in the inspector.

If the parent of a rect Transform is also a rect Transform, the child Rect Transform can being anchored to the parent Rect Tr Ansform in various ways. For example, the child can is anchored to the center of the parent, or to one of the corners.

If the parent game object of a RECT component also has a RECT component, then the RECT component of the subclass object can be fixed in a different way by the anchor point in the RECT component of the parent object, such as the child class being fixed in the middle of the parent class, or a corner.

UI element anchored to the center of the parent. The element maintains a fixed offset to the center.

The anchor point of the UI element is fixed at the center of the parent class, and when the parent class changes, it remains at a distance from the parent class

UI element anchored to the lower right corner of the parent. The element maintains a fixed offset to the lower right corner.

The anchor point of the UI element is fixed when the parent class changes, it always and the lower right corner to maintain a certain deviation

The anchoring also allows the child to stretch together with the width or height of the parent. Each corner of the rectangle have a fixed offset to their corresponding anchor, i.e. the top left corner of the rectangle have A fixed offset to the top left anchor, etc. This is the different corners of the rectangle can is anchored to different points in the parent rectangle.

Setting the anchor point allows the subclass object to change the width and height with the parent object at the same time. The four corners of the rectangle and its corresponding anchor points have a fixed offset, such as the upper left corner of the rectangle corresponding to the upper left corner of the anchor point has a fixed offset. In this way, the different corners of the rectangle are fixed with different anchor points corresponding to the parent class.

UI element with a left corners anchored to lower left corner of the parent and right corners anchored to lower right. The corners of the element maintains fixed offsets to their respective anchors.

The left two corners of the UI element make the lower-left corner of the parent element the anchor point, and the two corners on the right of the subclass element set the lower-right corner of the parent element to the anchor point. Each corner of the element and the anchor point they correspond to will remain offset.

The positions of the anchors is defined in fractions (or percentages) of the parent rectangle width and height. 0.0 (0%) corresponds to the left or bottom side, 0.5 (50%) to the middle, and 1.0 (100%) to the right or top side. But anchors is not limited to the sides and middle; They can anchored to any point within the parect rectangle.

The position of the anchor point can be set by a fraction or a percentage in the parent class, 0.0 corresponds to the left and the bottom, the 1,0 corresponds to the right or the top, the left, 0 for the leftmost, 1 for the right, up and down, 0 for the bottom, and 1 for the top, but the anchor can not only in the parent object's border , you can also set the anchor point anywhere within the parent class rectangle.

UI element with left corners anchored to a point a certain percentage from the left side of the parent and right corners a Nchored to a point a certain percentage from the right side of the parent rectangle.

The left border of the left two corners of the UI element and the parent object remains a fixed scale, and the right side is the same.

You can drag each of the anchors individually, or if they is together, you can drag them together by clicking in the Midd Le in between them and dragging. If you holding down Shift key while dragging an anchor, the corresponding corner of the rectangle would move together with The anchor.

A useful feature of the anchor handles is, they automatically snap to the anchors of sibling rectangles Recise positioning.

You can set each anchor point separately, or you can place the mouse in the middle of the anchor point and drag, while setting all the anchor points. If you hold the SHIFT key while dragging an anchor point, the vertices of the parent object corresponding to the anchor point will also move. The anchor point has a very useful feature, They will automatically align with the UI rectangle of the other sibling and maintain an accurate position

Anchor Presets (anchor preset)

In the Inspector, the Anchor Preset button can be found in the upper left corner of the Rect Transform component. Clicking the button brings up the Anchor presets dropdown. From this can quickly select from some of the most common anchoring options. You can anchor the UI element into the sides or middle of the parent, or stretch together with the parent size. The horizontal and vertical anchoring is independent.

On the Inspector panel, the Anchor preset button is visible at the top left of the Rect transform component, click this button to see the Anchorage Presets Panel Open, From here you can quickly select several of the most commonly used anchor placement methods. This allows you to pin the UI element to the frame or center of the parent object, and it will change flexibly as the size of the parent object changes, and the horizontal anchor setting and the vertical anchor point setting are independent of each other.

The Anchor presets buttons displays the currently selected preset option if there is one. If the anchors on either the horizontal or vertical axis is set to different positions than any of the presets, the Custo M options is shown.

If a pre-made anchor preset is selected, the Anchor preset button is displayed. If the horizontal or vertical direction of the anchor point is not selected for several prefabricated anchor settings, then the Anchor button will be displayed as custom at the corresponding point.

Anchor and Position fields in the Inspector

You can click the anchors expansion arrow to reveal the anchor number fields if they is not already visible. Anchor Mincorresponds to the lower left Anchor handle in the Scene View, and Anchor Max corresponds to the upper R ight handle.

If you don't see the anchor Min,max numbers, you can also click the Anchor Extension button to display them. The minimum value of Ancher (0,0) corresponds to the lower-left corner, which corresponds to the upper-right corner.

The position fields of rectangle is shown differently depending on whether the anchors is together (which produces a fix Ed width and height) or separated (which causes the rectangle to stretch together with the parent rectangle).

The Postion box will show different values depending on whether the anchor is placed together (it will show a corrected width and height), or be displayed separately (this will cause the UI element's rectangle to change with the rectangle of the parent object).

When all the anchor handles is together the fields displayed is POS X, pos Y, Width and Height. The POS X and POS Y values indicate the position of the pivot relative to the anchors.

When all the anchor icons are displayed in the POS box, the POS X, pos Y, width, and length are displayed. The values for Pos x and y represent the relationship between the pivot and the anchor point.

When the anchors is separated the fields can change partially or completely to left, right, Top and Bottom. These fields define the padding inside the rectangle defined by the anchors. The left and right fields is used if the anchors is separated horizontally and the Top and Bottom fields is used if the Y is separated vertically.

You can set the anchor point in the input box separately so that it is completely on the left or right or top or bottom. These input boxes represent the distance between the anchor point and the border. The left and the right input boxes can be set around, top and bottom to set up and down

Note that changing the values in the anchor or pivot fields would normally counter-adjust the positioning values in order t o Make the rectangle stay in place. If cases where this is not desired, the Raw Mode can was enabled using a small button in the Inspector. This causes the anchor and pivot value to is able to is changed without any other values changing as a result. This would likely cause the rectangle to being visually moved or resized, since its position and size are dependent on the Anch OR and pivot values.

As you change the values in the anchor and pivot input boxes, the position values change as well, which is to keep the UI rectangle in place. If this causes the rectangle position to not be in the desired position, the raw mode (which is the Inspector R icon) Can be activated in inspector, which makes anchor and pivot not change with other values, which can make the UI rectangle change visually because its position and size are independent of anchor and pivot

Unity_ugui Translations (2) Basic layout (basic layouts)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.