Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 生成空心立方体矩阵_C#_Unity3d_Math - Fatal编程技术网

C# 生成空心立方体矩阵

C# 生成空心立方体矩阵,c#,unity3d,math,C#,Unity3d,Math,我想生成一个具有x体积的立方体矩阵,但我只需要曲面上的立方体(我可以看到立方体)。 下一个代码就是它所做的,一个完整的矩阵。我如何得到我需要的 public class World : MonoBehaviour { public GameObject cube; public int volume; private void Awake() { for (int i = 0, x = 0; x < volume; x++)

我想生成一个具有x体积的立方体矩阵,但我只需要曲面上的立方体(我可以看到立方体)。 下一个代码就是它所做的,一个完整的矩阵。我如何得到我需要的

public class World : MonoBehaviour
{
    public GameObject cube;
    public int volume;

    private void Awake()
    {
        for (int i = 0, x = 0; x < volume; x++)
        {
            for (int y = 0; y < volume; y++)
            {
                for (int z = 0; z < volume; z++)
                {
                    i++;
                    var go = Instantiate(cube);
                    go.name = "Cube " + i;
                    go.transform.position = new Vector3
                    {
                        x = x,
                        y = y,
                        z = z
                    };
                }
            }
        }
    }
}
公共类世界:单一行为
{
公共游戏对象立方体;
公共int卷;
私人空间
{
对于(int i=0,x=0;x
只使用外部限制而忽略两者之间的任何内容如何

// ignores the cubes that are not placed on the limits
if (x != 0 && x != volume - 1 && y != 0 && y != volume - 1 && z != 0 && z != volume - 1) continue;

i++;

var go = Instantiate(cube);
go.name = "Cube " + i;
go.transform.position = new Vector3(x, y, z);
或者更容易理解

// only spawns cubes that are placed on the limits
if (x == 0 || x == volume - 1 || y == 0 || y == volume - 1 || z == 0 || z == volume - 1)
{
    i++;

    var go = Instantiate(cube);
    go.name = "Cube " + i;
    go.transform.position = new Vector3(x, y, z);
}

正如Eliasar提到的,我还建议使用比
I
更好的变量名,例如,正如您自己所说的
index
。最后,它只是一个名字,但它更干净。但是,我也建议将其移到
定义之外,如

int index = 0;
for(int x = 0; ...)
而不是

for(int index = 0 , x = 0; ...)

这很难理解

只使用外部限制而忽略两者之间的任何内容怎么样

// ignores the cubes that are not placed on the limits
if (x != 0 && x != volume - 1 && y != 0 && y != volume - 1 && z != 0 && z != volume - 1) continue;

i++;

var go = Instantiate(cube);
go.name = "Cube " + i;
go.transform.position = new Vector3(x, y, z);
或者更容易理解

// only spawns cubes that are placed on the limits
if (x == 0 || x == volume - 1 || y == 0 || y == volume - 1 || z == 0 || z == volume - 1)
{
    i++;

    var go = Instantiate(cube);
    go.name = "Cube " + i;
    go.transform.position = new Vector3(x, y, z);
}

正如Eliasar提到的,我还建议使用比
I
更好的变量名,例如,正如您自己所说的
index
。最后,它只是一个名字,但它更干净。但是,我也建议将其移到
定义之外,如

int index = 0;
for(int x = 0; ...)
而不是

for(int index = 0 , x = 0; ...)

这很难理解

回答得很好!我会使用比
I
更好的变量名,因为它不用于迭代。类似于
count
的东西可能会很好。@Eliasar好的,是indexI的i吗?我会假设您在使用i进行当前计数时,正在迭代x、y和z。回答得很好!我会使用比
I
更好的变量名,因为它不用于迭代。类似于
count
的东西可能会很好。@Eliasar好的,是i of indexI,它会假设你在x、y和z上迭代,同时使用i作为当前计数。