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; // 引用游戏场景中的金币文本 /// /// 刷新金币数据,从PlayerWallet或PlayerPrefs读取,并更新UI /// 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(); } /// /// 脚本启用时加载金币数据 /// 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"); } } /// /// 单例初始化,防止重复实例,加载金币数据 /// private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); LoadCoins(); } else { Destroy(gameObject); } } /// /// 应用暂停/恢复时保存或加载金币数据 /// /// 是否暂停 private void OnApplicationPause(bool pause) { if (pause) { SaveCoins(); } else { // 从后台恢复时重新加载数据 LoadCoins(); } } /// /// 应用退出时保存金币数据 /// private void OnApplicationQuit() { SaveCoins(); } /// /// 从PlayerWallet或PlayerPrefs加载金币数据 /// 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(); } /// /// 保存金币数据到PlayerPrefs /// public void SaveCoins() { if (ComponentsManager.PlayerWallet != null) { coins = ComponentsManager.PlayerWallet.GetMoney; } PlayerPrefs.SetInt("_playerMoney", coins); PlayerPrefs.Save(); Debug.Log($"[GameDataManager] Saved coins: {coins}"); } /// /// 启动时从PlayerPrefs加载金币数据并通知监听者 /// private void Start() { // 从PlayerPrefs加载保存的金币数据 if (ComponentsManager.PlayerWallet != null) { coins = ComponentsManager.PlayerWallet.GetMoney; } else { coins = PlayerPrefs.GetInt("_playerMoney", 0); } // 通知所有监听者更新显示 onCoinsChanged?.Invoke(); } /// /// 设置金币数量,并同步到PlayerWallet和PlayerPrefs /// /// 新金币数量 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(); } /// /// 刷新金币UI显示 /// private void UpdateCoinsDisplay() { if (gameSceneCoinsText != null) { gameSceneCoinsText.text = $"{coins}"; Debug.Log($"[GameDataManager] Updated coins display: {coins}"); } } /// /// 增加金币,并同步到PlayerWallet和PlayerPrefs /// /// 增加的金币数量 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(); } /// /// 设置游戏场景中的金币文本引用,并立即刷新显示 /// /// 金币文本组件 public void SetGameSceneCoinsText(TextMeshProUGUI coinsText) { gameSceneCoinsText = coinsText; // 立即更新显示 if (gameSceneCoinsText != null) { gameSceneCoinsText.text = $"{coins}"; } } }