为什么当我 clone/duplicate gameobject 内存使用率达到 98%?

Why when i clone/duplicate gameobject the memory usage is getting to 98%?

void CreateCubesBesideWaypoints()
    {
        const int lightCount = 20;
        Vector3[] lightPositions = new Vector3[lightCount];

        for (int i = 0; i < waypoints.Length - 1; i++)
        {
            posToChunkDistances(waypoints[i].transform.position, waypoints[waypoints.Length - 1].transform.position, lightPositions, lightCount);
            for (int x = 0; x < lightPositions.Length; x++)
            {
                lightPrefab.GetComponent<Renderer>().material.color = Color.red;
                GameObject cloneLightPrefab = Instantiate(lightPrefab, lightPositions[x], Quaternion.identity);
                cloneLightPrefab.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                cloneLightPrefab.tag = "LightPrefab";
                //cloneLightPrefab.transform.SetParent(lightPrefab.transform);
            }
        }
    }

这样就可以正常工作了。但是如果我使用这条线:

cloneLightPrefab.transform.SetParent(lightPrefab.transform);

然后内存使用率达到 98%,我需要重新启动电脑。 lightPrefab 是我拖到此脚本的 Cube GameObject。 我想要做的是将所有新的 cloneLightPrefab 游戏对象放在 lightPrefab 下,如 children.

还有一个子题,在内循环就行了:

for (int x = 0; x < lightPositions.Length; x++)

我应该把它改成:

for (int x = 0; x < lightPositions.Length - 1; x++)

你的代码中有很多问题,所以这个答案会很长。

1。首先,你不需要外循环:for (int i = 0; i < waypoints.Length - 1; i++)。请删除它。这没有包含在我的上一个答案中,我不知道你为什么把它添加到那里。这将增加您在循环中花费的时间。

2。您在修改预制件时 lightPrefab.GetComponent<Renderer>().material.color = Color.red; 您应该修改克隆的对象。 cloneLightPrefab.GetComponent<Renderer>().material.color = Color.red;

3.不要将实例化对象的parent设置为prefab。这就是你用 cloneLightPrefab.transform.SetParent(lightPrefab.transform);.

做的

创建一个空的游戏对象,然后将其用作您实例化的所有灯光对象的父对象。 GameObject parentObject = new GameObject("LightParentObj"); 然后 cloneLightPrefab.transform.SetParent(parentObject.transform);.

如果您还不知道如何创建预制件,请查看 this post。

您的固定代码应该看起来更像这样:

void CreateCubesBesideWaypoints()
{
    const int lightCount = 20;
    Vector3[] lightPositions = new Vector3[lightCount];

    posToChunkDistances(waypoints[0].transform.position, waypoints[waypoints.Length - 1].transform.position, lightPositions, lightCount);
    GameObject parentObject = new GameObject("LightParentObj");
    for (int x = 0; x < lightPositions.Length; x++)
    {
        GameObject cloneLightPrefab = Instantiate(lightPrefab, lightPositions[x], Quaternion.identity);
        cloneLightPrefab.GetComponent<Renderer>().material.color = Color.red;
        cloneLightPrefab.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        cloneLightPrefab.tag = "LightPrefab";
        cloneLightPrefab.transform.SetParent(parentObject.transform);
    }
}

Should i change it to: for (int x = 0; x < lightPositions.Length - 1; x++)

当你有 for (int x = 0; x < lightPositions.Length; x++), 循环从 0 开始,到 lightPositions.Length - 1 结束。

如果将其更改为for (int x = 0; x < lightPositions.Length - 1; x++),循环将从0开始并以lightPositions.Length - 2结束。基本上,循环不会完成对数组中所有项目的循环。

我认为您应该 Google 并理解 for 在 C# 中使用数组循环,然后再继续编码。这是您必须知道的基本内容之一。

注意:

使用 for (int x = 0; x < lightPositions.Length - 1; x++) 可能没问题的情况之一是使用 <= 而不是 <

例如,for (int x = 0; x <= lightPositions.Length - 1; x++)。在这种情况下,循环仍将遍历数组中的所有项目。