Files
CityBuilder/Assets/Scripts/BuildMenu.cs
SHOUTING_PIRATE 1c91215efd initial commit
2025-07-07 20:59:04 +01:00

68 lines
1.8 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class BuildMenu : MonoBehaviour
{
[Header("UI References")]
public GameObject popupPanel;
public Button optionButtonPrefab;
public Transform buttonParent;
[Header("Presets")]
public BuildingPreset[] buildingPresets;
private void Start()
{
if (popupPanel != null)
popupPanel.SetActive(false);
}
public void ShowMenu()
{
if (popupPanel == null || optionButtonPrefab == null || buttonParent == null)
{
Debug.LogError("BuildMenu: UI references not set!");
return;
}
// Remove old buttons
foreach (Transform child in buttonParent)
{
Destroy(child.gameObject);
}
Debug.Log("BuildMenu: Old buttons removed");
// Create a button for each preset
foreach (var preset in buildingPresets)
{
Debug.Log($"BuildMenu: Creating button for preset {preset.name}");
var btn = Instantiate(optionButtonPrefab, buttonParent);
// Support both Text and TextMeshProUGUI
var text = btn.GetComponentInChildren<Text>();
if (text != null)
text.text = preset.name;
else
{
var tmp = btn.GetComponentInChildren<TextMeshProUGUI>();
if (tmp != null)
tmp.text = preset.name;
}
btn.onClick.AddListener(() => OnPresetSelected(preset));
}
popupPanel.SetActive(true);
}
public void HideMenu()
{
if (popupPanel != null)
popupPanel.SetActive(false);
}
private void OnPresetSelected(BuildingPreset preset)
{
BuildingPlacement.Instance.BeginNewBuildingPlacement(preset);
HideMenu();
}
}