【问题标题】:How would I make a 3D checkerboard array [closed]我将如何制作 3D 棋盘阵列 [关闭]
【发布时间】:2020-04-28 05:29:27
【问题描述】:

我正在使用 unity 尝试在 3d 数组中制作 3D 棋盘。我还没有找到其他地方回答这个问题。

如果可能,最好在 3 个嵌套 for 循环中进行,这就是通常制作 2d 棋盘的方式。

【问题讨论】:

  • 2 个嵌套循环,大小与您的棋盘一样大,然后交替创建黑白窗格?
  • 指的是“3d数组里面”,它是什么3d数组?您是否有 GameObjects 或者您想说白色为 0,黑色为 1 并获得一个整数数组?

标签: arrays unity3d multidimensional-array


【解决方案1】:

这是一个在 Start 方法中生成 3D 棋盘的脚本,以便您可以将其拖动到场景中的对象并按照步骤操作。 嵌套循环中的逻辑检查索引 i 和 j 的和是奇数还是偶数。然后代码通过检查对应于高度的索引 k 是奇数还是偶数,在黑白瓷砖之间交替。

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

public class Checkerboard3D : MonoBehaviour
{
    private void Start()
    {
        // This is the container for the checkerboard
        List<List<List<GameObject>>> Checkerboard = new List<List<List<GameObject>>>();
        // Create primitive, reference tiles
        GameObject CheckerBoardWhiteTile = GameObject.CreatePrimitive(PrimitiveType.Cube);
        GameObject CheckerBoardBlackTile = GameObject.CreatePrimitive(PrimitiveType.Cube);
        CheckerBoardBlackTile.GetComponent<Renderer>().material.color = Color.black;
        // Rows, columns, and height can be parameters to the function
        Int32 rows = 10;
        Int32 columns = 10;
        Int32 height = 10;
        for(Int32 i = 0; i < rows; i++)
        {
            Checkerboard.Add(new List<List<GameObject>>());
            for(Int32 j = 0; j < columns; j++)
            {
                Checkerboard[i].Add(new List<GameObject>());
                for(Int32 k = 0; k < height; k++)
                {
                    GameObject objectToAdd = null;
                    // The position is set this way since Y is up in Unity
                    Vector3 position = new Vector3(i, k, j);
                    if(((i + j) % 2) == 0)
                    {
                        if(k % 2 == 0)
                        {
                            objectToAdd = GameObject.Instantiate(CheckerBoardWhiteTile, position, Quaternion.identity);
                        }
                        else
                        {
                            objectToAdd = GameObject.Instantiate(CheckerBoardBlackTile, position, Quaternion.identity);
                        }
                    }
                    else
                    {
                        if(k % 2 == 0)
                        {
                            objectToAdd = GameObject.Instantiate(CheckerBoardBlackTile, position, Quaternion.identity);
                        }
                        else
                        {
                            objectToAdd = GameObject.Instantiate(CheckerBoardWhiteTile, position, Quaternion.identity);
                        }
                    }
                    // Thus, you can access elements of the checkerboards by Checkerboard[i][j][k]
                    Checkerboard[i][j].Add(objectToAdd); 
                }
            }
        }
        // To clean up the primitive GameObjects we instantiated in the beginning
        Destroy(CheckerBoardWhiteTile);
        Destroy(CheckerBoardBlackTile);
    }
}

如果您想添加比例,则需要根据您的比例更改Vector3 position,并在实例化objectToAdd GameObject 时提供Vector3 scale

此脚本生成 10x10x10 棋盘的结果如下:

【讨论】:

  • 帮助很大,谢谢!我能够将它放入我的代码中并且效果很好。
  • 我很高兴这对你有用,编码愉快!在 StackOverflow 中,如果您发现某个答案有帮助,您可以考虑点赞。因此,其他人会在顶部看到最有用的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 1970-01-01
  • 2012-04-20
  • 2022-11-04
  • 1970-01-01
相关资源
最近更新 更多