我将如何制作3D棋盘阵列

问题描述 投票:0回答:1

我正在团结一致地尝试在3D阵列中制作3D棋盘格。我还没有找到其他地方来回答这个问题。

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

arrays unity3d multidimensional-array
1个回答
0
投票

这里是一个在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,并在实例化Vector3 scale时提供objectToAdd GameObject

此脚本生成10x10x10棋盘的结果如下:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.