using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Serialization; public class GridManager : MonoBehaviour { public int width = 10; public int height = 10; public float cellSize = 1f; public GameObject cellPrefab; // 预制体: 一个格子 public GameObject startPositionObject; void Start() { if (startPositionObject != null) { Vector3 startPosition = startPositionObject.transform.position; // print($"坐标: {startPosition}"); GenerateGrid(startPosition); } } void GenerateGrid(Vector3 startPosition) { // start position // GameObject cell = Instantiate(cellPrefab, startPosition, Quaternion.identity); // 1: -29 1.5 // 2: -27 1.5 for (int row = 0; row < width; row++) { for (int column = 0; column < height; column++) { GameObject cell = Instantiate(cellPrefab, startPosition + new Vector3(row * cellSize, -column * cellSize, 0), Quaternion.identity); cell.name = $"cell_{cell.transform.position}"; } } // cell.transform.parent = transform; // for (int x = 0; x < width; x++) // { // for (int y = 0; y < height; y++) // { // Vector3 pos = new Vector3(x * cellSize, y * cellSize, 0); // GameObject cell = Instantiate(cellPrefab, pos, Quaternion.identity); // cell.transform.parent = transform; // } // } } }