Wallet.cs 797 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using UnityEngine;
  3. namespace Player.Wallet
  4. {
  5. public class Wallet : MonoBehaviour
  6. {
  7. private int _playerMoney;
  8. public event Action<int> PlayerMoneyEvent;
  9. public int GetMoney => _playerMoney;
  10. void Awake()
  11. {
  12. _playerMoney = PlayerPrefs.HasKey("_playerMoney")
  13. ? PlayerPrefs.GetInt("_playerMoney")
  14. : GameBalance.InitialMoney;
  15. }
  16. private void Start()
  17. {
  18. AddMoney(2220);
  19. }
  20. public void AddMoney(int value)
  21. {
  22. _playerMoney += value;
  23. WalletFunc();
  24. }
  25. public void UseMoney(int value)
  26. {
  27. _playerMoney -= value;
  28. WalletFunc();
  29. }
  30. private void WalletFunc()
  31. {
  32. PlayerPrefs.SetInt("_playerMoney", _playerMoney);
  33. PlayerMoneyEvent?.Invoke(_playerMoney);
  34. }
  35. }
  36. }