267 lines
7.0 KiB
C#
267 lines
7.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class storeItemPrefab : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
[Header("so")]
|
|
public storeItemSO thisItemSO;
|
|
|
|
[Header("basics")]
|
|
public Image thisItem_iconImage;
|
|
public Text thisItem_nameText;
|
|
public Text thisItem_amountAndLimitationText;
|
|
public Image thisPrice_iconImage;
|
|
public Text thisItem_priceText;
|
|
|
|
[Header("other")]
|
|
public GameObject rightCorner_statusImage; //这个是用来显示限量或新品等状态的图标
|
|
public GameObject leftCorner_statusImage; //这个是用来显示售罄等状态的图标
|
|
public GameObject lock_cannotClickImage; //这个是用来显示无法点击的锁图标的
|
|
|
|
[Header("texts")]
|
|
public Text why_cannot_buy;
|
|
|
|
[Header("descriptions")]
|
|
public GameObject descriptionObject;
|
|
public Text itemTitle;
|
|
public Text itemDescription;
|
|
|
|
[Header("quick buy")]
|
|
public Button quickBuyButton;
|
|
public Material quickBuy_cannotBuy_mtr;
|
|
|
|
public Action<storeItemPrefab> onItemClicked;
|
|
public Action<storeItemPrefab> onQuickBuyClicked;
|
|
|
|
private const float ShowDelay = 0.5f;
|
|
private const float HideDelay = 0.25f;
|
|
private const float FadeDuration = 0.2f;
|
|
|
|
private CanvasGroup descriptionCanvasGroup;
|
|
private Coroutine pendingShowCoroutine;
|
|
private Coroutine pendingHideCoroutine;
|
|
private int lastNotifyFrame = -1;
|
|
private Graphic quickBuyGraphic;
|
|
private Material quickBuyDefaultMaterial;
|
|
|
|
private void Awake()
|
|
{
|
|
PrepareDescriptionPanel();
|
|
PrepareQuickBuyButton();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
StopPendingDescriptionCoroutines();
|
|
if (descriptionCanvasGroup != null)
|
|
{
|
|
descriptionCanvasGroup.DOKill();
|
|
descriptionCanvasGroup.alpha = 0f;
|
|
descriptionCanvasGroup.interactable = false;
|
|
descriptionCanvasGroup.blocksRaycasts = false;
|
|
}
|
|
|
|
if (descriptionObject != null)
|
|
{
|
|
descriptionObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
NotifyClicked();
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
if (descriptionObject == null) return;
|
|
|
|
if (pendingHideCoroutine != null)
|
|
{
|
|
StopCoroutine(pendingHideCoroutine);
|
|
pendingHideCoroutine = null;
|
|
}
|
|
|
|
if (pendingShowCoroutine == null)
|
|
{
|
|
pendingShowCoroutine = StartCoroutine(ShowDescriptionWithDelay());
|
|
}
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
if (descriptionObject == null) return;
|
|
|
|
if (pendingShowCoroutine != null)
|
|
{
|
|
StopCoroutine(pendingShowCoroutine);
|
|
pendingShowCoroutine = null;
|
|
}
|
|
|
|
if (pendingHideCoroutine == null)
|
|
{
|
|
pendingHideCoroutine = StartCoroutine(HideDescriptionWithDelay());
|
|
}
|
|
}
|
|
|
|
public void NotifyClicked()
|
|
{
|
|
if (lastNotifyFrame == Time.frameCount)
|
|
{
|
|
return;
|
|
}
|
|
|
|
lastNotifyFrame = Time.frameCount;
|
|
if (onItemClicked != null)
|
|
{
|
|
onItemClicked(this);
|
|
}
|
|
}
|
|
|
|
public void NotifyQuickBuyClicked()
|
|
{
|
|
if (onQuickBuyClicked != null)
|
|
{
|
|
onQuickBuyClicked(this);
|
|
}
|
|
}
|
|
|
|
public void RefreshQuickBuyState(bool canQuickBuy)
|
|
{
|
|
if (quickBuyButton != null)
|
|
{
|
|
quickBuyButton.interactable = canQuickBuy;
|
|
}
|
|
|
|
if (quickBuyGraphic == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
quickBuyGraphic.material = canQuickBuy ? quickBuyDefaultMaterial : quickBuy_cannotBuy_mtr;
|
|
}
|
|
|
|
public void SetDescriptionContent(string title, string description)
|
|
{
|
|
if (itemTitle != null)
|
|
{
|
|
itemTitle.text = title ?? string.Empty;
|
|
}
|
|
|
|
if (itemDescription != null)
|
|
{
|
|
itemDescription.text = string.IsNullOrWhiteSpace(description) ? "暂无描述信息" : description;
|
|
}
|
|
}
|
|
|
|
private void PrepareDescriptionPanel()
|
|
{
|
|
if (descriptionObject == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
descriptionCanvasGroup = descriptionObject.GetComponent<CanvasGroup>();
|
|
if (descriptionCanvasGroup == null)
|
|
{
|
|
descriptionCanvasGroup = descriptionObject.AddComponent<CanvasGroup>();
|
|
}
|
|
|
|
descriptionCanvasGroup.DOKill();
|
|
descriptionCanvasGroup.alpha = 0f;
|
|
descriptionCanvasGroup.interactable = false;
|
|
descriptionCanvasGroup.blocksRaycasts = false;
|
|
descriptionObject.SetActive(false);
|
|
}
|
|
|
|
private void PrepareQuickBuyButton()
|
|
{
|
|
if (quickBuyButton == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
quickBuyButton.onClick.RemoveListener(NotifyQuickBuyClicked);
|
|
quickBuyButton.onClick.AddListener(NotifyQuickBuyClicked);
|
|
|
|
quickBuyGraphic = quickBuyButton.targetGraphic;
|
|
if (quickBuyGraphic == null)
|
|
{
|
|
quickBuyGraphic = quickBuyButton.GetComponent<Graphic>();
|
|
}
|
|
|
|
if (quickBuyGraphic != null)
|
|
{
|
|
quickBuyDefaultMaterial = quickBuyGraphic.material;
|
|
}
|
|
}
|
|
|
|
private IEnumerator ShowDescriptionWithDelay()
|
|
{
|
|
yield return new WaitForSeconds(ShowDelay);
|
|
pendingShowCoroutine = null;
|
|
ShowDescriptionPanel();
|
|
}
|
|
|
|
private IEnumerator HideDescriptionWithDelay()
|
|
{
|
|
yield return new WaitForSeconds(HideDelay);
|
|
pendingHideCoroutine = null;
|
|
HideDescriptionPanel();
|
|
}
|
|
|
|
private void ShowDescriptionPanel()
|
|
{
|
|
if (descriptionObject == null || descriptionCanvasGroup == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
descriptionCanvasGroup.DOKill();
|
|
descriptionObject.SetActive(true);
|
|
descriptionCanvasGroup.interactable = true;
|
|
descriptionCanvasGroup.blocksRaycasts = true;
|
|
descriptionCanvasGroup.DOFade(1f, FadeDuration).SetEase(Ease.OutQuad);
|
|
}
|
|
|
|
private void HideDescriptionPanel()
|
|
{
|
|
if (descriptionObject == null || descriptionCanvasGroup == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
descriptionCanvasGroup.DOKill();
|
|
descriptionCanvasGroup.interactable = false;
|
|
descriptionCanvasGroup.blocksRaycasts = false;
|
|
descriptionCanvasGroup.DOFade(0f, FadeDuration)
|
|
.SetEase(Ease.OutQuad)
|
|
.OnComplete(() =>
|
|
{
|
|
if (descriptionObject != null)
|
|
{
|
|
descriptionObject.SetActive(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void StopPendingDescriptionCoroutines()
|
|
{
|
|
if (pendingShowCoroutine != null)
|
|
{
|
|
StopCoroutine(pendingShowCoroutine);
|
|
pendingShowCoroutine = null;
|
|
}
|
|
|
|
if (pendingHideCoroutine != null)
|
|
{
|
|
StopCoroutine(pendingHideCoroutine);
|
|
pendingHideCoroutine = null;
|
|
}
|
|
}
|
|
}
|