using UnityEngine; using UnityEngine.UI; public class InventorySystem : MonoBehaviour { public static InventorySystem Instance; [Header("UI Settings")] public Image[] slotImages; public Sprite defaultEmptyIcon; public CanvasGroup uiCanvasGroup; [Header("Settings")] public float fadeTime = 3f; private ItemData[] _items = new ItemData[5]; private float _timer; private bool _isVisible; public bool isInputActive = false; private void Awake() { Instance = this; } private void Start() { uiCanvasGroup.alpha = 0; UpdateUI(); } private void Update() { if (!isInputActive) return; HandleInput(); HandleFade(); } private void HandleInput() { if (Input.GetKeyDown(KeyCode.Alpha1)) ShowInventory(); if (Input.GetKeyDown(KeyCode.Alpha2)) ShowInventory(); if (Input.GetKeyDown(KeyCode.Alpha3)) ShowInventory(); if (Input.GetKeyDown(KeyCode.Alpha4)) ShowInventory(); if (Input.GetKeyDown(KeyCode.Alpha5)) ShowInventory(); } private void HandleFade() { if (_timer > 0) { _timer -= Time.deltaTime; uiCanvasGroup.alpha = Mathf.Lerp(uiCanvasGroup.alpha, 1, Time.deltaTime * 10); } else { uiCanvasGroup.alpha = Mathf.Lerp(uiCanvasGroup.alpha, 0, Time.deltaTime * 2); } } public void ShowInventory() { _timer = fadeTime; } public bool AddItem(ItemData newItem) { if (HasItem(newItem)) { Debug.Log("An item of this type is already in the inventory: " + newItem.itemName); return false; } for (int i = 0; i < _items.Length; i++) { if (_items[i] == null) { _items[i] = newItem; UpdateUI(); ShowInventory(); Debug.Log("Picked up: " + newItem.itemName); return true; } } Debug.Log("Inventory is full!"); return false; } public void RemoveItem(ItemData itemToRemove) { for (int i = 0; i < _items.Length; i++) { if (_items[i] == itemToRemove) { _items[i] = null; UpdateUI(); ShowInventory(); Debug.Log("Removed: " + itemToRemove.itemName); return; } } } private void UpdateUI() { for (int i = 0; i < slotImages.Length; i++) { if (_items[i] != null) { slotImages[i].sprite = _items[i].icon; slotImages[i].color = Color.white; } else { slotImages[i].color = Color.clear; } } } public bool HasItem(ItemData itemToCheck) { foreach (var item in _items) { if (item != null && item == itemToCheck) { return true; } } return false; } }