using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UIElements; public class GridManager : MonoBehaviour { public int width = 10; public int height = 10; public float cellSize = 1f; [Tooltip("一个格子的预制体")] public GameObject oneCellPrefab; // 预制体: 一个格子 [Tooltip("世界中心位置坐标")] public GameObject startPositionObject; void Start() { if (startPositionObject != null) { Vector3 start_position = startPositionObject.transform.position; // print($"坐标: {startPosition}"); GenerateGrid(start_position); } else { Debug.Log("Start Position GameObject Null!!!"); throw new Exception("throw new Exception"); } } private void GenerateGrid(Vector3 start_position) { // 1: -29 1.5 // 2: -27 1.5 int m_width = width / 2; for (int create_height = 0; create_height <= height; create_height++) { // 创建中心格子 create_a_cell(start_position + new Vector3(0 * cellSize, -create_height * cellSize, 0)); for (int create_width = 1; create_width <= m_width; create_width++) { create_a_cell(start_position + new Vector3(create_width * cellSize, -create_height * cellSize, 0)); // 创建左边衍生的格子 create_a_cell(start_position + new Vector3(-create_width * cellSize, -create_height * cellSize, 0)); } } } private void create_a_cell(Vector3 m_create_position) { GameObject cell = Instantiate(oneCellPrefab, m_create_position, Quaternion.identity); cell.name = $"cell_{cell.transform.position}"; } }