UGUI ContentSizeFitter之Button根據Text自適應


環境

Unity3D 5.3.6f1

練習地址:https://github.com/zhaoqingqing/UGUIDemo  布局放在Layout文件夾

文檔:https://docs.unity3d.com/Manual/script-ContentSizeFitter.html

button根據Text自適應

效果如下:Button的長度會根據Text的長度自動變化

button根據Text做適應

 

Button的設置如下:

image

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系列的組件結合使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM