all green

プログラム、アプリ作成、Web等備忘録が中心です

uGUIのText表示サイズをテキスト内容に合わせたサイズに変更する(Unity 5.3.1f1)

テキストの内容にあわせてTextの幅や高さを変更したい場合

環境
Unity 5.3.1f1

// 幅と高さをテキスト内容に合わせる
private void AdjustSize()
{
	Text text = GetComponent<Text>();
	text.rectTransform.sizeDelta = new Vector2(text.preferredWidth, text.preferredHeight);
}

// 高さをテキスト内容に合わせる(折り返し有効)
private void AdjustHeight()
{
	Text text = GetComponent<Text>();
	text.rectTransform.sizeDelta = new Vector2(text.rectTransform.sizeDelta.x, text.preferredHeight);
}