docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Interface ILayoutElement

    A component is treated as a layout element by the auto layout system if it implements ILayoutElement.

    Namespace: UnityEngine.UI
    Assembly: UnityEngine.UI.dll
    Syntax
    public interface ILayoutElement
    Remarks

    The layout system will invoke CalculateLayoutInputHorizontal before querying minWidth, preferredWidth, and flexibleWidth. It can potentially save performance if these properties are cached when CalculateLayoutInputHorizontal is invoked, so they don't need to be recalculated every time the properties are queried.

    The layout system will invoke CalculateLayoutInputVertical before querying minHeight, preferredHeight, and flexibleHeight.It can potentially save performance if these properties are cached when CalculateLayoutInputVertical is invoked, so they don't need to be recalculated every time the properties are queried.

    The minWidth, preferredWidth, and flexibleWidth properties should not rely on any properties of the RectTransform of the layout element, otherwise the behavior will be non-deterministic. The minHeight, preferredHeight, and flexibleHeight properties may rely on horizontal aspects of the RectTransform, such as the width or the X component of the position. Any properties of the RectTransforms on child layout elements may always be relied on.

    Properties

    flexibleHeight

    The extra relative height this layout element should be allocated if there is additional available space.

    Declaration
    float flexibleHeight { get; }
    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 flexible height to 100.
            foreach (LayoutElement element in myLayoutElements)
            {
                element.flexibleHeight = 100f;
            }
        }
    }

    flexibleWidth

    The extra relative width this layout element should be allocated if there is additional available space.

    Declaration
    float flexibleWidth { get; }
    Property Value
    Type Description
    float
    Remarks

    Setting preferredWidth to -1 removed the preferredWidth.

    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 flexible width to 200.
            foreach (LayoutElement element in myLayoutElements)
            {
                element.flexibleWidth = 200f;
            }
        }
    }

    layoutPriority

    The layout priority of this component.

    Declaration
    int layoutPriority { get; }
    Property Value
    Type Description
    int
    Remarks

    If multiple components on the same GameObject implement the ILayoutElement interface, the values provided by components that return a higher priority value are given priority. However, values less than zero are ignored. This way a component can override only select properties by leaving the remaning values to be -1 or other values less than zero.

    minHeight

    The minimum height this layout element may be allocated.

    Declaration
    float minHeight { get; }
    Property Value
    Type Description
    float

    minWidth

    The minimum width this layout element may be allocated.

    Declaration
    float minWidth { get; }
    Property Value
    Type Description
    float

    preferredHeight

    The preferred height this layout element should be allocated if there is sufficient space.

    Declaration
    float preferredHeight { get; }
    Property Value
    Type Description
    float
    Remarks

    PreferredHeight can be set to -1 to remove the size.

    preferredWidth

    The preferred width this layout element should be allocated if there is sufficient space.

    Declaration
    float preferredWidth { get; }
    Property Value
    Type Description
    float
    Remarks

    PreferredWidth can be set to -1 to remove the size.

    Methods

    CalculateLayoutInputHorizontal()

    After this method is invoked, layout horizontal input properties should return up-to-date values. Children will already have up-to-date layout horizontal inputs when this methods is called.

    Declaration
    void CalculateLayoutInputHorizontal()

    CalculateLayoutInputVertical()

    After this method is invoked, layout vertical input properties should return up-to-date values. Children will already have up-to-date layout vertical inputs when this methods is called.

    Declaration
    void CalculateLayoutInputVertical()
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)