132 lines
3.7 KiB
C#
132 lines
3.7 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class materialPrefab : MonoBehaviour
|
|
{
|
|
[Header("imgs")]
|
|
public Image boarder;
|
|
public Image qualityBorder;
|
|
public Sprite[] qualityBorderSprites;
|
|
public Image materialImg;
|
|
public Button materialButton;
|
|
public TextMeshProUGUI materialAmount;
|
|
public Text materialName;
|
|
|
|
private Action clickAction;
|
|
|
|
private void Awake()
|
|
{
|
|
if (materialButton != null)
|
|
{
|
|
materialButton.onClick.RemoveListener(HandleClick);
|
|
materialButton.onClick.AddListener(HandleClick);
|
|
}
|
|
}
|
|
|
|
public void Bind(Sprite sprite, string displayName, string amountText, ItemRarity rarity, bool selected, bool interactable, Action onClick)
|
|
{
|
|
clickAction = onClick;
|
|
|
|
if (materialImg != null)
|
|
{
|
|
materialImg.sprite = sprite;
|
|
materialImg.enabled = sprite != null;
|
|
}
|
|
|
|
if (materialName != null)
|
|
{
|
|
materialName.text = displayName ?? string.Empty;
|
|
}
|
|
|
|
if (materialAmount != null)
|
|
{
|
|
materialAmount.text = amountText ?? string.Empty;
|
|
}
|
|
|
|
ApplyQualityBorder(rarity);
|
|
SetSelected(selected);
|
|
|
|
if (materialButton != null)
|
|
{
|
|
materialButton.interactable = interactable;
|
|
}
|
|
}
|
|
|
|
public void Bind(Sprite sprite, string displayName, string amountText, bool selected, bool interactable, Action onClick)
|
|
{
|
|
Bind(sprite, displayName, amountText, ItemRarity.None, selected, interactable, onClick);
|
|
}
|
|
|
|
public void BindOwnedRequired(Sprite sprite, string displayName, int ownedAmount, int requiredAmount, ItemRarity rarity, bool selected, bool interactable, Action onClick)
|
|
{
|
|
string ownedText = ownedAmount < requiredAmount
|
|
? $"<color=#FF4D4D>{Mathf.Max(0, ownedAmount)}</color>"
|
|
: Mathf.Max(0, ownedAmount).ToString();
|
|
string amountText = $"{ownedText}/{Mathf.Max(0, requiredAmount)}";
|
|
Bind(sprite, displayName, amountText, rarity, selected, interactable, onClick);
|
|
}
|
|
|
|
public void BindOwnedRequired(Sprite sprite, string displayName, int ownedAmount, int requiredAmount, bool selected, bool interactable, Action onClick)
|
|
{
|
|
BindOwnedRequired(sprite, displayName, ownedAmount, requiredAmount, ItemRarity.None, selected, interactable, onClick);
|
|
}
|
|
|
|
public void SetSelected(bool selected)
|
|
{
|
|
if (boarder != null)
|
|
{
|
|
boarder.enabled = selected;
|
|
}
|
|
}
|
|
|
|
private void ApplyQualityBorder(ItemRarity rarity)
|
|
{
|
|
if (qualityBorder == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Sprite sprite = ResolveQualityBorderSprite(rarity);
|
|
qualityBorder.sprite = sprite;
|
|
qualityBorder.enabled = sprite != null;
|
|
}
|
|
|
|
private Sprite ResolveQualityBorderSprite(ItemRarity rarity)
|
|
{
|
|
if (qualityBorderSprites != null && qualityBorderSprites.Length > 0)
|
|
{
|
|
int enumIndex = (int)rarity;
|
|
if (enumIndex >= 0 && enumIndex < qualityBorderSprites.Length)
|
|
{
|
|
Sprite sprite = qualityBorderSprites[enumIndex];
|
|
if (sprite != null)
|
|
{
|
|
return sprite;
|
|
}
|
|
}
|
|
|
|
if (qualityBorderSprites[0] != null)
|
|
{
|
|
return qualityBorderSprites[0];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private void HandleClick()
|
|
{
|
|
if (materialButton != null && !materialButton.interactable)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (clickAction != null)
|
|
{
|
|
clickAction.Invoke();
|
|
}
|
|
}
|
|
}
|