LoginSignup
8
10

More than 5 years have passed since last update.

見出しを表示する。uGUI。文字の長さに合わせて背景のサイズ変更したい。

Last updated at Posted at 2019-04-09

こういうの。

スクリーンショット 2019-04-10 10.22.43.png
0409.gif

最初思いついたのは、
1. Image を Text の子に設定する。
2. Text に、Content Size Fitter を追加する。
3. すると、Imageがテキストより前に描画されて、テキストが見えない・・・。

ダメだったので
他の方法を探しました。

1. Text を Image の子に設定する。

スクリーンショット 2019-04-09 21.23.52.png

2. Image に、Horizontal Layout Group を追加する。

「Child Controls Size」  の、 「Width」 をオンにする。 他はオフ。

スクリーンショット 2019-04-09 21.28.19.png

3. Image に、Content Size Fitter を追加する。

「Horizontal Fit」 を 「Preferred Size」 にする。

スクリーンショット 2019-04-09 21.28.01.png

設定は以上です。
あとは、

テキストを中央揃えにして

スクリーンショット 2019-04-09 21.29.14.png

padding とか設定すると良いと思います。

スクリーンショット 2019-04-09 21.38.04.png

参考

Unity - Manual: Making UI elements fit the size of their content
UI 要素をコンテンツサイズに合わせる - Unity マニュアル
Best way to make a background panel for a Text that has a ContentSizeFitter - Unity Forum
Scaling background with text: Detecting horizontal overflow? - Unity Forum

8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10