UnityのUI.Text
で、実際にテキストを表示した際のサイズが知りたい場合はpreferredWidth
やpreferredHeight
を使って調べることができるそうです。
使い方
Text text;
float width = text.preferredWidth;
float height = text.preferredHeight;
テキストのサイズに合わせてUIを調整したい場合に便利かなと思います。
TextMeshPro
TexhMeshProの場合もpreferredWidth
とpreferredHeight
を使って実際のサイズを調べることができるようです。
使い方
TMPro.TextMeshProUGUI text;
float width = text.preferredWidth;
float height = text.preferredHeight;
リンク
Unity – Scripting API: ILayoutElement
https://docs.unity3d.com/ScriptReference/UI.ILayoutElement.html
Class TextMeshProUGUI | TextMeshPro | 1.5.6
https://docs.unity3d.com/Packages/com.unity.textmeshpro@1.5/api/TMPro.TextMeshProUGUI.html