Game_CodeMM/Assets/00_scripts/GridManager.cs

54 lines
1.9 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-10-03 02:18:28 +08:00
using UnityEngine.UIElements;
2025-09-27 20:03:16 +08:00
public class GridManager : MonoBehaviour
{
public int width = 10;
public int height = 10;
public float cellSize = 1f;
2025-10-03 02:18:28 +08:00
[Tooltip("一个格子的预制体")] public GameObject oneCellPrefab; // 预制体: 一个格子
[Tooltip("世界中心位置坐标")] public GameObject startPositionObject;
2025-09-27 20:03:16 +08:00
void Start()
{
2025-09-28 22:26:21 +08:00
if (startPositionObject != null)
{
2025-10-03 02:18:28 +08:00
Vector3 start_position = startPositionObject.transform.position;
2025-09-28 22:26:21 +08:00
// print($"坐标: {startPosition}");
2025-10-03 02:18:28 +08:00
GenerateGrid(start_position);
}
else
{
Debug.Log("Start Position GameObject Null!!!");
throw new Exception("throw new Exception");
2025-09-28 22:26:21 +08:00
}
2025-09-27 20:03:16 +08:00
}
2025-10-03 02:18:28 +08:00
private void GenerateGrid(Vector3 start_position)
2025-09-27 20:03:16 +08:00
{
2025-09-28 22:26:21 +08:00
// 1: -29 1.5
// 2: -27 1.5
2025-10-03 02:18:28 +08:00
int m_width = width / 2;
for (int create_height = 0; create_height <= height; create_height++)
2025-09-27 20:03:16 +08:00
{
2025-10-03 02:18:28 +08:00
// 创建中心格子
create_a_cell(start_position + new Vector3(0 * cellSize, -create_height * cellSize, 0));
for (int create_width = 1; create_width <= m_width; create_width++)
2025-09-27 20:03:16 +08:00
{
2025-10-03 02:18:28 +08:00
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));
2025-09-27 20:03:16 +08:00
}
}
2025-10-03 02:18:28 +08:00
}
private void create_a_cell(Vector3 m_create_position)
{
GameObject cell = Instantiate(oneCellPrefab, m_create_position, Quaternion.identity);
cell.name = $"cell_{cell.transform.position}";
2025-09-27 20:03:16 +08:00
}
}