簡體   English   中英

如何防止秤改變尺寸?

[英]How to prevent Scale from changing Sizes?

在上一篇文章中,我詢問了如何調試秤的問題: 調試秤

解決問題后,似乎還有另一個我不了解的問題。 我正在嘗試刪除按鈕列表並填充新的按鈕列表(刷新)。

我介紹了刪除按鈕然后再添加回來的功能。 我使用的是用於設置原始按鈕的相同腳本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,但僅適用於代碼的首次運行。

看來,當我刪除對象時,它正在存儲上一次加載的比例。

因此,我強制新對象具有所需的比例,從而解決了該問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM