如何创建三维交错数组?求3D网格盒子生成的正确脚本写法
3D网格盒子生成的几种C#实现方案
以下是几种Unity中生成3D网格盒子的可行脚本写法,解决迭代生成时的常见错误:
1. 基础三层循环生成(直观易读)
适合静态网格一次性生成,逻辑清晰:
using UnityEngine; public class BasicGridGenerator : MonoBehaviour { // 三个维度的盒子数量 public int xCount = 3; public int yCount = 3; public int zCount = 3; // 单个盒子的尺寸 public Vector3 boxSize = Vector3.one; // 盒子间的间隙 public float spacing = 0.1f; void Start() { GenerateGrid(); } void GenerateGrid() { // 计算每个盒子的步长(尺寸+间隙) Vector3 step = boxSize + Vector3.one * spacing; for (int x = 0; x < xCount; x++) { for (int y = 0; y < yCount; y++) { for (int z = 0; z < zCount; z++) { // 计算当前盒子的位置 Vector3 position = new Vector3( x * step.x, y * step.y, z * step.z ); // 创建Cube并设置属性 GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube); box.transform.position = position; box.transform.localScale = boxSize; box.name = $"Box_{x}_{y}_{z}"; // 设为当前物体子对象,方便管理 box.transform.SetParent(transform); } } } } }
2. 基于数组存储维度的实现(贴合你的初始思路)
用数组存储x/y/z维度数量,同时增加数组合法性校验,避免索引越界错误:
using UnityEngine; public class ArrayBasedGridGenerator : MonoBehaviour { // 数组存储[x, y, z]三个维度的盒子数量 public int[] dimensions = new int[] {3, 3, 3}; public Vector3 boxSize = Vector3.one; public float spacing = 0.1f; void Start() { GenerateGridFromArray(); } void GenerateGridFromArray() { // 校验数组长度,防止索引越界 if (dimensions.Length != 3) { Debug.LogError("Dimensions数组必须包含3个元素:x、y、z方向的数量"); return; } int xCount = dimensions[0]; int yCount = dimensions[1]; int zCount = dimensions[2]; Vector3 step = boxSize + Vector3.one * spacing; for (int x = 0; x < xCount; x++) { for (int y = 0; y < yCount; y++) { for (int z = 0; z < zCount; z++) { Vector3 pos = new Vector3(x * step.x, y * step.y, z * step.z); GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube); box.transform.SetParent(transform); box.transform.position = pos; box.transform.localScale = boxSize; } } } } }
3. 对象池优化版(适合频繁显示/隐藏场景)
如果需要反复生成销毁盒子,用对象池减少性能消耗:
using UnityEngine; using System.Collections.Generic; public class PooledGridGenerator : MonoBehaviour { public int[] dimensions = {3, 3, 3}; public Vector3 boxSize = Vector3.one; public float spacing = 0.1f; // 拖入自定义Cube预制体 public GameObject boxPrefab; // 对象池容器 private List<GameObject> boxPool = new List<GameObject>(); void Start() { GeneratePooledGrid(); } void GeneratePooledGrid() { if (dimensions.Length != 3) { Debug.LogError("Dimensions数组长度必须为3"); return; } int totalBoxes = dimensions[0] * dimensions[1] * dimensions[2]; // 初始化对象池 InitializePool(totalBoxes); int xCount = dimensions[0]; int yCount = dimensions[1]; int zCount = dimensions[2]; Vector3 step = boxSize + Vector3.one * spacing; int poolIndex = 0; for (int x = 0; x < xCount; x++) { for (int y = 0; y < yCount; y++) { for (int z = 0; z < zCount; z++) { GameObject box = boxPool[poolIndex]; box.SetActive(true); box.transform.position = new Vector3(x * step.x, y * step.y, z * step.z); box.transform.localScale = boxSize; poolIndex++; } } } } // 预创建所有盒子存入对象池 void InitializePool(int total) { for (int i = 0; i < total; i++) { GameObject box = Instantiate(boxPrefab, transform); box.SetActive(false); boxPool.Add(box); } } // 隐藏所有盒子的方法 public void HideAllBoxes() { foreach (var box in boxPool) { box.SetActive(false); } } }
注意事项
- 循环条件务必使用
x < xCount而非x <= xCount,避免超出维度数量导致的索引错误 - 调整
spacing参数可控制盒子间的间隙,防止重叠 - 把生成的盒子设为当前物体的子对象,便于层级管理和后续操作
内容的提问来源于stack exchange,提问作者mk95
相关产品推荐
相关产品推荐

