I’ve seen a couple of threads here about getting the size of a TextMeshPro object using Object.bounds. (
TextMeshProUGUI get the width (prefersize) of the rendered text )
I’ve seen a post that says if you need the size of the object immediately, you need to use theForceMeshUpdate()
Method.
I’m trying to build and size a dynamic popup window, so I’ve attempted the following, but the Object.bounds.size.x
is always 0. I’m sure I’m doing something really dumb but at the moment, I’m completely unsure what that might be.
foreach ( string val in values ) {
GameObject row = Instantiate(_cellInfoRowPrefab);
TextMeshProUGUI labelField = row.GetComponentsInChildren()[0];
TextMeshProUGUI valueField = row.GetComponentsInChildren()[1];
if ( firstRow ) {
labelField.text = label.Length > 0 ? $“{label} :” : “”;
firstRow = false;
}
else {
labelField.text = “”;
}
labelField.ForceMeshUpdate();
valueField.text = val.Trim();
valueField.ForceMeshUpdate();
Util.DebugMessage($“valueField: {valueField.bounds.size.x} labelField: {labelField.bounds.size.x}”);
if ( labelField.bounds.size.x > _maxLabelFieldWidth ) _maxLabelFieldWidth = labelField.bounds.size.x;
if ( valueField.bounds.size.x > _maxValueFieldWidth ) _maxValueFieldWidth = valueField.bounds.size.x;
_infoRows.Add(row);
}