C# 为什么Unity卡在Application.EnterPlayMode上?

C# 为什么Unity卡在Application.EnterPlayMode上?,c#,unity3d,perlin-noise,C#,Unity3d,Perlin Noise,我正在尝试使用柏林噪声和统一的行进立方体创建程序性地形生成器。它一直在工作,直到我从创建高度贴图切换到将其制作成3d阵列。然后,每当我点击play时,unity就会打开一个对话框,上面写着Application.EnterPlayMode,它不会消失,也不会进入play模式。当它发生时,一切都会停止响应,他们唯一停止响应的方法就是在任务管理器中杀死它 所讨论的脚本如下: using System.Collections; using System.Collections.Generic; usi

我正在尝试使用柏林噪声和统一的行进立方体创建程序性地形生成器。它一直在工作,直到我从创建高度贴图切换到将其制作成3d阵列。然后,每当我点击play时,unity就会打开一个对话框,上面写着Application.EnterPlayMode,它不会消失,也不会进入play模式。当它发生时,一切都会停止响应,他们唯一停止响应的方法就是在任务管理器中杀死它

所讨论的脚本如下:

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;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类噪音:单一行为
{
//确定是否显示调试值
public bool debug=false;
//确定地形的平坦度
公共浮子噪声标度=0.5f;
//使用的柏林噪声类型
公共枚举柏林噪声{
二,
三
};
公共PerlinNoise perlinNoiseDimension=PerlinNoise.twoD;
//在所有计算之后返回噪波数据的步骤
公共浮点[,]getterrandata(整数x,整数y,整数z)
{
浮动[,]地形数据=新浮动[x,y,z];
if(perlinNoiseDimension==PerlinNoise.twoD)
{
terrainData=PerlinNoise2D(x,y,z,noiseScale);
}
回归水陆;
}
//使用2D柏林噪波确定噪波值
私有浮点[,]PerlinNoise2D(整数x,整数y,整数z,浮点noiseScale)
{
浮动[,]体素高度=新浮动[x,y,z];
如果(调试)
{
Log(“Heightmap”);
}
//要从中取样的原点
浮点xOrg=随机范围(0.0f,0.9999999f);
浮动约尔格=随机范围(0.0f,0.9999999f);
对于(int currentx=0;currentx对于(int currentz=0;currentz,您将在

for(int currentz = 0; currentz <= height; z++)
{
    voxelHeights[currentx, currenty, currentz] = 1;
}
然而,我不确定高度是如何在这里起作用的,因为我希望它看起来更像例如

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

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

derHugo的答案在我看来非常正确。我要补充的是,我不推荐Unity的Mathf.PerlinNoise函数。Perlin可能是标志性的噪波名称,但它产生可见的网格对齐,可能不再是最佳选择。我会寻找一个好的单纯形噪波实现来导入。Tha你的山脉不会都成45度和90度的直线。
for(int currentz = 0; currentz < z; currentz++)
{
    voxelHeights[currentx, currenty, currentz] = height;
}