-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanceManager.cs
More file actions
64 lines (53 loc) · 1.56 KB
/
BalanceManager.cs
File metadata and controls
64 lines (53 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Runtime.CompilerServices;
using TMPro;
using UnityEngine;
public class BalanceManager : MonoBehaviour
{
// Attach to the balance game object
private TextMeshProUGUI balanceTextUI;
[HideInInspector] public float balanceAmount = 0f;
private const float DEFAULT_BALANCE = 10f;
private const string BALANCE_KEY = "Balance";
public GameObject insufficientBalancePopUp; // Integrate popups using your UI handling system via UIManager scriptê
private void Start()
{
balanceTextUI = GetComponentInChildren<TextMeshProUGUI>();
RetrieveBalance();
}
public void UpdateBalanceUI()
{
balanceTextUI.text = "Balance: " + balanceAmount.ToString() + "$";
}
public void AddBalance(float amount)
{
balanceAmount += amount;
SaveBalance();
UpdateBalanceUI();
}
private void SaveBalance()
{
PlayerPrefs.SetFloat(BALANCE_KEY, balanceAmount);
PlayerPrefs.Save();
}
private void RetrieveBalance()
{
balanceAmount = PlayerPrefs.GetFloat(BALANCE_KEY, DEFAULT_BALANCE);
UpdateBalanceUI();
}
public bool isSufficient(float amount)
{
return balanceAmount + amount >= 0;
}
public void ResetBalance()
{
PlayerPrefs.DeleteKey(BALANCE_KEY);
RetrieveBalance();
Debug.Log($"ResetBalance{balanceAmount}");
}
// Testing
public void CleanMemory()
{
PlayerPrefs.DeleteAll();
RetrieveBalance();
}
}