Game_CodeMM/Assets/00_scripts/GridManager.cs

52 lines
1.7 KiB
C#
Raw Normal View History

2025-09-27 20:03:16 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2025-09-28 22:26:21 +08:00
using UnityEngine.Serialization;
2025-09-27 20:03:16 +08:00
public class GridManager : MonoBehaviour
{
public int width = 10;
public int height = 10;
public float cellSize = 1f;
public GameObject cellPrefab; // 预制体: 一个格子
2025-09-28 22:26:21 +08:00
public GameObject startPositionObject;
2025-09-27 20:03:16 +08:00
void Start()
{
2025-09-28 22:26:21 +08:00
if (startPositionObject != null)
{
Vector3 startPosition = startPositionObject.transform.position;
// print($"坐标: {startPosition}");
GenerateGrid(startPosition);
}
2025-09-27 20:03:16 +08:00
}
2025-09-28 22:26:21 +08:00
void GenerateGrid(Vector3 startPosition)
2025-09-27 20:03:16 +08:00
{
2025-09-28 22:26:21 +08:00
// start position
// GameObject cell = Instantiate(cellPrefab, startPosition, Quaternion.identity);
// 1: -29 1.5
// 2: -27 1.5
for (int row = 0; row < width; row++)
2025-09-27 20:03:16 +08:00
{
2025-09-28 22:26:21 +08:00
for (int column = 0; column < height; column++)
2025-09-27 20:03:16 +08:00
{
2025-09-28 22:26:21 +08:00
GameObject cell = Instantiate(cellPrefab,
startPosition + new Vector3(row * cellSize, -column * cellSize, 0),
Quaternion.identity);
cell.name = $"cell_{cell.transform.position}";
2025-09-27 20:03:16 +08:00
}
}
2025-09-28 22:26:21 +08:00
// 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;
// }
// }
2025-09-27 20:03:16 +08:00
}
}