using System.Collections; using System.Collections.Generic; using UnityEngine; public class BuildManager : MonoBehaviour { // Start is called before the first frame update // private bool isBuilding = false; [Tooltip("一个格子的预制体")] public GameObject building_prefab; private GameObject building_object; // private HashSet occupied_positions = new HashSet(); [Tooltip("一个用于存放临时网格的空物体")] public Transform around_grid_parent; // 用于存放所有生成的网格对象 private List around_position = new List(); // 挂载到UI的按钮事件 public void m_button_down_start_building() { create_around_position(); } private void create_around_position() { Vector2Int[] directions = { new Vector2Int(0, 1), // 上 new Vector2Int(0, -1), // 下 new Vector2Int(-1, 0), // 左 new Vector2Int(1, 0), // 右 new Vector2Int(-1, 1), // 左上 new Vector2Int(1, 1), // 右上 new Vector2Int(-1, -1), // 左下 new Vector2Int(1, -1), // 右下 }; Vector2Int center_position = new Vector2Int(0, 0); foreach (var dir in directions) { Vector2Int new_pos = center_position + dir; GameObject grid = Instantiate(building_prefab, new Vector3(new_pos.x * 2, new_pos.y * 2, 0), Quaternion.identity); grid.transform.parent = around_grid_parent; around_position.Add(new_pos); } } // public void m_button_down_building() // { // // building_object = Instantiate(building_prefab, Vector3.zero, Quaternion.identity); // // isBuilding = true; // this.show_neighbor_grid(new Vector2Int(0, 0)); // } // // 添加初始建筑 // public void add_building(Vector2Int pos) // { // Instantiate(building_prefab, new Vector3(pos.x, pos.y, 0), Quaternion.identity); // occupied_positions.Add(pos); // } // // // 清除旧网格 // public void ClearGrids() // { // foreach (Transform child in around_grid_parent) // Destroy(child.gameObject); // } // // // 点击网格时调用 // public void place_building(Vector2Int pos) // { // if (occupied_positions.Contains(pos)) return; // // Instantiate(building_prefab, new Vector3(pos.x, pos.y, 0), Quaternion.identity); // occupied_positions.Add(pos); // // ClearGrids(); // show_neighbor_grid(pos); // 自动显示新建筑的周围格子 // } // // // // 生成周围网格 // private void show_neighbor_grid(Vector2Int center_position) // { // Vector2Int[] directions = // { // new Vector2Int(0, 1), // 上 // new Vector2Int(0, -1), // 下 // new Vector2Int(-1, 0), // 左 // new Vector2Int(1, 0), // 右 // new Vector2Int(-1, 1), // 左上 // new Vector2Int(1, 1), // 右上 // new Vector2Int(-1, -1), // 左下 // new Vector2Int(1, -1), // 右下 // }; // // foreach (var dir in directions) // { // Vector2Int around_position = center_position + dir; // if (occupied_positions.Contains(around_position)) continue; // GameObject grid = Instantiate(building_prefab, // new Vector3(around_position.x, around_position.y, 0), // Quaternion.identity, around_grid_parent); // bool is_diagonal = Mathf.Abs(dir.x) == 1 && Mathf.Abs(dir.y) == 1; // grid.GetComponent().color = is_diagonal ? Color.red : Color.green; // // grid.GetComponent().Init(this, new_pos); // } // } // // void Start() // { // } // Update is called once per frame void Update() { // if (isBuilding) // { // Vector3 mouse_position = Input.mousePosition; // mouse_position = Camera.main.ScreenToWorldPoint(mouse_position); // mouse_position.z = 0; // building_object.transform.position = mouse_position; // } } }