解决Unity 各种LayoutGroup无法解决的 动态子成员位置不对 强制刷新布局问题

对于unity的gridLayout verticalLayout 或者horizontalLayout
经常有加入新成员或者改变成员大小后,部件大小、位置不对的问题。
一般来说,这个方式就能解决

LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransform);

这样

也有些则只能像这个链接10楼里说到的那样用开关gameobject 等一帧之类的方法解决。
比如

horizLayoutGroup.CalculateLayoutInputHorizontal();
horizLayoutGroup.CalculateLayoutInputVertical();
horizLayoutGroup.SetLayoutHorizontal();
horizLayoutGroup.SetLayoutVertical();

而上面这个链接的15楼则完美解决了我的问题,而这个问题的本质来源于
layoutgroup下的子物体有contentSizeFitter 或者dotween动画等控制其大小造成的冲突

那删掉就解决了
那么我又想要contentSizeFitter 之类的怎么办呢?
这就是这里解决的问题了

我姑且给大家翻译过来
Make the parent have the content size fitter and set either the horizontal or vertical fit to preferred size.
让有layoutGroup成员的这个父物体挂着ContentSizeFitter ,把这个ContentSizeFitter 设为horizontal或者vertical fit是preferred的状态
在这里插入图片描述
Let its immediate child be the background, and add a layout element to it to control its minimum size. Also add a layout group to it to control the text that you will put as a child to it.
让它的直辖子物体 十字军之王狂喜 是我们新建的一个background物体(有没有Image无所谓),
给他加一个Layout 元素设定其最小长宽
然后再给它加一个vertical layout group 让它的子物体Text本身来控制长宽
所以把child control size两个勾选
在这里插入图片描述
Add the text as a child of background
把text(或者别的本来的子物体)放成这个background物体的子物体
text当然要有ContentSizeFitter

在这里插入图片描述

Change the text to check the background.
改text就会自动改动background物体的长宽
contentsizefitter 达到了 效果 也没有影响主layout的布局。

猜你喜欢

转载自blog.csdn.net/weixin_42767868/article/details/125297035