この記事でのバージョン Unity 2017.2.0f3 はじめに 今回はタイトル通り、uGUIのTextで文字列がぴったり収まるサイズを取得する方法のご紹介です! preferredWidth, preferredHeight 方法は簡単、TextのpreferredWidth, preferredHeightを参照するだけ。 //Textを取得 Text text = GetComponent<Text>(); //取得したTextをピッタリ収まるようにサイズ変更 text.rectTransform.sizeDelta = new Vector2(text.preferredWidth, text.preferredHeight); これでFont, Font Style, Font Size等、諸々の数値を考慮した状態でピッタリ収まるようになります。 ただし、以下のようにTextの
![文字列がTextにピッタリ収まるサイズを取得する方法と注意点【Unity】【uGUI】 - (:3[kanのメモ帳]](https://cdn-ak-scissors.b.st-hatena.com/image/square/ee6c45e0a4daff719db82d03caed2022ba029f1f/height=288;version=1;width=512/https%3A%2F%2Fcdn.image.st-hatena.com%2Fimage%2Fscale%2Fdbdaccfbacb35b43e0734e588c2ba4f3cd27edf7%2Fbackend%3Dimagemagick%3Bversion%3D1%3Bwidth%3D1300%2Fhttps%253A%252F%252Fcdn-ak.f.st-hatena.com%252Fimages%252Ffotolife%252Fk%252Fkan_kikuchi%252F20171106%252F20171106070439.gif)