为什么 Unity 会卡在 Application.EnterPlayMode 上?

Rat*_*eep 4 c# unity-game-engine perlin-noise

我正在尝试使用柏林噪声和统一的行进立方体来创建程序地形生成器。它一直有效,直到我从创建高度图切换到将其制作成 3d 数组。然后,每当我单击播放时,Unity都会打开一个对话框,其中写入了Application.EnterPlayMode,该对话框不会消失并且永远不会进入播放模式。当发生这种情况时,一切都会停止响应,阻止它的唯一方法就是在任务管理器中杀死它。

有问题的脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Noise : MonoBehaviour
{
    //Determines whether to show debug values
    public bool debug = false;

    //Determines flatness of the terrain
    public float noiseScale = 0.5f;

    //Type of perlin noise to use
    public enum PerlinNoise {
        twoD,
        threeD
    };
    public PerlinNoise perlinNoiseDimension = PerlinNoise.twoD;

    //To return noise data after all calculations
    public float[,,] getTerrainData(int x, int y, int z)
    {
        float[,,] terrainData = new float[x, y, z];

        if(perlinNoiseDimension == PerlinNoise.twoD)
        {
            terrainData = PerlinNoise2D(x, y, z, noiseScale);
        }
        return terrainData;
    }

    //Determine noise values using 2D Perlin noise
    private float[,,] PerlinNoise2D(int x, int y, int z, float noiseScale)
    {
        float[,,] voxelHeights = new float[x, y, z];

        if (debug)
        {
            Debug.Log("Heightmap");
        }

        //Origin points to sample from
        float xOrg = Random.Range(0.0f, 0.9999999f);
        float yOrg = Random.Range(0.0f, 0.9999999f);

        for (int currentx = 0; currentx < x; currentx++)
        {
            for (int currenty = 0; currenty < y; currenty++)
            {
                //Convert Values to Fractions
                float xCoord = (float)currentx / (x * noiseScale) + xOrg;
                float yCoord = (float)currenty / (y * noiseScale) + yOrg;

                float height = Mathf.PerlinNoise(xCoord, yCoord) * z;

                for(int currentz = 0; currentz <= height; z++)
                {
                    voxelHeights[currentx, currenty, currentz] = 1;
                }

                if (debug)
                {
                    Debug.Log("Height = " + height + ", X = " + currentx + ", Y = " + currenty + ", X Coord = " + xCoord + ", Y Coord = " + yCoord);
                }
            }

        }
        return voxelHeights;
    }
}
Run Code Online (Sandbox Code Playgroud)

它所显示的图像如下:

错误

der*_*ugo 7

你正在造成一个永无休止的循环

for(int currentz = 0; currentz <= height; z++)
{
    voxelHeights[currentx, currenty, currentz] = 1;
}
Run Code Online (Sandbox Code Playgroud)

这里你正在增加z++,但你的循环条件是打开的currentz <= height。在循环中,您永远不会更新 的值,currentz也不会更新 的值height,因此循环永远不会结束。

由于索引的使用,它可能应该是

for(int currentz = 0; currentz <= height; currentz++)
{
    voxelHeights[currentx, currenty, currentz] = 1;
}
Run Code Online (Sandbox Code Playgroud)

但是不确定它是如何height在这里发挥作用的,因为我希望它看起来有点像例如

for(int currentz = 0; currentz < z; currentz++)
{
    voxelHeights[currentx, currenty, currentz] = height;
}
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎更有意义。