RectTransform sizeDelta not updating fast enough

I’m instantiating a prefab that calculates its own size depending on the context (basically the description text on it), because of some animations I can’t use the VerticalLayoutGroup to position them because I have some animations in place and it gets in the place.

Everything is fine with this setup except for 1 thing, the sizeDelta of the objects doesn’t update in the same frame, but it takes 2-3 frames to give the final value even if the visuals are fine since instantiated.

Frame:44 - Instantiate - RectTransform.sizeDelta:(500.00, 0.00)
Frame:45 - RectTransform.sizeDelta:(315.76, 19.00)
Frame:46 - RectTransform.sizeDelta:(315.76, 31.93)
Frame:47 - RectTransform.sizeDelta:(315.76, 31.93)
Frame:48 - RectTransform.sizeDelta:(315.76, 31.93)
Frame:49 - RectTransform.sizeDelta:(315.76, 31.93)

Object 2:
Frame:115 - Instantiate - RectTransform.sizeDelta:(500.00, 0.00)
Frame:116 - RectTransform.sizeDelta:(315.76, 19.00)
Frame:117 - RectTransform.sizeDelta:(315.76, 31.93)
Frame:118 - RectTransform.sizeDelta:(315.76, 55.86)
Frame:119 - RectTransform.sizeDelta:(315.76, 55.86)
Frame:120 - RectTransform.sizeDelta:(315.76, 55.86)

This is the logs for 2 objects instantiated by the system. When instantiated the sizeDelta is the one at the prefab even though I already set the text before logging. Then it takes several frames to reach the stable sizesDelta, the Y is the automatically calculated value, X is set directly after instantiation.

Any ideas how I can have that deltaSize at the same time I instantiate the objects? because I need that value to position them properly before any animation on them.

Cheers.

So nobody…

This is crazy nobody has a solution. I try to do any of the following after setting sizeDelta:

  1. Call Canvas.ForceUpdateCanvases()

  2. Call ForceRebuildLayoutImmediate on each child LayoutGroup:

var groups = root.GetComponentsInChildren<T>();
if (groups == null) return;

// Assume groups are returned in order of hierarchy. Rebuild from the bottom up.
int i = groups.Length;
while (i --> 0) {
  LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)groups.transform);
}

But this doesn’t even work all the time, and I end up having to wait a frame anyway. Curious if you’ve found a better solution

It is been a while, sorry I didn’t see your reply @j1mmie

I will try that soon