GameDataManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using UnityEngine;
  2. using System;
  3. using TMPro;
  4. using Player.Wallet; // 添加命名空间
  5. public class GameDataManager : MonoBehaviour
  6. {
  7. // 单例实例
  8. public static GameDataManager Instance { get; private set; }
  9. // 当前金币数量
  10. public int coins { get; private set; }
  11. // 金币变动事件(用于UI等监听)
  12. public Action onCoinsChanged;
  13. // 游戏场景中的金币文本引用
  14. [SerializeField] private TextMeshProUGUI gameSceneCoinsText; // 引用游戏场景中的金币文本
  15. /// <summary>
  16. /// 刷新金币数据,从PlayerWallet或PlayerPrefs读取,并更新UI
  17. /// </summary>
  18. public void RefreshCoins()
  19. {
  20. if (ComponentsManager.PlayerWallet != null)
  21. {
  22. coins = ComponentsManager.PlayerWallet.GetMoney;
  23. Debug.Log($"[GameDataManager] Refreshed coins from PlayerWallet: {coins}");
  24. }
  25. else
  26. {
  27. coins = PlayerPrefs.GetInt("_playerMoney", 0);
  28. Debug.Log($"[GameDataManager] Refreshed coins from PlayerPrefs: {coins}");
  29. }
  30. // 更新UI显示
  31. UpdateCoinsDisplay();
  32. // 通知其他监听者
  33. onCoinsChanged?.Invoke();
  34. }
  35. /// <summary>
  36. /// 脚本启用时加载金币数据
  37. /// </summary>
  38. private void OnEnable()
  39. {
  40. // 确保单例设置正确
  41. /* if (Instance == null)
  42. {
  43. Instance = this;
  44. DontDestroyOnLoad(gameObject);
  45. LoadCoins();
  46. }
  47. else if (Instance != this)
  48. {
  49. Debug.LogWarning("Multiple GameDataManager instances detected!");
  50. Destroy(gameObject);
  51. }*/
  52. if (Instance == this)
  53. {
  54. LoadCoins();
  55. Debug.Log("[GameDataManager] Reloaded coins on enable");
  56. }
  57. }
  58. /// <summary>
  59. /// 单例初始化,防止重复实例,加载金币数据
  60. /// </summary>
  61. private void Awake()
  62. {
  63. if (Instance == null)
  64. {
  65. Instance = this;
  66. DontDestroyOnLoad(gameObject);
  67. LoadCoins();
  68. }
  69. else
  70. {
  71. Destroy(gameObject);
  72. }
  73. }
  74. /// <summary>
  75. /// 应用暂停/恢复时保存或加载金币数据
  76. /// </summary>
  77. /// <param name="pause">是否暂停</param>
  78. private void OnApplicationPause(bool pause)
  79. {
  80. if (pause)
  81. {
  82. SaveCoins();
  83. }
  84. else
  85. {
  86. // 从后台恢复时重新加载数据
  87. LoadCoins();
  88. }
  89. }
  90. /// <summary>
  91. /// 应用退出时保存金币数据
  92. /// </summary>
  93. private void OnApplicationQuit()
  94. {
  95. SaveCoins();
  96. }
  97. /// <summary>
  98. /// 从PlayerWallet或PlayerPrefs加载金币数据
  99. /// </summary>
  100. private void LoadCoins()
  101. {
  102. if (ComponentsManager.PlayerWallet != null)
  103. {
  104. coins = ComponentsManager.PlayerWallet.GetMoney;
  105. Debug.Log($"[GameDataManager] Loaded coins from PlayerWallet: {coins}");
  106. }
  107. else
  108. {
  109. coins = PlayerPrefs.GetInt("_playerMoney", 0);
  110. Debug.Log($"[GameDataManager] Loaded coins from PlayerPrefs: {coins}");
  111. }
  112. // 更新显示
  113. UpdateCoinsDisplay();
  114. // 通知其他监听者
  115. onCoinsChanged?.Invoke();
  116. }
  117. /// <summary>
  118. /// 保存金币数据到PlayerPrefs
  119. /// </summary>
  120. public void SaveCoins()
  121. {
  122. if (ComponentsManager.PlayerWallet != null)
  123. {
  124. coins = ComponentsManager.PlayerWallet.GetMoney;
  125. }
  126. PlayerPrefs.SetInt("_playerMoney", coins);
  127. PlayerPrefs.Save();
  128. Debug.Log($"[GameDataManager] Saved coins: {coins}");
  129. }
  130. /// <summary>
  131. /// 启动时从PlayerPrefs加载金币数据并通知监听者
  132. /// </summary>
  133. private void Start()
  134. {
  135. // 从PlayerPrefs加载保存的金币数据
  136. if (ComponentsManager.PlayerWallet != null)
  137. {
  138. coins = ComponentsManager.PlayerWallet.GetMoney;
  139. }
  140. else
  141. {
  142. coins = PlayerPrefs.GetInt("_playerMoney", 0);
  143. }
  144. // 通知所有监听者更新显示
  145. onCoinsChanged?.Invoke();
  146. }
  147. /// <summary>
  148. /// 设置金币数量,并同步到PlayerWallet和PlayerPrefs
  149. /// </summary>
  150. /// <param name="amount">新金币数量</param>
  151. public void UpdateCoins(int amount)
  152. {
  153. int difference = amount - coins; // 计算差值
  154. coins = amount;
  155. // 同步到 PlayerWallet
  156. if (ComponentsManager.PlayerWallet != null)
  157. {
  158. if (difference > 0)
  159. {
  160. ComponentsManager.PlayerWallet.AddMoney(difference);
  161. }
  162. else if (difference < 0)
  163. {
  164. ComponentsManager.PlayerWallet.UseMoney(-difference); // 使用正数作为参数
  165. }
  166. }
  167. // 保存到 PlayerPrefs
  168. SaveCoins();
  169. // 通知更新
  170. onCoinsChanged?.Invoke();
  171. }
  172. /// <summary>
  173. /// 刷新金币UI显示
  174. /// </summary>
  175. private void UpdateCoinsDisplay()
  176. {
  177. if (gameSceneCoinsText != null)
  178. {
  179. gameSceneCoinsText.text = $"{coins}";
  180. Debug.Log($"[GameDataManager] Updated coins display: {coins}");
  181. }
  182. }
  183. /// <summary>
  184. /// 增加金币,并同步到PlayerWallet和PlayerPrefs
  185. /// </summary>
  186. /// <param name="amount">增加的金币数量</param>
  187. public void AddCoins(int amount)
  188. {
  189. int oldCoins = coins;
  190. coins += amount;
  191. Debug.Log($"Adding coins: {amount}, Old: {oldCoins}, New: {coins}");
  192. if (ComponentsManager.PlayerWallet != null)
  193. {
  194. ComponentsManager.PlayerWallet.AddMoney(amount);
  195. Debug.Log($"PlayerWallet updated, new balance: {ComponentsManager.PlayerWallet.GetMoney}");
  196. }
  197. SaveCoins();
  198. onCoinsChanged?.Invoke();
  199. }
  200. /// <summary>
  201. /// 设置游戏场景中的金币文本引用,并立即刷新显示
  202. /// </summary>
  203. /// <param name="coinsText">金币文本组件</param>
  204. public void SetGameSceneCoinsText(TextMeshProUGUI coinsText)
  205. {
  206. gameSceneCoinsText = coinsText;
  207. // 立即更新显示
  208. if (gameSceneCoinsText != null)
  209. {
  210. gameSceneCoinsText.text = $"{coins}";
  211. }
  212. }
  213. }