Files
SummerGame/Assets/_Project/Scripts/MetaPart/FurniturePaletteUI.cs
T

151 lines
5.6 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class FurniturePaletteUI : MonoBehaviour
{
[Header("UI Элементы: Палитра (Scroll View)")]
public GameObject palettePanel;
public Transform colorsContainer;
public GameObject colorBtnTemplate;
[Header("UI Элементы: Вкладки деталей")]
public GameObject tabsPanel;
public Transform tabsContainer;
public GameObject tabBtnTemplate;
private FurnitureObject _currentFurniture;
private FurnitureVisuals _currentVisuals;
private int _selectedSlotIndex = 0;
public void ShowColors(FurnitureObject furniture)
{
if (furniture == null || furniture.runtimeItemData == null) return;
_currentVisuals = furniture.GetComponent<FurnitureVisuals>();
if (_currentVisuals == null || furniture.runtimeItemData.configurableSlots.Count == 0)
{
HideColors();
return;
}
_currentFurniture = furniture;
_selectedSlotIndex = 0;
GenerateTabs();
ShowColorsForSlot(_selectedSlotIndex);
palettePanel.SetActive(true);
tabsPanel.SetActive(furniture.runtimeItemData.configurableSlots.Count > 1);
}
private void GenerateTabs()
{
foreach (Transform child in tabsContainer) Destroy(child.gameObject);
for (int i = 0; i < _currentFurniture.runtimeItemData.configurableSlots.Count; i++)
{
int slotIndex = i;
string slotName = _currentFurniture.runtimeItemData.configurableSlots[i].slotName;
GameObject tabObj = Instantiate(tabBtnTemplate, tabsContainer, false);
tabObj.transform.localScale = Vector3.one;
TextMeshProUGUI tabText = tabObj.GetComponentInChildren<TextMeshProUGUI>();
if (tabText != null) tabText.text = slotName;
Button btn = tabObj.GetComponent<Button>();
btn.onClick.AddListener(() =>
{
_selectedSlotIndex = slotIndex;
ShowColorsForSlot(_selectedSlotIndex);
});
}
}
private void ShowColorsForSlot(int slotIndex)
{
// 1. Очищаем старые кнопки
foreach (Transform child in colorsContainer) Destroy(child.gameObject);
var slotData = _currentFurniture.runtimeItemData.configurableSlots[slotIndex];
// Если для этой детали нет цветов - скрываем панель и выходим
if (slotData.availableStyles == null || slotData.availableStyles.Count == 0)
{
palettePanel.SetActive(false);
return;
}
palettePanel.SetActive(true);
// 2. Создаем новые кнопки
foreach (AtlasStyleSO style in slotData.availableStyles)
{
if (style == null) continue;
GameObject btnObj = Instantiate(colorBtnTemplate, colorsContainer, false);
RectTransform rect = btnObj.GetComponent<RectTransform>();
rect.localScale = Vector3.one;
rect.localPosition = new Vector3(rect.localPosition.x, rect.localPosition.y, 0f);
rect.sizeDelta = new Vector2(100f, 100f);
Image img = btnObj.GetComponent<Image>();
if (img != null)
{
img.sprite = style.uiIcon;
img.color = new Color(img.color.r, img.color.g, img.color.b, 1f);
}
Button btn = btnObj.GetComponent<Button>();
AtlasStyleSO styleToApply = style;
btn.onClick.AddListener(() =>
{
if (_currentVisuals != null) _currentVisuals.ApplyStyleToSlot(slotIndex, styleToApply);
});
}
// 3. Вызываем умный перерасчет размера панели
ResizePalettePanel();
}
private void ResizePalettePanel()
{
// Заставляем Unity моментально пересчитать UI (решает баг с нулевым размером)
Canvas.ForceUpdateCanvases();
RectTransform contentRect = colorsContainer.GetComponent<RectTransform>();
LayoutRebuilder.ForceRebuildLayoutImmediate(contentRect);
// Узнаем, сколько места реально заняли все кнопки внутри Content
float totalButtonsWidth = contentRect.rect.width;
// Вычисляем максимум (например, 90% от ширины экрана)
RectTransform canvasRect = palettePanel.GetComponentInParent<Canvas>().GetComponent<RectTransform>();
float maxAllowedWidth = canvasRect.rect.width * 0.9f;
// Выбираем ширину: либо размер кнопок, либо максимум экрана
float finalPanelWidth = Mathf.Min(totalButtonsWidth, maxAllowedWidth);
// Меняем ширину Главной Панели (Scroll View)
RectTransform panelRect = palettePanel.GetComponent<RectTransform>();
panelRect.sizeDelta = new Vector2(finalPanelWidth, panelRect.sizeDelta.y);
// Включаем прокрутку ТОЛЬКО если кнопки не влезли
ScrollRect scroll = palettePanel.GetComponent<ScrollRect>();
if (scroll != null)
{
scroll.enabled = (totalButtonsWidth > maxAllowedWidth);
}
}
public void HideColors()
{
palettePanel.SetActive(false);
tabsPanel.SetActive(false);
_currentVisuals = null;
_currentFurniture = null;
}
}