【问题标题】:How to debug a scale issue?如何调试规模问题?
【发布时间】:2016-11-15 22:54:17
【问题描述】:

我正在开发商店页面。该页面充满了面板预制件。由于某种原因,当它填充预制件时,它比预制件的比例尺寸增加了一倍多。如果我将预制件拖到适当的位置,它会按预期工作。

我在计划目标规模中没有任何代码。我不确定它来自哪里。

这是我用于列表填充的代码。

[System.Serializable]
public class Item
{
    public string itemName;
    public int price;
}

public class ShopScrollList : MonoBehaviour {
    public List<Item> itemList;
    public Transform contentPanel;
    public Text storeDisplayText;
    public SimpleObjectPool buttonObjectPool;

    void Start () {
        RefreshDisplay();
    }


    private void RefreshDisplay()
    {
        AddButtons();
    }

    private void AddButtons()
    {
        for (int i = 0; i < itemList.Count; i++)
        {
            Item item = itemList[i];
            GameObject newButton = buttonObjectPool.GetObject();
            newButton.transform.SetParent(contentPanel);

            ButtonDetails buttonDetails = newButton.GetComponent<ButtonDetails>();
            buttonDetails.Setup(item, this);
        }
    }
}

SimpleObjectPool 脚本:

// A very simple object pooling class
public class SimpleObjectPool : MonoBehaviour
{
    // the prefab that this object pool returns instances of
    public GameObject prefab;
    // collection of currently inactive instances of the prefab
    private Stack<GameObject> inactiveInstances = new Stack<GameObject>();

    // Returns an instance of the prefab
    public GameObject GetObject()
    {
        GameObject spawnedGameObject;

        // if there is an inactive instance of the prefab ready to return, return that
        if (inactiveInstances.Count > 0)
        {
            // remove the instance from teh collection of inactive instances
            spawnedGameObject = inactiveInstances.Pop();
        }
        // otherwise, create a new instance
        else
        {
            spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);

            // add the PooledObject component to the prefab so we know it came from this pool
            PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
            pooledObject.pool = this;
        }

        // put the instance in the root of the scene and enable it
        spawnedGameObject.transform.SetParent(null);
        spawnedGameObject.SetActive(true);

        // return a reference to the instance
        return spawnedGameObject;
    }

    // Return an instance of the prefab to the pool
    public void ReturnObject(GameObject toReturn)
    {
        PooledObject pooledObject = toReturn.GetComponent<PooledObject>();

        // if the instance came from this pool, return it to the pool
        if (pooledObject != null && pooledObject.pool == this)
        {
            // make the instance a child of this and disable it
            toReturn.transform.SetParent(transform);
            toReturn.SetActive(false);

            // add the instance to the collection of inactive instances
            inactiveInstances.Push(toReturn);
        }
        // otherwise, just destroy it
        else
        {
            Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
            Destroy(toReturn);
        }
    }
}

// a component that simply identifies the pool that a GameObject came from
public class PooledObject : MonoBehaviour
{
    public SimpleObjectPool pool;
}

如何调试以找出问题的根源。这里只有一件事会影响变换,不应该调整比例。

【问题讨论】:

  • 如果没有 SimpleObjectPool 脚本,几乎无法提供帮助。
  • 我已经添加了 SimpleObjectPool 脚本
  • 好的。将spawnedGameObject = (GameObject)GameObject.Instantiate(prefab); 替换为spawnedGameObject = (GameObject)Instantiate(prefab);。让我知道问题是否仍然存在。
  • 这并没有解决问题
  • spawnedGameObject = (GameObject)Instantiate(prefab, prefab.transform.position, prefab.transform.rotation); spawnedGameObject.transform.localScale = prefab.transform.localScale; 怎么样?哎呀。我也忘记了秤。看看编辑。

标签: unity3d scale


【解决方案1】:

答案是:

newButton.transform.SetParent(contentPanel, false);

https://docs.unity3d.com/ScriptReference/Transform.SetParent.html

//这使播放器保持其本地方向而不是其全局方向。 player.transform.SetParent(newParent, false);

【讨论】:

  • 其实我之前已经看到过这个问题,这就是解决方案。刚刚看到你现在对newButton.transform.SetParent(contentPanel); 发表评论。
猜你喜欢
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
相关资源
最近更新 更多