C#如何将2D数组拆分为任意数量的2D数组

时间:2017-10-23 17:58:40

标签: c# arrays unity3d

我有以下脚本,我正在尝试确定如何将hts拆分成较小的块以分配给它正在填充的各种地形。

public class TerrainGenerator : MonoBehaviour {
    private void ApplyHeights(float[,] hts) {
        for(int x = 0; x < xSize; x++) {
            for(int z = 0; z < zSize; z++) {
                float[,] h = new float[template.terrainData.heightMapWidth, template.terrainData.heightMapHeight];
                h = Split2DFloatArray(template.terrainData.heightMapWidth, template.terrainData.heightMapHeight,x*template.terrainData.heightMapWidth, z*template.terrainData.heightMapHeight);
    }
}

但是我一直无法找到合适的方法来拆分数组,而我发现的问题涉及锯齿状数组并且没有答案。

1 个答案:

答案 0 :(得分:0)

这个怎么样?

private void ApplyHeights(float[,] hts)
{
    int hwidth = template.terrainData.heightMapWidth;
    int hheight = template.terrainData.heightMapHeight;
    for (int x = 0; x < xSize; x++)
    {
        for (int z = 0; z < zSize; z++)
        {
            float[,] h = new float[hwidth, hheight];
            int offsetX = x * hwidth;
            int offsetZ = z * hheight;
            for (int hx = 0; hx < hwidth; hx++)
            {
                for (int hz = 0; hz < hheight; hz++)
                {
                    h[hx, hz] = hts[offsetX + hx, offsetZ + hz];
                }
            }
            // Do something with h.
        }
    }
}