2020年01月11日

文字列に合わせたUI Textのサイズを取得する方法

UnityのUI.Textで、実際にテキストを表示した際のサイズが知りたい場合はpreferredWidthpreferredHeightを使って調べることができるそうです。

使い方

Text text;

float width = text.preferredWidth;
float height = text.preferredHeight;

テキストの長さに合わせてUIを調整したい場合に便利かなと思います。

リンク

Unity - Scripting API: ILayoutElement
https://docs.unity3d.com/ScriptReference/UI.ILayoutElement.html
posted by unity-chan at 15:52 | Script