如何调试规模问题? [英] How to debug a scale issue?

查看:48
本文介绍了如何调试规模问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理商店页面.该页面充满了面板预制件.出于某种原因,当它填充预制件时,它是预制件尺寸的两倍多.如果我将预制件拖到位,它会按预期工作.

I am working on a store page. The page is filled with panel prefabs. For some reason when it fills the prefabs it is more than doubling the scale size of the prefab. If I drag the prefab into place it works as expected.

我在计划目标范围内没有任何代码.我不确定它来自哪里.

I don't have any code in the program targeting scale. I am not sure where it is coming from.

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

This is the code I am using for the list population.

[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);
        }
    }
}

简单对象池脚本:

// 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;
}

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

How can I debug to figure out where the issue is coming from. There is only one thing in here affecting the transform and that shouldn't adjust the scale.

推荐答案

答案是:

newButton.transform.SetParent(contentPanel, false);

newButton.transform.SetParent(contentPanel, false);

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

//这使玩家保持其局部方向而不是全局方向.player.transform.SetParent(newParent, false);

//this makes the player keep its local orientation rather than its global orientation. player.transform.SetParent(newParent, false);

这篇关于如何调试规模问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆