I use UI to display a scroll panel in which I put several Texts dynamically.
These Text elements have a content size fitter.
If I try to get the size of these Texts right after I create it, I get the default size, not the size after the content size fitter was applied.
I search about a callback called when the text was resized by the fitter but I found anything.
How can I detect when the content size fitter was applied ?
Edit: Found a solution, but a not-so-neat one. I’ve put it in a coroutine and this does work.
Same situation as klem described, assigned a new text, but the size keeps the old one.
Does it has something to do with the frames of something?
Anyway, put it in an coroutine and it gives the new size immediately
Hi, I meet the similar situation. I want to get the current text game object’s width of its rect transform.
But the width value seems to be always delayed, maybe it will be calculated in next frame by ContentSizeFitter component. After I check the Unity document, I found the system will call SetLayoutHorizontal method to adjust the size of a game object automatically. So, I force it to adjust the size of game object immediately by calling that method. Then problem solved.
use this
text.rectTransform.sizeDelta = new Vector2 (text.preferredWidth,text.preferredHeight);
backGround.sizeDelta = new Vector2 (text.preferredWidth+20.0f,text.preferredHeight+20.0f);