文章目录
- 准备
- 代码实现
- 实现效果
准备
- 空物体
- 生成脚本
- 地面预制体
代码实现
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;public class CreateGround : MonoBehaviour
{[SerializeField]public int Column = 1; // 列public int Row = 1; // 行public float spacing = 10f;public GameObject ground;private List<GameObject> groundLists = new();void Start(){if (ground != null){Debug.Log("Groung_Row:" + Row);Debug.Log("Groung_Column:" + Column);for (int row = 0; row < Row; row++){for (int col = 0; col < Column; col++){float x = col * spacing;float z = row * spacing;GameObject goj = GameObject.Instantiate(ground, new Vector3(x, 0, z), Quaternion.identity);if (goj != null){groundLists.Add(goj);}}}}}
}
实现效果
- 把脚本挂载到空物体上
- 把地板预制体拖入脚本中
- 运行测试, 生成5x5间隔为10的地面
- 生成3x5间隔20的地面