I'm working on a component where I need the correct anchored positions of children of a HorizontalLayoutGroup in the Start function.
I can force the HorizontalLayoutGroup to rebuild by doing:
public HorizontalLayoutGroup horizLayoutGroup;
public RectTransform exampleChild;
private void Start()
{
horizLayoutGroup.CalculateLayoutInputHorizontal();
horizLayoutGroup.CalculateLayoutInputVertical();
horizLayoutGroup.SetLayoutHorizontal();
horizLayoutGroup.SetLayoutVertical();
Debug.Log(exampleChild.anchoredPosition);
}
But it seems odd that doing:
public RectTransform horizRectTransform;
public RectTransform exampleChild;
private void Start()
{
LayoutRebuilder.ForceRebuildLayoutImmediate(horizRectTransform);
Debug.Log(exampleChild.anchoredPosition);
}
Does not work, because as far as I can tell from the source code LayoutRebuilder.ForceRebuildLayoutImmediate is calling the same functions:
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;
}
}
And the whole purpose of it seems to be to rebuild layouts: Wiki Entry
Forces an immediate rebuild of the layout element and child layout elements affected by the calculations.
So yeah I don't really have a problem with this necessarily I'm just wondering why it doesn't work as one would expect it to. If anyone has more info on this I would be thrilled to hear it!
Links to the source code for things: