123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- using UnityEngine;
- using System;
- using TMPro;
- using Player.Wallet; // 添加命名空间
- public class GameDataManager : MonoBehaviour
- {
- // 单例实例
- public static GameDataManager Instance { get; private set; }
-
- // 当前金币数量
- public int coins { get; private set; }
- // 金币变动事件(用于UI等监听)
- public Action onCoinsChanged;
- // 游戏场景中的金币文本引用
- [SerializeField] private TextMeshProUGUI gameSceneCoinsText; // 引用游戏场景中的金币文本
- /// <summary>
- /// 刷新金币数据,从PlayerWallet或PlayerPrefs读取,并更新UI
- /// </summary>
- public void RefreshCoins()
- {
- if (ComponentsManager.PlayerWallet != null)
- {
- coins = ComponentsManager.PlayerWallet.GetMoney;
- Debug.Log($"[GameDataManager] Refreshed coins from PlayerWallet: {coins}");
- }
- else
- {
- coins = PlayerPrefs.GetInt("_playerMoney", 0);
- Debug.Log($"[GameDataManager] Refreshed coins from PlayerPrefs: {coins}");
- }
-
- // 更新UI显示
- UpdateCoinsDisplay();
-
- // 通知其他监听者
- onCoinsChanged?.Invoke();
- }
- /// <summary>
- /// 脚本启用时加载金币数据
- /// </summary>
- private void OnEnable()
- {
- // 确保单例设置正确
- /* if (Instance == null)
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- LoadCoins();
- }
- else if (Instance != this)
- {
- Debug.LogWarning("Multiple GameDataManager instances detected!");
- Destroy(gameObject);
- }*/
- if (Instance == this)
- {
- LoadCoins();
- Debug.Log("[GameDataManager] Reloaded coins on enable");
- }
- }
- /// <summary>
- /// 单例初始化,防止重复实例,加载金币数据
- /// </summary>
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- LoadCoins();
- }
- else
- {
- Destroy(gameObject);
- }
- }
- /// <summary>
- /// 应用暂停/恢复时保存或加载金币数据
- /// </summary>
- /// <param name="pause">是否暂停</param>
- private void OnApplicationPause(bool pause)
- {
- if (pause)
- {
- SaveCoins();
- }
- else
- {
- // 从后台恢复时重新加载数据
- LoadCoins();
- }
- }
- /// <summary>
- /// 应用退出时保存金币数据
- /// </summary>
- private void OnApplicationQuit()
- {
- SaveCoins();
- }
- /// <summary>
- /// 从PlayerWallet或PlayerPrefs加载金币数据
- /// </summary>
- private void LoadCoins()
- {
- if (ComponentsManager.PlayerWallet != null)
- {
- coins = ComponentsManager.PlayerWallet.GetMoney;
- Debug.Log($"[GameDataManager] Loaded coins from PlayerWallet: {coins}");
- }
- else
- {
- coins = PlayerPrefs.GetInt("_playerMoney", 0);
- Debug.Log($"[GameDataManager] Loaded coins from PlayerPrefs: {coins}");
- }
-
- // 更新显示
- UpdateCoinsDisplay();
-
- // 通知其他监听者
- onCoinsChanged?.Invoke();
- }
- /// <summary>
- /// 保存金币数据到PlayerPrefs
- /// </summary>
- public void SaveCoins()
- {
- if (ComponentsManager.PlayerWallet != null)
- {
- coins = ComponentsManager.PlayerWallet.GetMoney;
- }
-
- PlayerPrefs.SetInt("_playerMoney", coins);
- PlayerPrefs.Save();
- Debug.Log($"[GameDataManager] Saved coins: {coins}");
- }
- /// <summary>
- /// 启动时从PlayerPrefs加载金币数据并通知监听者
- /// </summary>
- private void Start()
- {
- // 从PlayerPrefs加载保存的金币数据
- if (ComponentsManager.PlayerWallet != null)
- {
- coins = ComponentsManager.PlayerWallet.GetMoney;
- }
- else
- {
- coins = PlayerPrefs.GetInt("_playerMoney", 0);
- }
-
- // 通知所有监听者更新显示
- onCoinsChanged?.Invoke();
- }
- /// <summary>
- /// 设置金币数量,并同步到PlayerWallet和PlayerPrefs
- /// </summary>
- /// <param name="amount">新金币数量</param>
- public void UpdateCoins(int amount)
- {
- int difference = amount - coins; // 计算差值
- coins = amount;
-
- // 同步到 PlayerWallet
- if (ComponentsManager.PlayerWallet != null)
- {
- if (difference > 0)
- {
- ComponentsManager.PlayerWallet.AddMoney(difference);
- }
- else if (difference < 0)
- {
- ComponentsManager.PlayerWallet.UseMoney(-difference); // 使用正数作为参数
- }
- }
-
- // 保存到 PlayerPrefs
- SaveCoins();
-
- // 通知更新
- onCoinsChanged?.Invoke();
- }
- /// <summary>
- /// 刷新金币UI显示
- /// </summary>
- private void UpdateCoinsDisplay()
- {
- if (gameSceneCoinsText != null)
- {
- gameSceneCoinsText.text = $"{coins}";
- Debug.Log($"[GameDataManager] Updated coins display: {coins}");
- }
- }
- /// <summary>
- /// 增加金币,并同步到PlayerWallet和PlayerPrefs
- /// </summary>
- /// <param name="amount">增加的金币数量</param>
- public void AddCoins(int amount)
- {
- int oldCoins = coins;
- coins += amount;
-
- Debug.Log($"Adding coins: {amount}, Old: {oldCoins}, New: {coins}");
-
- if (ComponentsManager.PlayerWallet != null)
- {
- ComponentsManager.PlayerWallet.AddMoney(amount);
- Debug.Log($"PlayerWallet updated, new balance: {ComponentsManager.PlayerWallet.GetMoney}");
- }
-
- SaveCoins();
- onCoinsChanged?.Invoke();
- }
- /// <summary>
- /// 设置游戏场景中的金币文本引用,并立即刷新显示
- /// </summary>
- /// <param name="coinsText">金币文本组件</param>
- public void SetGameSceneCoinsText(TextMeshProUGUI coinsText)
- {
- gameSceneCoinsText = coinsText;
- // 立即更新显示
- if (gameSceneCoinsText != null)
- {
- gameSceneCoinsText.text = $"{coins}";
- }
- }
- }
|