環境
Unity3D 5.3.6f1
練習地址:https://github.com/zhaoqingqing/UGUIDemo 布局放在Layout文件夾
文檔:https://docs.unity3d.com/Manual/script-ContentSizeFitter.html
button根據Text自適應
效果如下:Button的長度會根據Text的長度自動變化
Button的設置如下:
1、添加HorizontalLayoutGroup,並設置Padding,就是左右兩邊的空白,因為我們是想讓Button的長度(水平方向)做變化,所以我們添加Horizontal組件。
2、添加 ContentSizeFitter,這個組件的Horizontal Fit(水平適配)設置了PreferredSize(優先尺寸),當Text的PreferrdWidth發生改變之后,它就會改變Button的長度。
ContentSizeFitter
ContentSizeFitter的描述:The size is determined by the minimum or preferred sizes provided by layout element components on the Game Object. Such layout elements can be Image or Text components, layout groups, or a Layout Element component.
需要和Layout系列的組件結合使用。


