如何将固定宽度的重叠元素放在特定空间内?

时间:2019-03-20 12:14:52

标签: unity3d math

如何在屏幕上的固定空间内均匀地放置一定宽度的UI元素?我想计算将元素放置在空间中的位置,而无需缩放或旋转元素。

例如:我想将4张宽度为500单位的卡片放入宽度为1000单位的底部白色空间内。 enter image description here

预期结果:放置四张卡,使其占据1000个单位。 enter image description here

如果有八张卡,则预期结果。 enter image description here

1 个答案:

答案 0 :(得分:1)

card_offset = (total_width - card_width) / (number_of_cards - 1) * card_index;

示例:

1st card offset = (1000 - 500) / (4 - 1) * 0 = 0
2st card offset = (1000 - 500) / (4 - 1) * 1 = 167
3st card offset = (1000 - 500) / (4 - 1) * 2 = 334
4st card offset = (1000 - 500) / (4 - 1) * 3 = 500
相关问题