Class LayoutElement
Add this component to a GameObject to make it into a layout element or override values on an existing layout element.
Inherited Members
Namespace: UnityEngine.UI
Assembly: UnityEngine.UI.dll
Syntax
[AddComponentMenu("Layout/Layout Element", 140)]
[RequireComponent(typeof(RectTransform))]
[ExecuteAlways]
public class LayoutElement : UIBehaviour, ILayoutElement, ILayoutIgnorer
Constructors
LayoutElement()
Protected default constructor. Use UnityEngine.GameObject.AddComponent<T>() to add a LayoutElement to a GameObject.
Declaration
protected LayoutElement()
Properties
flexibleHeight
The extra relative height this layout element should be allocated if there is additional available space.
Declaration
public virtual float flexibleHeight { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
flexibleWidth
The extra relative width this layout element should be allocated if there is additional available space.
Declaration
public virtual float flexibleWidth { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
ignoreLayout
Should this RectTransform be ignored by the layout system?
Declaration
public virtual bool ignoreLayout { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
Setting this property to true will make a parent layout group component not consider this RectTransform part of the group. The RectTransform can then be manually positioned despite being a child GameObject of a layout group.
layoutPriority
The Priority of layout this element has.
Declaration
public virtual int layoutPriority { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
maxHeight
The maximum height this layout element may be allocated.
Declaration
public virtual float maxHeight { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when using UI elements.
public class ExampleClass : MonoBehaviour
{
public Transform MyContentPanel;
//Sets the maximum height on all children in the content panel.
public void Start()
{
//Assign all the children of the content panel to an array.
LayoutElement[] myLayoutElements = MyContentPanel.GetComponentsInChildren<LayoutElement>();
//For each child in the array change its LayoutElement's maximum height size to 64.
foreach (LayoutElement element in myLayoutElements)
{
element.maxHeight = 64f;
}
}
}
maxWidth
The maximum width this layout element may be allocated.
Declaration
public virtual float maxWidth { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when using UI elements.
public class ExampleClass : MonoBehaviour
{
public Transform MyContentPanel;
//Sets the maximum width on all children in the content panel.
public void Start()
{
//Assign all the children of the content panel to an array.
LayoutElement[] myLayoutElements = MyContentPanel.GetComponentsInChildren<LayoutElement>();
//For each child in the array change its LayoutElement's maximum width size to 200.
foreach (LayoutElement element in myLayoutElements)
{
element.maxWidth = 200f;
}
}
}
minHeight
The minimum height this layout element may be allocated.
Declaration
public virtual float minHeight { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when using UI elements.
public class ExampleClass : MonoBehaviour
{
public Transform MyContentPanel;
//Sets the flexible height on on all children in the content panel.
public void Start()
{
//Assign all the children of the content panel to an array.
LayoutElement[] myLayoutElements = MyContentPanel.GetComponentsInChildren<LayoutElement>();
//For each child in the array change its LayoutElement's minimum height size to 64.
foreach (LayoutElement element in myLayoutElements)
{
element.minHeight = 64f;
}
}
}
minWidth
The minimum width this layout element may be allocated.
Declaration
public virtual float minWidth { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when using UI elements.
public class ExampleClass : MonoBehaviour
{
public Transform MyContentPanel;
//Sets the flexible height on on all children in the content panel.
public void Start()
{
//Assign all the children of the content panel to an array.
LayoutElement[] myLayoutElements = MyContentPanel.GetComponentsInChildren<LayoutElement>();
//For each child in the array change its LayoutElement's minimum width size to 200.
foreach (LayoutElement element in myLayoutElements)
{
element.minWidth = 200f;
}
}
}
preferredHeight
The preferred height this layout element should be allocated if there is sufficient space.
Declaration
public virtual float preferredHeight { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
Remarks
PreferredHeight can be set to -1 to remove the size.
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when using UI elements.
public class ExampleClass : MonoBehaviour
{
public Transform MyContentPanel;
//Sets the flexible height on on all children in the content panel.
public void Start()
{
//Assign all the children of the content panel to an array.
LayoutElement[] myLayoutElements = MyContentPanel.GetComponentsInChildren<LayoutElement>();
//For each child in the array change its LayoutElement's preferred height size to 100.
foreach (LayoutElement element in myLayoutElements)
{
element.preferredHeight = 100f;
}
}
}
preferredWidth
The preferred width this layout element should be allocated if there is sufficient space.
Declaration
public virtual float preferredWidth { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
Remarks
PreferredWidth can be set to -1 to remove the size.
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when using UI elements.
public class ExampleClass : MonoBehaviour
{
public Transform MyContentPanel;
//Sets the flexible height on on all children in the content panel.
public void Start()
{
//Assign all the children of the content panel to an array.
LayoutElement[] myLayoutElements = MyContentPanel.GetComponentsInChildren<LayoutElement>();
//For each child in the array change its LayoutElement's preferred width size to 250.
foreach (LayoutElement element in myLayoutElements)
{
element.preferredWidth = 250f;
}
}
}
Methods
CalculateLayoutInputHorizontal()
Called by the layout system. Calculates and stores horizontal layout properties.
Declaration
public virtual void CalculateLayoutInputHorizontal()
CalculateLayoutInputVertical()
Called by the layout system. Calculates and stores vertical layout properties.
Declaration
public virtual void CalculateLayoutInputVertical()
OnBeforeTransformParentChanged()
Called before the parent changes. Notifies the layout system that a rebuild is needed.
Declaration
protected override void OnBeforeTransformParentChanged()
Overrides
OnDidApplyAnimationProperties()
Called when animation properties are applied. Notifies the layout system that a rebuild is needed.
Declaration
protected override void OnDidApplyAnimationProperties()
Overrides
OnDisable()
Called when it becomes disabled. Notifies the layout system that a rebuild is needed.
Declaration
protected override void OnDisable()
Overrides
OnEnable()
Called when it becomes enabled. Notifies the layout system that a rebuild is needed.
Declaration
protected override void OnEnable()
Overrides
OnTransformParentChanged()
Called when the parent changes. Notifies the layout system that a rebuild is needed.
Declaration
protected override void OnTransformParentChanged()
Overrides
OnValidate()
Declaration
protected override void OnValidate()
Overrides
SetDirty()
Mark the LayoutElement as dirty.
Declaration
protected void SetDirty()
Remarks
This will make the auto layout system process this element on the next layout pass. This method should be called by the LayoutElement whenever a change is made that potentially affects the layout.