- Home /
Get layoutgroup and contentSizeFitter to update immediately
I'm populating a new UI scrollview programatically. The content gameobject of the scrollview has a verticalLayoutGroup and a contentSizeFitter to arrange it. The children of it I'm adding programmatically are prefabs with layoutElement components.
I want to populate it, have the verticalLayoutGroup and contentSizeFitter arrange everything, and then disable them so I can move the elements around (I want to animate them in), all in the same frame.
The only way Ive found to do this has been to add the elements. Then wait one frame, then disable the verticalLayoutGroup and contentSizeFitter.
Ive searched around and tried the following things but they dont work (all the children end up on top of each other, not arranged properly)
void SetupAnimateIn()
{
//Populate content with children elements here
Canvas.ForceUpdateCanvases();
_ContentSizeFitter.enabled = false;
_VerticalLayoutGroup.enabled = false;
}
void SetupAnimateIn()
{
//Populate content with children elements here
LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)Content.transform );
_ContentSizeFitter.enabled = false;
_VerticalLayoutGroup.enabled = false;
}
void SetupAnimateIn()
{
//Populate content with children elements here
Canvas.ForceUpdateCanvases();
_VerticalLayoutGroup.CalculateLayoutInputVertical();
_VerticalLayoutGroup.SetLayoutVertical();
_ContentSizeFitter.SetLayoutVertical();
LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)Content.transform );
_ContentSizeFitter.enabled = false;
_VerticalLayoutGroup.enabled = false;
}
The only thing that has worked for me so far is..
void SetupAnimateIn()
{
//Populate content with children elements here
// Wait one frame
_ContentSizeFitter.enabled = false;
_VerticalLayoutGroup.enabled = false;
}
Answer by k1ljcore · Jun 12, 2017 at 09:13 AM
This solved the issue for me:
Canvas.ForceUpdateCanvases();
_vertLayoutGroup.SetLayoutVertical();
Answer by MaxWeiChen · Jul 05, 2017 at 04:46 AM
i have a solution, you just need add enable = true
//Populate content with children elements here
_ContentSizeFitter.enabled = true;
_ContentSizeFitter.SetLayoutVertical();
LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)Content.transform );
_ContentSizeFitter.enabled = false;
@$$anonymous$$axWeiChen your solution worked for me just swapping the .enable = true and .enabled = false (turn it off update then turn it back on)
Thanks. Just calling LayoutRebuilder.ForceRebuildLayoutImmediate
method solved my issue.
Answer by Lord-Megabite · Feb 13, 2017 at 12:09 PM
I dont understand what cause this issue, but y have this problem in editor when im create a new object with this component, and for me, this solved the issue:
HorizontalLayoutGroup hlg = itSelf.container.GetComponent(); hlg.enabled = false; hlg.enabled = true;
Did you ever solve this in a stable way as I am having the same issue?
Answer by WylieFoxxx · Oct 19, 2020 at 08:16 PM
Trying to Rebuild the layout immediately wasn't working for me, so I had to use a coroutine that waits until fixed update, then toggles the menu on and off... A pretty bad solution if you ask me, but it works:
// Usage: StartCoroutine(RefreshLayout(layout));
IEnumerator RefreshLayout(GameObject layout)
{
yield return new WaitForFixedUpdate();
VerticalLayoutGroup vlg = layout.GetComponent<VerticalLayoutGroup>();
vlg.enabled = false;
vlg.enabled = true;
}
Your answer
Follow this Question
Related Questions
[UI] Grid Layout Group Custom Sort Order 1 Answer
Unity crashes when using ExecuteAlways on a UIBehaviour 0 Answers
How can I edit the horizontal layout group on a panel or object 1 Answer
Nested UI elements and layout 0 Answers
How can I return a Dragged Object From a Layer Group to its Original Position? 0 Answers