在上一篇文章中,我询问了如何调试扩展问题:Debugging Scale
解决问题之后,似乎还有另一个问题,我不明白。我正在尝试删除我的按钮列表并填充新的按钮列表(刷新)。
我打破了删除按钮的功能,然后再添加它们。我使用的是设置原始按钮的相同脚本MakeAllButtons
。
当我删除按钮列表并添加按钮列表时,它看起来像这样。缩小比例为.3。
如果我添加另一个按钮列表(只是再次单击Add
),它看起来像这样:
按钮列表的比例返回到它所假设的位置。
添加按钮代码:
public void MakeAllButtons()
{
for (int i = 0; i < positionList.Count; i++)
{
Position position = positionList[i];
PlayerPrefs.SetInt("PositionUnlocked" + i, position.unlocked); //sets if the item is unlocked
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(positionPanel, false); //this was the problem originally
ButtonDetails buttonDetails = newButton.GetComponent<ButtonDetails>();
buttonDetails.SetupPosition(position, this);
}
}
删除按钮代码
public void RemoveButtons()
{
while (positionPanel.childCount > 0)
{
GameObject toRemove = positionPanel.transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
创建newButton
之后,newButton.localScale
被设置为.3,但仅限于第一次运行代码。
答案 0 :(得分:0)
当我移除对象时,它似乎存储了之前加载的比例。
所以我强迫新对象拥有我想要的比例,并解决了问题。
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.localScale = new Vector3(1, 1, 1);
newButton.transform.SetParent(positionPanel, false);