Force Unity UI element to refresh/update?

I have a Button element with a Content Size Fitter set to preferred size. The goal is to make the Button’s RectTransform resize automatically based on its text. However, I noticed that the rect doesn’t automatically resize when new text is pushed to its child’s Text component. It only resizes when the child’s values change (e.g. if I manually change the font size) or when the button is selected.

Is there any way to manually force Unity to “update” the button as soon as the text changes? I know about Button.Select(), but I have two buttons visible at the same time, and I want all of this to happen in one frame.

Here’s an example of what the buttons look like when they’re first set to active and populated with text:

alt text

And here’s what they look like after the bottom one is highlighted:
alt text

This will do what you want in regards to fixing up layouts from things like ContentSizeFitters and VerticalLayoutGroups.

The easiest use for this would be:

LayoutRebuilder.ForceRebuildLayoutImmediate(transform as RectTransform);

Perhaps, this might help? Unity - Scripting API: RectTransform.ForceUpdateRectTransforms


from forum: (

Maybe LayoutRebuilder.ForceRebuildLayoutImmediate can be useful for that case