using System.Collections.Generic; using OTGIntegrated.Items; using UnityEngine; using UnityEngine.UI; namespace OTGIntegrated.Inventory { public sealed class InventoryUI : MonoBehaviour { public GameObject panelRoot; public Transform slotGrid; public Text titleText; public Text descriptionText; public Text hintText; public Button closeButton; public Font uiFont; private Inventory inventory; private readonly List spawnedSlots = new List(); public void Initialize(Inventory inventory) { this.inventory = inventory; if (inventory != null) { inventory.OnInventoryChanged -= Refresh; inventory.OnInventoryChanged += Refresh; } if (closeButton != null) { closeButton.onClick.RemoveAllListeners(); closeButton.onClick.AddListener(() => FindObjectOfType()?.CloseCurrentWindow()); } Refresh(); } private void OnEnable() { Refresh(); } private void OnDestroy() { if (inventory != null) { inventory.OnInventoryChanged -= Refresh; } } public void Refresh() { if (slotGrid == null) { return; } ClearSlots(); IReadOnlyList slots = inventory != null ? inventory.GetAllSlots() : null; int maxSlots = inventory != null ? inventory.MaxSlots : 20; for (int i = 0; i < maxSlots; i++) { InventorySlot slot = slots != null && i < slots.Count ? slots[i] : null; CreateSlot(i, slot); } if (titleText != null) { titleText.text = "Инвентарь"; } if (hintText != null) { hintText.text = "I — закрыть F5 — сохранить F9 — загрузить"; } if (descriptionText != null && string.IsNullOrEmpty(descriptionText.text)) { descriptionText.text = "Выберите предмет."; } } private void CreateSlot(int index, InventorySlot slot) { GameObject root = new GameObject($"InventorySlot_{index}", typeof(RectTransform), typeof(Image), typeof(Button)); root.transform.SetParent(slotGrid, false); spawnedSlots.Add(root); RectTransform rect = root.GetComponent(); rect.sizeDelta = new Vector2(90f, 90f); Image background = root.GetComponent(); background.color = slot != null && !slot.IsEmpty() ? Color.Lerp(new Color(0.09f, 0.1f, 0.12f, 0.95f), slot.item.itemColor, 0.35f) : new Color(0.07f, 0.08f, 0.1f, 0.82f); Text label = CreateText(root.transform, "Label", slot != null && !slot.IsEmpty() ? slot.item.displayName : string.Empty, 14, TextAnchor.MiddleCenter); label.rectTransform.anchorMin = new Vector2(0.08f, 0.24f); label.rectTransform.anchorMax = new Vector2(0.92f, 0.78f); label.rectTransform.offsetMin = Vector2.zero; label.rectTransform.offsetMax = Vector2.zero; Text count = CreateText(root.transform, "Count", slot != null && !slot.IsEmpty() ? slot.amount.ToString() : string.Empty, 14, TextAnchor.LowerRight); count.rectTransform.anchorMin = new Vector2(0.1f, 0.05f); count.rectTransform.anchorMax = new Vector2(0.95f, 0.35f); count.rectTransform.offsetMin = Vector2.zero; count.rectTransform.offsetMax = Vector2.zero; Button button = root.GetComponent