95 lines
4.3 KiB
C#
95 lines
4.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);
|
||
|
||
foreach (Transform child in contentContainer)
|
||
{
|
||
Destroy(child.gameObject);
|
||
}
|
||
|
||
foreach (FurniturePreset preset in furniture.runtimeItemData.availablePresets)
|
||
{
|
||
// ДОБАВЛЯЕМ 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); // Ваш размер кнопки
|
||
|
||
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);
|
||
}
|
||
|
||
Button btn = btnObj.GetComponent<Button>();
|
||
FurniturePreset presetToApply = preset;
|
||
|
||
btn.onClick.AddListener(() =>
|
||
{
|
||
if (_currentVisuals != null) _currentVisuals.ApplyPreset(presetToApply);
|
||
});
|
||
}
|
||
|
||
// ==========================================
|
||
// УМНЫЙ РАСЧЕТ РАЗМЕРА И ПРОКРУТКИ
|
||
// ==========================================
|
||
|
||
// 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);
|
||
}
|
||
}
|
||
|
||
public void HideColors()
|
||
{
|
||
palettePanel.SetActive(false);
|
||
_currentVisuals = null;
|
||
}
|
||
} |