Wallet.cs 978 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using UnityEngine;
  3. namespace Player.Wallet
  4. {
  5. public class Wallet : MonoBehaviour
  6. {
  7. // private int _money; // 添加私有字段
  8. private int _playerMoney;
  9. public event Action<int> PlayerMoneyEvent;
  10. //public event System.Action<int> PlayerMoneyEvent;
  11. public int GetMoney => _playerMoney;
  12. // public int GetMoney => _money; // 获取金币属性
  13. void Awake()
  14. {
  15. _playerMoney = PlayerPrefs.HasKey("_playerMoney")
  16. ? PlayerPrefs.GetInt("_playerMoney")
  17. : GameBalance.InitialMoney;
  18. }
  19. private void Start()
  20. {
  21. AddMoney(0);
  22. }
  23. public void AddMoney(int value)
  24. {
  25. _playerMoney += value;
  26. WalletFunc();
  27. }
  28. public void UseMoney(int value)
  29. {
  30. _playerMoney -= value;
  31. WalletFunc();
  32. }
  33. private void WalletFunc()
  34. {
  35. PlayerPrefs.SetInt("_playerMoney", _playerMoney);
  36. PlayerMoneyEvent?.Invoke(_playerMoney);
  37. }
  38. }
  39. }