LayoutRebuilder.ForceRebuildLayoutImmediate不重建Horizo​​ntalLayoutGroup Unity

时间:2018-01-27 18:13:10

标签: c# user-interface unity3d layout 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);
}

但做起来似乎很奇怪:

 public RectTransform horizRectTransform;
 public RectTransform exampleChild;

 private void Start()
 {
     LayoutRebuilder.ForceRebuildLayoutImmediate(horizRectTransform);
     Debug.Log(exampleChild.anchoredPosition);
 }

不起作用,因为据我所知,源代码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;
        }
    }

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

  

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

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

指向事物源代码的链接:

LayoutRebulider

HorizontalLayoutGroup

HorizontalOrVerticalLayoutGroup

LayoutGroup

0 个答案:

没有答案