73 lines
1.4 KiB
C#
73 lines
1.4 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
|
|
public class CoinManager : MonoBehaviour
|
|
{
|
|
public static CoinManager Instance;
|
|
|
|
[Header("Coin Data")]
|
|
public int currentCoins = 0;
|
|
|
|
[Header("UI")]
|
|
public TMP_Text coinText;
|
|
|
|
[Header("Scene Setting")]
|
|
public bool dontDestroyOnLoad = true;
|
|
|
|
void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
|
|
if (dontDestroyOnLoad)
|
|
{
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
UpdateCoinUI();
|
|
}
|
|
|
|
public void AddCoin(int amount)
|
|
{
|
|
currentCoins += amount;
|
|
UpdateCoinUI();
|
|
|
|
Debug.Log("Coin Added: " + amount + " / Total Coin: " + currentCoins);
|
|
}
|
|
|
|
public bool SpendCoin(int amount)
|
|
{
|
|
if (currentCoins < amount)
|
|
{
|
|
Debug.Log("Not enough coins. Current: " + currentCoins + " / Need: " + amount);
|
|
return false;
|
|
}
|
|
|
|
currentCoins -= amount;
|
|
UpdateCoinUI();
|
|
|
|
Debug.Log("Coin Spent: " + amount + " / Total Coin: " + currentCoins);
|
|
return true;
|
|
}
|
|
|
|
public int GetCoinCount()
|
|
{
|
|
return currentCoins;
|
|
}
|
|
|
|
void UpdateCoinUI()
|
|
{
|
|
if (coinText != null)
|
|
{
|
|
coinText.text = "Coin: " + currentCoins;
|
|
}
|
|
}
|
|
} |