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

posted @ 2017-06-22 17:59  赵青青  阅读(2967)  评论(0编辑  收藏  举报