using System.Collections; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class uBagItemPrefab : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler { [Header("ui")] public Image itemIcon; public TMP_Text itemCount; public Image itemBtmImg; private uBag owner; private Coroutine hoverRoutine; private float hoverDelaySeconds = 0.5f; public void Bind(uBag bagOwner, Sprite icon, string countText, Color backgroundColor, Sprite backgroundSprite, float hoverDelay) { owner = bagOwner; hoverDelaySeconds = hoverDelay > 0f ? hoverDelay : 0.5f; if (itemIcon != null) { itemIcon.sprite = icon; itemIcon.enabled = icon != null; itemIcon.color = Color.white; } if (itemCount != null) { itemCount.text = countText ?? string.Empty; } if (itemBtmImg != null) { itemBtmImg.sprite = backgroundSprite; itemBtmImg.color = backgroundSprite != null ? Color.white : backgroundColor; } } public void OnPointerEnter(PointerEventData eventData) { if (owner == null) { return; } CancelHover(); hoverRoutine = StartCoroutine(HoverDelay()); } public void OnPointerExit(PointerEventData eventData) { CancelHover(); if (owner != null) { owner.HidePreviewFor(this); } } public void OnPointerClick(PointerEventData eventData) { if (owner == null || eventData == null || eventData.button != PointerEventData.InputButton.Left) { return; } owner.SelectItemForDetail(this); } private IEnumerator HoverDelay() { yield return new WaitForSecondsRealtime(hoverDelaySeconds); hoverRoutine = null; if (owner != null) { owner.ShowPreviewFor(this); } } private void OnDisable() { CancelHover(); if (owner != null) { owner.HidePreviewFor(this); } } private void CancelHover() { if (hoverRoutine != null) { StopCoroutine(hoverRoutine); hoverRoutine = null; } } }