您的位置:首页 > 产品设计 > UI/UE

UGUI ContentSizeFitter之Button根据Text自适应

2017-06-22 17:59 393 查看

环境

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系列的组件结合使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: