84 lines
2.1 KiB
C#
84 lines
2.1 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class materialPrefab : MonoBehaviour
|
|
{
|
|
[Header("imgs")]
|
|
public Image boarder;
|
|
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, 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;
|
|
}
|
|
|
|
SetSelected(selected);
|
|
|
|
if (materialButton != null)
|
|
{
|
|
materialButton.interactable = interactable;
|
|
}
|
|
}
|
|
|
|
public void BindOwnedRequired(Sprite sprite, string displayName, int ownedAmount, int requiredAmount, 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, selected, interactable, onClick);
|
|
}
|
|
|
|
public void SetSelected(bool selected)
|
|
{
|
|
if (boarder != null)
|
|
{
|
|
boarder.enabled = selected;
|
|
}
|
|
}
|
|
|
|
private void HandleClick()
|
|
{
|
|
if (materialButton != null && !materialButton.interactable)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (clickAction != null)
|
|
{
|
|
clickAction.Invoke();
|
|
}
|
|
}
|
|
}
|