LayoutRebuilder.ForceRebuildLayoutImmediate 不重建 Horizo​​ntalLayoutGroup Unity

Bek*_*ega 5 c# layout user-interface unity-game-engine unity-ui

我正在处理一个组件,我需要在 Start 函数中正确定位 Horizo​​ntalLayoutGroup 的子项。

我可以通过执行以下操作强制 Horizo​​ntalLayoutGroup 重建:

public HorizontalLayoutGroup horizLayoutGroup;
public RectTransform exampleChild;

private void Start()
{
    horizLayoutGroup.CalculateLayoutInputHorizontal();
    horizLayoutGroup.CalculateLayoutInputVertical();
    horizLayoutGroup.SetLayoutHorizontal();
    horizLayoutGroup.SetLayoutVertical();

    Debug.Log(exampleChild.anchoredPosition);
}
Run Code Online (Sandbox Code Playgroud)

但这样做似乎很奇怪:

 public RectTransform horizRectTransform;
 public RectTransform exampleChild;

 private void Start()
 {
     LayoutRebuilder.ForceRebuildLayoutImmediate(horizRectTransform);
     Debug.Log(exampleChild.anchoredPosition);
 }
Run Code Online (Sandbox Code Playgroud)

不起作用,因为据我从源代码中可以看出 LayoutRebuilder.ForceRebuildLayoutImmediate 正在调用相同的函数:

public static void ForceRebuildLayoutImmediate(RectTransform layoutRoot)
    {
        var rebuilder = s_Rebuilders.Get();
        rebuilder.Initialize(layoutRoot);
        rebuilder.Rebuild(CanvasUpdate.Layout);
        s_Rebuilders.Release(rebuilder);
    }

    public void Rebuild(CanvasUpdate executing)
    {
        switch (executing)
        {
            case CanvasUpdate.Layout:
                // It's unfortunate that we'll perform the same GetComponents querys for the tree 2 times,
                // but each tree have to be fully iterated before going to the next action,
                // so reusing the results would entail storing results in a Dictionary or similar,
                // which is probably a bigger overhead than performing GetComponents multiple times.
                PerformLayoutCalculation(m_ToRebuild, e => (e as ILayoutElement).CalculateLayoutInputHorizontal());
                PerformLayoutControl(m_ToRebuild, e => (e as ILayoutController).SetLayoutHorizontal());
                PerformLayoutCalculation(m_ToRebuild, e => (e as ILayoutElement).CalculateLayoutInputVertical());
                PerformLayoutControl(m_ToRebuild, e => (e as ILayoutController).SetLayoutVertical());
                break;
        }
    }
Run Code Online (Sandbox Code Playgroud)

它的全部目的似乎是重建布局:Wiki Entry

强制立即重建受计算影响的布局元素和子布局元素。

所以是的,我真的没有问题,我只是想知道为什么它不像人们期望的那样工作。如果有人对此有更多信息,我会很高兴听到它!

事物的源代码链接:

LayoutRebulider

水平布局组

水平或垂直布局组

布局组