2026-08-11 20:38:39 +03:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (Transform child in contentContainer)
|
|
|
|
|
|
{
|
|
|
|
|
|
Destroy(child.gameObject);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (FurniturePreset preset in furniture.runtimeItemData.availablePresets)
|
|
|
|
|
|
{
|
2026-08-15 21:12:47 +03:00
|
|
|
|
// ДОБАВЛЯЕМ false в конце! Это запретит кнопке улетать по оси Z
|
|
|
|
|
|
GameObject btnObj = Instantiate(colorBtnTemplate, contentContainer, 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); // Ваш размер кнопки
|
|
|
|
|
|
|
2026-08-11 20:38:39 +03:00
|
|
|
|
Image img = btnObj.GetComponent<Image>();
|
2026-08-15 21:12:47 +03:00
|
|
|
|
if (img != null)
|
2026-08-11 20:38:39 +03:00
|
|
|
|
{
|
2026-08-15 21:12:47 +03:00
|
|
|
|
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);
|
2026-08-11 20:38:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Button btn = btnObj.GetComponent<Button>();
|
2026-08-15 19:32:01 +03:00
|
|
|
|
FurniturePreset presetToApply = preset;
|
2026-08-11 20:38:39 +03:00
|
|
|
|
|
|
|
|
|
|
btn.onClick.AddListener(() =>
|
|
|
|
|
|
{
|
2026-08-15 21:12:47 +03:00
|
|
|
|
if (_currentVisuals != null) _currentVisuals.ApplyPreset(presetToApply);
|
2026-08-11 20:38:39 +03:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-08-15 19:32:01 +03:00
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
2026-08-15 21:12:47 +03:00
|
|
|
|
// УМНЫЙ РАСЧЕТ РАЗМЕРА И ПРОКРУТКИ
|
2026-08-15 19:32:01 +03:00
|
|
|
|
// ==========================================
|
2026-08-15 21:12:47 +03:00
|
|
|
|
|
|
|
|
|
|
// 1. Заставляем Content рассчитать свою ширину со всеми кнопками
|
|
|
|
|
|
RectTransform contentRect = contentContainer.GetComponent<RectTransform>();
|
|
|
|
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(contentRect);
|
|
|
|
|
|
float totalButtonsWidth = contentRect.sizeDelta.x;
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Вычисляем максимальную ширину (например, 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)
|
|
|
|
|
|
RectTransform panelRect = palettePanel.GetComponent<RectTransform>();
|
|
|
|
|
|
panelRect.sizeDelta = new Vector2(finalPanelWidth, panelRect.sizeDelta.y);
|
|
|
|
|
|
|
|
|
|
|
|
// 5. Отключаем прокрутку, если кнопки влезли в экран (чтобы панель не "пружинила" без нужды)
|
|
|
|
|
|
ScrollRect scroll = palettePanel.GetComponent<ScrollRect>();
|
|
|
|
|
|
if (scroll != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
scroll.enabled = (totalButtonsWidth > maxAllowedWidth);
|
|
|
|
|
|
}
|
2026-08-11 20:38:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void HideColors()
|
|
|
|
|
|
{
|
|
|
|
|
|
palettePanel.SetActive(false);
|
|
|
|
|
|
_currentVisuals = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|