65 lines
2.3 KiB
C#
65 lines
2.3 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class FurniturePaletteUI : MonoBehaviour
|
||
{
|
||
[Header("UI Элементы")]
|
||
public GameObject palettePanel; // Панель, которая выезжает с цветами
|
||
public Transform contentContainer; // Объект с Horizontal/Grid Layout Group
|
||
public GameObject colorBtnTemplate; // Префаб кнопки цвета
|
||
|
||
private FurnitureVisuals _currentVisuals;
|
||
|
||
public void ShowColors(FurnitureObject furniture)
|
||
{
|
||
// Проверяем, есть ли карточка и скрипт визуала
|
||
if (furniture == null || furniture.runtimeItemData == null) return;
|
||
|
||
_currentVisuals = furniture.GetComponent<FurnitureVisuals>();
|
||
if (_currentVisuals == null || furniture.runtimeItemData.availablePresets.Count <= 1)
|
||
{
|
||
// Если цвет всего один или нет визуала - не показываем панель
|
||
palettePanel.SetActive(false);
|
||
return;
|
||
}
|
||
|
||
palettePanel.SetActive(true);
|
||
|
||
// 1. Очищаем старые кнопки
|
||
foreach (Transform child in contentContainer)
|
||
{
|
||
Destroy(child.gameObject);
|
||
}
|
||
|
||
// 2. Создаем новые кнопки цветов
|
||
foreach (FurniturePreset preset in furniture.runtimeItemData.availablePresets)
|
||
{
|
||
GameObject btnObj = Instantiate(colorBtnTemplate, contentContainer);
|
||
|
||
// Ставим иконку цвета (которую вы настраивали в SO)
|
||
Image img = btnObj.GetComponent<Image>();
|
||
if (img != null && preset.presetIcon != null)
|
||
{
|
||
img.sprite = preset.presetIcon;
|
||
}
|
||
|
||
// Настраиваем клик по кнопке
|
||
Button btn = btnObj.GetComponent<Button>();
|
||
FurniturePreset presetToApply = preset; // Замыкание для кнопки
|
||
|
||
btn.onClick.AddListener(() =>
|
||
{
|
||
if (_currentVisuals != null)
|
||
{
|
||
_currentVisuals.ApplyPreset(presetToApply);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
public void HideColors()
|
||
{
|
||
palettePanel.SetActive(false);
|
||
_currentVisuals = null;
|
||
}
|
||
} |