Question by
Phedg1 · Feb 16, 2016 at 09:35 AM ·
unity 5recttransform
Cant Get Width Height Of Content Size Fitter RectTransform
I have a GameObject with a UI text element and a content size fitter attached. It seems that once it is attached you can no longer retrieve the width/height of the recttransform of the object in script. Neither method I know works. They all return 0.
gameObject.GetComponent().rect.size
gameObject.GetComponent().rect.height
gameObject.GetComponent().sizeDelta
Does anybody know a way to still get these values?
Comment
Best Answer
Answer by Phedg1 · Feb 17, 2016 at 12:21 AM
The solution, it seems, it not to query the RectTransform, but instead the Text component.
gameObject.GetComponent<Text>().preferredHeight;
gameObject.GetComponent<Text>().preferredWidth;
And what about if there is no Text component attached, then how can i get preferredHeight of gameObject ?
Okay got it, by using LayoutUtility.GetPreferredHeight(RectTransform rect)
Answer by $$anonymous$$ · Mar 31, 2017 at 03:58 PM
Do you know by any chance how to acces the height of an object when it is not a text? I have a simple panel for my main menu (it's height is also driven by a content size fitter) and i am struggeling to acces the height of it. This is driving me insane, why is it so hard to do such a simple thing?! please respond, when you have any tips for me!Phedg1
by using LayoutUtility.GetPreferredHeight(RectTransform rect)
Your answer
