160 lines
3.9 KiB
C#
160 lines
3.9 KiB
C#
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// 인벤토리 슬롯 하나를 담당하는 UI 스크립트입니다.
|
|
/// FishSlot, CompassSlot, TrashSlot, BottleSlot 등에 각각 붙입니다.
|
|
/// </summary>
|
|
[DisallowMultipleComponent]
|
|
public class InventorySlotUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler
|
|
{
|
|
[Header("Item")]
|
|
[SerializeField] private InventoryItemType itemType;
|
|
|
|
[Header("UI References")]
|
|
[SerializeField] private Image slotBackground;
|
|
[SerializeField] private Image itemIcon;
|
|
[SerializeField] private TMP_Text countText;
|
|
[SerializeField] private GameObject newBadge;
|
|
[SerializeField] private CanvasGroup canvasGroup;
|
|
|
|
[Header("Tooltip")]
|
|
[SerializeField] private InventoryTooltipUI tooltipUI;
|
|
|
|
[Header("Settings")]
|
|
[SerializeField] private bool hideWhenZero = false;
|
|
[SerializeField] private bool dimWhenZero = true;
|
|
[SerializeField] private float zeroAlpha = 0.35f;
|
|
[SerializeField] private float ownedAlpha = 1f;
|
|
[SerializeField] private float newBadgeShowTime = 1.2f;
|
|
|
|
private int currentCount;
|
|
private Coroutine newBadgeRoutine;
|
|
|
|
public InventoryItemType ItemType => itemType;
|
|
public int CurrentCount => currentCount;
|
|
|
|
private void Awake()
|
|
{
|
|
if (canvasGroup == null)
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
|
|
|
if (newBadge != null)
|
|
newBadge.SetActive(false);
|
|
}
|
|
|
|
public void SetItemType(InventoryItemType newItemType)
|
|
{
|
|
itemType = newItemType;
|
|
}
|
|
|
|
public void SetCount(int count, bool showNewBadge)
|
|
{
|
|
int previousCount = currentCount;
|
|
currentCount = Mathf.Max(0, count);
|
|
|
|
UpdateVisualState();
|
|
|
|
if (showNewBadge && currentCount > previousCount)
|
|
ShowNewBadge();
|
|
}
|
|
|
|
public void SetIcon(Sprite sprite)
|
|
{
|
|
if (itemIcon == null)
|
|
return;
|
|
|
|
itemIcon.sprite = sprite;
|
|
itemIcon.enabled = sprite != null;
|
|
}
|
|
|
|
public void SetSlotBackground(Sprite sprite)
|
|
{
|
|
if (slotBackground == null)
|
|
return;
|
|
|
|
slotBackground.sprite = sprite;
|
|
slotBackground.enabled = sprite != null;
|
|
}
|
|
|
|
public void ShowNewBadge()
|
|
{
|
|
if (newBadge == null)
|
|
return;
|
|
|
|
if (newBadgeRoutine != null)
|
|
StopCoroutine(newBadgeRoutine);
|
|
|
|
newBadgeRoutine = StartCoroutine(NewBadgeRoutine());
|
|
}
|
|
|
|
public void HideNewBadge()
|
|
{
|
|
if (newBadgeRoutine != null)
|
|
{
|
|
StopCoroutine(newBadgeRoutine);
|
|
newBadgeRoutine = null;
|
|
}
|
|
|
|
if (newBadge != null)
|
|
newBadge.SetActive(false);
|
|
}
|
|
|
|
private IEnumerator NewBadgeRoutine()
|
|
{
|
|
newBadge.SetActive(true);
|
|
yield return new WaitForSeconds(newBadgeShowTime);
|
|
newBadge.SetActive(false);
|
|
newBadgeRoutine = null;
|
|
}
|
|
|
|
private void UpdateVisualState()
|
|
{
|
|
bool hasItem = currentCount > 0;
|
|
|
|
if (countText != null)
|
|
countText.text = $"x{currentCount}";
|
|
|
|
if (hideWhenZero)
|
|
gameObject.SetActive(hasItem);
|
|
|
|
if (canvasGroup != null && dimWhenZero)
|
|
canvasGroup.alpha = hasItem ? ownedAlpha : zeroAlpha;
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
ShowTooltip();
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
HideTooltip();
|
|
}
|
|
|
|
public void OnSelect(BaseEventData eventData)
|
|
{
|
|
ShowTooltip();
|
|
}
|
|
|
|
public void OnDeselect(BaseEventData eventData)
|
|
{
|
|
HideTooltip();
|
|
}
|
|
|
|
public void ShowTooltip()
|
|
{
|
|
if (tooltipUI != null)
|
|
tooltipUI.ShowTooltip(itemType, currentCount);
|
|
}
|
|
|
|
public void HideTooltip()
|
|
{
|
|
if (tooltipUI != null)
|
|
tooltipUI.HideTooltip();
|
|
}
|
|
}
|