【问题标题】:How to prevent Scale from changing Sizes?如何防止Scale改变Size?
【发布时间】:2017-04-06 13:14:46
【问题描述】:

在之前的帖子中,我询问了如何调试规模问题: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,但仅限于代码的第一次运行。

【问题讨论】:

    标签: c# unity3d scale


    【解决方案1】:

    看来,当我移除对象时,它正在存储上一次加载的比例。

    所以我强制新对象具有我想要的比例,它解决了问题。

            GameObject newButton = buttonObjectPool.GetObject();
            newButton.transform.localScale = new Vector3(1, 1, 1);
            newButton.transform.SetParent(positionPanel, false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多