AFAIK there is no solution out of the box for your problem… so you would have to write your own content size fitter (maybe you can extend the existing one).
However, it is almost always a bad idea to scale UI elements. It leads to unexpected behavior in many cases. In the very most cases there is a way to achieve your goals without scaling.
I see you have used stretched panel, so it’s one case. Then again, if you have not split anchors… it’s different.
Maybe just do it with code?
I tried to solve it, have done something similar as a practice. It’s crude but should work. Text can be scaled, I didn’t take into account moving pivots or parent scaling… but this way panel should be fitting text rect that is child of this panel.
NOTE: this only works if child text is centered unlike your pivot, that is in upper left corner. Stretched text won’t work either…
// Dimensions
sizeFinalX = textRt.sizeDelta.x * textRt.localScale.x;
sizeFinalY = textRt.sizeDelta.y * textRt.localScale.y;
splitAnchors = Vector3.Distance(panelRt.anchorMin, panelRt.anchorMax) > 0.001f;
// Anchors split... above not working, use this
if (splitAnchors)
{
splitAnchors = true;
panelRt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, sizeFinalX);
panelRt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, sizeFinalY);
}
else
{
// Anchors not split, center pivot
panelRt.sizeDelta = new Vector2(sizeFinalX, sizeFinalY);
}
…maybe it’s helpful… it does not use scaling, instead panel is resized.
Thanks! Using script would be fine, since i’m settings the text from script anyways…
but got it actually working, had forgotten that can set the canvas dynamic pixel scale instead of scaling text…(to get sharp font), so can remove scaling from text component.
Blast from the past… had this issue today, found another workaround:
Add dummy gameobject before the panel AND scale that down
then you dont need to have scaled text or panel, which means: content size fitter and layout group will resize the panel correctly to the child text size.