UGUI源码分析:LayoutGroup中的纵横布局组件(HorizontalOrVerticalLayoutGroup)

系列

UGUI源码分析系列总览
相关前置:
UGUI CanvasUpdateSystem源码分析
UGUI源码分析:LayoutSystem布局系统


UML图一览

在这里插入图片描述


LayoutGroup

BaseClass: UIBehaviour

Interface: ILayoutElement, ILayoutGroup

Intro:布局系统的实施组件的基类

虽然UGUI组件中有一些组件都继承了ILayoutElement接口,但并不会涉及对接口方法的实现。这是因为这些组件主要是布局操作的接收方,只需要通过该接口被布局实施方所发现即可

LayoutGroup,是布局组件的基类(GridLayoutGroup、HorizontalOrVerticalLayoutGroup)。本文主要针对纵横布局组件(HorizontalLayoutGroup、VerticalLayoutGroup)进行分析。

属性介绍

在这里插入图片描述

  • Padding:内部边距,调整实际用于布局区域的大小
  • Spacing :子物体直接的间隔
  • Child Alignment :子物体对齐方式
  • Child Controls Size :组件控制子物体尺寸开关,开启时组件可以更改物体尺寸。
  • Child Force Expand :组件控制子物体填充区域开关,若可以修改尺寸则会改变子物体尺寸填充区域,若不可以修改尺寸,则根据区域大小均衡分布子物体。

布局过程

接下来,作者以HorizontalLayoutGroup为例,逐步进行布局实现的分析。

延续LayoutRebuilderRebuild方法(详情:UGUI源码分析:LayoutSystem布局系统),首先被执行的是ILayoutElementCalculateLayoutInputHorizontal方法。该方法将收集其子节点下所有没有被标记ignoreLayout的物体(m_RectChildren)。

// HorizontalLayoutGroup
public override void CalculateLayoutInputHorizontal()
{
   
    //  基类(LayoutGroup)方法
    base.CalculateLayoutInputHorizontal();
    CalcAlongAxis(0, false);
}
//LayoutGroup 
public virtual void CalculateLayoutInputHorizontal()
{
   
    //清空list,准备收集子节点下没有被ignoreLayout的物体
    m_RectChildren.Clear();
    var toIgnoreList = ListPool<Component>.Get();
    for (int i = 0; i < rectTransform.childCount; i++)
    {
   
        var rect = rectTransform.GetChild(i) as RectTransform;
        if (rect == null || !rect.gameObject.activeInHierarchy)
            continue;
        rect.GetComponents(typeof(ILayoutIgnorer), toIgnoreList);
        if (toIgnoreList.Count == 0)
        {
   
            m_RectChildren.Add(rect);
            continue;
        }
        for (int j = 0; j < toIgnoreList.Count; j++
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值