Content size fitter does not update rect transform immediately

Hello,

I’m trying to update a text component multiple times in a single frame within a coroutine so that it will update it’s associated RectTransform. However, I can get the rect transform to update if my code contains a yield return null statement.

Here is my process for a single frame:

  1. Start the coroutine
  2. Instantiate a new object which has horizontal layout, content fitter, text, and recttransform components.
  3. In a for loop, I update/increase the text value of the text component in order to increase the width of the associated rect transform.
  4. After each iteration of the loop I check the sizeDelta.x of the rect transform to see how much the overall width of the rect transform has changed.
  5. End coroutine

At every iteration in the for loop, I check the width of the of the rect transform, hoping that it will have a larger width value (which it should because of the aforementioned horizontal layout and content fitter components). However, after each iteration, i read the rect transforms width value, and it’s always the same - the starting value at which it was instantiated.

I can get the rect transforms width to update if I include a yield return null at the end of the loop, but I would prefer to not have to do this as it means that I have to wait frames in order to continually update the rect transforms width.

Am I overlooking something? Is it possible to update a rect transforms width multiple times in a single frame?

Hey, probably too late to help you, but perhaps someone else will read this.

I had a similar problem before and ended up using something like this:

  // write title one letter at a time
  foreach (char c in textString) {  
    textComponent.text += c;
    yield return new WaitForSeconds (writeTextDelay);
  }
  
  // Wait for text to size itself
  // Then update the size of the passage so the next passage will go under it
  yield return new WaitForEndOfFrame ();
  _currentPageHeight += title.GetComponent <RectTransform> ().rect.height;

Basically, by using WaitForEndOfFrame you can wait for LateUpdate (where all the rects get resized) and get the rect size in the same frame. You still can’t do this multiple times in one frame though. :confused: Oh well. That may solve your problem, it did mine.

You can check out more info here: http://www.samuraifight.com/2015/11/15/unity-coroutines/