Добавил цвета из атлса, надо добавить иконки для них
This commit is contained in:
1 parent
2fd7b76285
commit
7f1386882d
126 files changed
+4802
-774
No files matched your search
@@ -1,85 +1,139 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
public class FurniturePaletteUI : MonoBehaviour
|
||||
{
|
||||
[Header("UI Элементы")]
|
||||
public GameObject palettePanel; // Панель, которая выезжает с цветами
|
||||
public Transform contentContainer; // Объект с Horizontal/Grid Layout Group
|
||||
public GameObject colorBtnTemplate; // Префаб кнопки цвета
|
||||
[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.availablePresets.Count <= 1)
|
||||
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;
|
||||
return;
|
||||
}
|
||||
|
||||
palettePanel.SetActive(true);
|
||||
|
||||
foreach (Transform child in contentContainer)
|
||||
// 2. Создаем новые кнопки
|
||||
foreach (AtlasStyleSO style in slotData.availableStyles)
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
if (style == null) continue;
|
||||
|
||||
foreach (FurniturePreset preset in furniture.runtimeItemData.availablePresets)
|
||||
{
|
||||
// ДОБАВЛЯЕМ false в конце! Это запретит кнопке улетать по оси Z
|
||||
GameObject btnObj = Instantiate(colorBtnTemplate, contentContainer, false);
|
||||
GameObject btnObj = Instantiate(colorBtnTemplate, colorsContainer, false);
|
||||
|
||||
RectTransform rect = btnObj.GetComponent<RectTransform>();
|
||||
rect.localScale = Vector3.one;
|
||||
// Принудительно обнуляем координату Z (на всякий случай)
|
||||
rect.localPosition = new Vector3(rect.localPosition.x, rect.localPosition.y, 0f);
|
||||
rect.sizeDelta = new Vector2(100f, 100f); // Ваш размер кнопки
|
||||
rect.sizeDelta = new Vector2(100f, 100f);
|
||||
|
||||
Image img = btnObj.GetComponent<Image>();
|
||||
if (img != null)
|
||||
{
|
||||
if (preset.presetIcon != null) img.sprite = preset.presetIcon;
|
||||
else if (preset.slotStyles != null && preset.slotStyles.Length > 0 && preset.slotStyles[0] != null)
|
||||
img.sprite = preset.slotStyles[0].uiIcon;
|
||||
|
||||
// Убеждаемся, что кнопка не прозрачная
|
||||
img.color = new Color(img.color.r, img.color.g, img.color.b, 1f);
|
||||
img.sprite = style.uiIcon;
|
||||
img.color = new Color(img.color.r, img.color.g, img.color.b, 1f);
|
||||
}
|
||||
|
||||
Button btn = btnObj.GetComponent<Button>();
|
||||
FurniturePreset presetToApply = preset;
|
||||
AtlasStyleSO styleToApply = style;
|
||||
|
||||
btn.onClick.AddListener(() =>
|
||||
{
|
||||
if (_currentVisuals != null) _currentVisuals.ApplyPreset(presetToApply);
|
||||
if (_currentVisuals != null) _currentVisuals.ApplyStyleToSlot(slotIndex, styleToApply);
|
||||
});
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// УМНЫЙ РАСЧЕТ РАЗМЕРА И ПРОКРУТКИ
|
||||
// ==========================================
|
||||
|
||||
// 1. Заставляем Content рассчитать свою ширину со всеми кнопками
|
||||
RectTransform contentRect = contentContainer.GetComponent<RectTransform>();
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(contentRect);
|
||||
float totalButtonsWidth = contentRect.sizeDelta.x;
|
||||
// 3. Вызываем умный перерасчет размера панели
|
||||
ResizePalettePanel();
|
||||
}
|
||||
|
||||
// 2. Вычисляем максимальную ширину (например, 90% от ширины экрана/канваса)
|
||||
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;
|
||||
|
||||
// 3. Выбираем, что меньше: реальная ширина кнопок или максимальная ширина
|
||||
// Выбираем ширину: либо размер кнопок, либо максимум экрана
|
||||
float finalPanelWidth = Mathf.Min(totalButtonsWidth, maxAllowedWidth);
|
||||
|
||||
// 4. Применяем этот размер к ГЛАВНОЙ панели (Scroll View)
|
||||
// Меняем ширину Главной Панели (Scroll View)
|
||||
RectTransform panelRect = palettePanel.GetComponent<RectTransform>();
|
||||
panelRect.sizeDelta = new Vector2(finalPanelWidth, panelRect.sizeDelta.y);
|
||||
|
||||
// 5. Отключаем прокрутку, если кнопки влезли в экран (чтобы панель не "пружинила" без нужды)
|
||||
// Включаем прокрутку ТОЛЬКО если кнопки не влезли
|
||||
ScrollRect scroll = palettePanel.GetComponent<ScrollRect>();
|
||||
if (scroll != null)
|
||||
{
|
||||
@@ -90,6 +144,8 @@ public class FurniturePaletteUI : MonoBehaviour
|
||||
public void HideColors()
|
||||
{
|
||||
palettePanel.SetActive(false);
|
||||
tabsPanel.SetActive(false);
|
||||
_currentVisuals = null;
|
||||
_currentFurniture = null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user