【发布时间】:2019-12-17 21:19:48
【问题描述】:
我正在使用 Unity 设计的游戏中以编程方式创建按钮。我实例化按钮预制件并将它们作为画布上的 Panel 对象的父级。我已将面板设置为具有以下属性的水平布局组:
我已将以下组件添加到按钮预制件中以实现我需要的外观:
做完这一切之后,我得到的结果是:
因此,我的问题是,如何删除每个按钮之间的间距?这些按钮应该彼此相邻(但不重叠)以看起来像一个句子。我有单独按钮的原因是用户需要单击句子中正确的 2 个字母字符(在上面的示例中是字母 ar)。我已将属性上的间距设置为 0 并选中/取消选中各种选项,但没有运气!
我在 Panel 和 Button 对象上都取消了 Child Force Expand 的宽度选项,但句子看起来都塞满了....如何确保按钮一个接一个整齐地放置(即不重叠)?
与以下评论相关的代码:
for (int i = 0; i < letterArray.Length; i++)
{
button = (GameObject)Instantiate(buttonPrefab);
button.transform.SetParent(buttonPanel.transform, false);
button.GetComponentInChildren<Text>().text = letterArray[i];
FitButtonWidthToText();
}
}
public void FitButtonWidthToText()
{
int widthOfTheText = 0;
CharacterInfo characterInfo;
foreach (char c in button.GetComponentInChildren<Text>().text)
{
button.GetComponentInChildren<Text>().font.GetCharacterInfo(c, out characterInfo, button.GetComponentInChildren<Text>().fontSize);
widthOfTheText += characterInfo.advance;
}
buttonTransform.sizeDelta = new Vector2(widthOfTheText, buttonTransform.sizeDelta.y);
}
【问题讨论】:
标签: c# unity3d button layout spacing