SoFunction
Updated on 2025-03-07

Unity UGUI’s LayoutElement LayoutElement Component Introduction Example

1. What is the LayoutElement component?

LayoutElement is a layout element component in Unity UGUI that controls the size and position of UI elements in the layout. It can be used to automatically resize UI elements to suit different screen resolution and layout requirements.

2. How the LayoutElement component works

The LayoutElement component controls the layout of UI elements by setting some properties, including minimum and maximum width, minimum and maximum height, width and height priority, etc. When the layout component where the UI element resides (such as HorizontalLayoutGroup or VerticalLayoutGroup) performs layout calculations, the final size and position of the UI element will be determined based on the attributes of the LayoutElement.

3. Common properties of LayoutElement component

  • Min Width (minimum width): Minimum width of UI elements.
  • Min Height (minimum height): Minimum height of UI elements.
  • Preferred Width (Preferred Width): The preferred width of the UI element.
  • Preferred Height: The preferred height of UI elements.
  • Flexible Width (scalable width): The width of the UI element can be scaled according to the layout requirements.
  • Flexible Height (scalable height): The height of UI elements can be scalable according to layout requirements.
  • Layout Priority: The priority of UI elements in layout calculations, used to determine the size and position of UI elements.

4. Common functions of LayoutElement component

  • CalculateLayoutInputHorizontal(): Calculate the layout input of UI elements in the horizontal direction.
  • CalculateLayoutInputVertical(): Calculate the layout input of UI elements in the vertical direction.

5. Example code

Example 1: Set the minimum width and minimum height of the UI element

LayoutElement layoutElement = GetComponent<LayoutElement>();
 = 100;
 = 50;

Operation steps:

  • Get the LayoutElement component.
  • Set the minimum width to 100.
  • Set the minimum height to 50.

Notes:

  • The values ​​of minimum width and minimum height should be adjusted according to actual needs.

Example 2: Set the preferred width and preferred height of the UI element

LayoutElement layoutElement = GetComponent<LayoutElement>();
 = 200;
 = 100;

Operation steps:

  • Get the LayoutElement component.
  • Set the preferred width to 200.
  • Set the preferred height to 100.

Notes:

  • The values ​​of the preferred width and preferred height should be adjusted according to actual needs.

Example 3: Set the scalable width and scalable height of the UI element

LayoutElement layoutElement = GetComponent<LayoutElement>();
 = 1;
 = 1;

Operation steps:

  • Get the LayoutElement component.
  • Set the scalable width to 1.
  • Set the scalable height to 1.

Notes:

  • The values ​​of the scalable width and scalable height should be adjusted according to actual needs.

Example 4: Set the layout priority of UI elements

LayoutElement layoutElement = GetComponent<LayoutElement>();
 = 1;

Operation steps:

  • Get the LayoutElement component.
  • Set the layout priority to 1.

Notes:

  • The greater the value of the layout priority, the higher the priority of the UI elements in the layout calculation.

Example 5: Calculate the layout input of UI elements

LayoutElement layoutElement = GetComponent<LayoutElement>();
();
();

Operation steps:

  • Get the LayoutElement component.
  • Call CalculateLayoutInputHorizontal() function to calculate layout input in the horizontal direction.
  • Call CalculateLayoutInputVertical() function to calculate layout input in the vertical direction.

Notes:

  • When you need to manually trigger the publishing calculation, you can call the CalculateLayoutInputHorizontal() and CalculateLayoutInputVertical() functions.

References

Unity official documentation:LayoutElement

The above is the detailed content of the LayoutElement LayoutElement component introduction example of Unity UGUI. For more information about the LayoutElement component of Unity UGUI, please follow my other related articles!