47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace MedievalShopkeeper
|
|
{
|
|
[RequireComponent(typeof(Button))]
|
|
public class StockBuyButton : MonoBehaviour
|
|
{
|
|
[Tooltip("The item to buy when this button is clicked.")]
|
|
public ItemData itemData;
|
|
|
|
[Tooltip("How many of the item to buy per click.")]
|
|
public int amountToBuy = 5;
|
|
|
|
private void Start()
|
|
{
|
|
Button btn = GetComponent<Button>();
|
|
btn.onClick.AddListener(OnClick);
|
|
}
|
|
|
|
private void OnClick()
|
|
{
|
|
if (itemData != null && ShopInventory.Instance != null)
|
|
{
|
|
bool success = ShopInventory.Instance.BuyStock(itemData, amountToBuy);
|
|
|
|
if (success)
|
|
{
|
|
if (AudioManager.Instance != null)
|
|
{
|
|
AudioManager.Instance.PlayBuyStock();
|
|
}
|
|
|
|
// Withdraw it immediately to the player's hands
|
|
PlayerController player = FindAnyObjectByType<PlayerController>();
|
|
if (player != null)
|
|
{
|
|
ShopInventory.Instance.WithdrawToPlayer(itemData, amountToBuy, player);
|
|
if (UIManager.Instance != null) UIManager.Instance.CloseAllMenus();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|