1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System;
- using UnityEngine;
- namespace Player.Wallet
- {
- public class Wallet : MonoBehaviour
- {
- // private int _money; // 添加私有字段
- private int _playerMoney;
- public event Action<int> PlayerMoneyEvent;
- //public event System.Action<int> PlayerMoneyEvent;
- public int GetMoney => _playerMoney;
- // public int GetMoney => _money; // 获取金币属性
- void Awake()
- {
- _playerMoney = PlayerPrefs.HasKey("_playerMoney")
- ? PlayerPrefs.GetInt("_playerMoney")
- : GameBalance.InitialMoney;
- }
- private void Start()
- {
- AddMoney(0);
- }
- public void AddMoney(int value)
- {
- _playerMoney += value;
- WalletFunc();
- }
-
- public void UseMoney(int value)
- {
- _playerMoney -= value;
- WalletFunc();
- }
- private void WalletFunc()
- {
- PlayerPrefs.SetInt("_playerMoney", _playerMoney);
- PlayerMoneyEvent?.Invoke(_playerMoney);
- }
-
- }
-
- }
|