55 lines
2.1 KiB
C#
55 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using System.Collections.Generic;
|
|
|
|
// Заполняет меню стройки кнопками для каждого доступного здания.
|
|
// Вешается на пустой объект-контейнер меню (например BuildMenuPanel).
|
|
|
|
|
|
public class BuildMenuUi : MonoBehaviour
|
|
{
|
|
[Header("Ссылки")]
|
|
public Transform buttonContainer; //Куда савить снопки
|
|
public GameObject buildingButtonPrefab; //Преваб одной кнопки здания
|
|
|
|
void Start()
|
|
{
|
|
RefreshMenu();
|
|
}
|
|
void OnEnable()
|
|
{
|
|
if (BuildManager.Instance == null) return;
|
|
RefreshMenu();
|
|
}
|
|
|
|
public void RefreshMenu()
|
|
{
|
|
//Чистка старых кнопок перед сборкой списка
|
|
foreach (Transform child in buttonContainer)
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
List<BuildingData> available = BuildManager.Instance.GetAvailableBuildings();
|
|
foreach (var building in available)
|
|
{
|
|
GameObject btnGO = Instantiate(buildingButtonPrefab, buttonContainer);
|
|
|
|
Transform iconT = btnGO.transform.Find("Icon");
|
|
Transform nameT = btnGO.transform.Find("NameText");
|
|
Transform costT = btnGO.transform.Find("CostText");
|
|
|
|
if (iconT != null) iconT.GetComponent<Image>().sprite = building.icon;
|
|
if (nameT != null) nameT.GetComponent<TMP_Text>().text = building.buildingName;
|
|
if (costT != null) costT.GetComponent<TMP_Text>().text = building.GetCostText();
|
|
Button btn = btnGO.GetComponent<Button>();
|
|
BuildingData capturedBuilding = building; // важно для замыкания в цикле
|
|
btn.onClick.AddListener(() =>
|
|
{
|
|
BuildManager.Instance.StartPlacing(capturedBuilding);
|
|
gameObject.SetActive(false); // закрываем меню после выбора, идём размещать здание
|
|
});
|
|
}
|
|
}
|
|
}
|