【问题标题】:C# how would i split a 2D array into an arbitrary amount of 2D arraysC#如何将二维数组拆分为任意数量的二维数组
【发布时间】:2017-10-23 17:58:40
【问题描述】:

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

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

但是我一直找不到合适的方法来抓取“块”并将它们复制到单独的数组中。

【问题讨论】:

  • 很遗憾,您不能拆分二维数组。但是,您可以将其中的片段复制到新的可能更小的二维数组中。
  • @Vikhram 这正是我想要做的,我只是无法弄清楚如何告诉它从哪里开始较大数组中的每个块以将其值复制到较小的数组.
  • 目前,您的问题有很多不相关的代码。如果您可以删除不相关的代码并告诉我们您想如何拆分数组data[rows, columns],我也许可以提供帮助。事实上,想象一下存在一个完全符合您要求的功能。你需要为此编写代码,而你不知道的编写伪代码
  • 我注意到您已在 9 小时前编辑了您的问题。你有没有看到我的回答?
  • @KyleDelaney 我确实注意到了你的回答,还没有机会去看看

标签: c# arrays unity3d


【解决方案1】:

这个怎么样?

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.
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 2020-11-18
    • 2022-07-06
    • 2016-08-10
    • 1970-01-01
    • 2016-03-15
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多