AccountFileInfo.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Core.Utility;
  5. using Fort23.UTool;
  6. using GameLogic.Bag;
  7. using GameLogic.Hero;
  8. using UnityEngine;
  9. using Utility;
  10. public class AccountFileInfo : Singleton<AccountFileInfo>
  11. {
  12. public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
  13. public PlayerData playerData = new PlayerData();
  14. [System.Serializable]
  15. public class PlayerData
  16. {
  17. public List<ItemData> ItemListData = new List<ItemData>();
  18. /// <summary>
  19. /// 主力英雄数据(主力等级(=afk2的共鸣等级))
  20. /// </summary>
  21. public List<HeroData> HeroListInLeadDatas = new List<HeroData>();
  22. /// <summary>
  23. /// 后备英雄(共享等级)
  24. /// </summary>
  25. public List<HeroData> HeroListInBackDatas = new List<HeroData>();
  26. }
  27. public void LoadPlayerData()
  28. {
  29. // #if UNITY_EDITOR || UNITY_STANDALONE_WIN
  30. // persistentDataPath = Application.streamingAssetsPath + $"/{playerId}playerSetting.txt";
  31. // #elif UNITY_ANDROID
  32. // playerSettingPath = Application.persistentDataPath+$"/{playerId}playerSetting.txt";
  33. // #elif UNITY_IPHONE
  34. // playerSettingPath = Application.persistentDataPath+$"/{playerId}playerSetting.txt";
  35. // #endif
  36. if (!File.Exists(persistentDataPath))
  37. {
  38. LogTool.Log("没有文件: " + persistentDataPath);
  39. ClearInitPlayerData();
  40. // File.Create(persistentDataPath).Close();
  41. }
  42. LogTool.Log("读取=文件: " + persistentDataPath);
  43. StreamReader sr = File.OpenText(persistentDataPath);
  44. string data = sr.ReadToEnd();
  45. sr.Close();
  46. playerData = new PlayerData();
  47. JsonUtility.FromJsonOverwrite(data, playerData);
  48. }
  49. private int lastHeroIdx = 0;
  50. /// <summary>
  51. /// 保存英雄数据
  52. /// </summary>
  53. /// <param name="heroInfo"></param>
  54. public void SaveHeroData(HeroInfo heroInfo)
  55. {
  56. var lastHeroData= playerData.HeroListInLeadDatas[lastHeroIdx];
  57. if (heroInfo.modelID == lastHeroData.heroModelId)
  58. {
  59. playerData.HeroListInLeadDatas[lastHeroIdx] = heroInfo.ToHeroData();
  60. SavePlayerData();
  61. return;
  62. }
  63. for (int i = 0; i < playerData.HeroListInLeadDatas.Count; i++)
  64. {
  65. HeroData heroData = playerData.HeroListInLeadDatas[i];
  66. if (heroData.heroModelId == heroInfo.modelID)
  67. {
  68. playerData.HeroListInLeadDatas[i] = heroInfo.ToHeroData();
  69. //存下来,用于快速查找
  70. lastHeroIdx = i;
  71. SavePlayerData();
  72. return;
  73. }
  74. }
  75. }
  76. private int lastItemIdx = 0;
  77. /// <summary>
  78. /// 保存item数据
  79. /// </summary>
  80. /// <param name="itemInfo"></param>
  81. public void SaveItemData(ItemInfo itemInfo)
  82. {
  83. if (itemInfo.ID == playerData.ItemListData[lastItemIdx].itemId)
  84. {
  85. playerData.ItemListData[lastItemIdx] = itemInfo.ToItemData();
  86. SavePlayerData();
  87. return;
  88. }
  89. for (int i = 0; i < playerData.ItemListData.Count; i++)
  90. {
  91. ItemData itemData = playerData.ItemListData[i];
  92. if (itemData.itemId == itemInfo.ID)
  93. {
  94. playerData.ItemListData[i] = itemInfo.ToItemData();
  95. //存下来,用于快速查找
  96. lastItemIdx = i;
  97. SavePlayerData();
  98. return;
  99. }
  100. }
  101. }
  102. public void SavePlayerData()
  103. {
  104. if (!string.IsNullOrEmpty(persistentDataPath))
  105. {
  106. string playerSettingJson = JsonManager.ToJson(playerData);
  107. File.WriteAllText(persistentDataPath, playerSettingJson);
  108. }
  109. }
  110. public void DeleteFile(string filePath)
  111. {
  112. if (File.Exists(filePath))
  113. {
  114. File.Delete(filePath); // 删除文件
  115. LogTool.Log($"文件已删除:{filePath}");
  116. }
  117. else
  118. {
  119. LogTool.Log($"文件不存在:{filePath}");
  120. }
  121. }
  122. /// <summary>
  123. /// 不要服务器的话,这里初始化玩家的起始数据
  124. /// </summary>
  125. public void ClearInitPlayerData()
  126. {
  127. ItemData coin = new ItemData()
  128. {
  129. itemId = GlobalParam.Item_Coin_ID,
  130. itemCount = 100,
  131. };
  132. ItemData diamond = new ItemData()
  133. {
  134. itemId = GlobalParam.Item_Diamond_ID,
  135. itemCount = 1000,
  136. };
  137. ItemData heroExp = new ItemData()
  138. {
  139. itemId = GlobalParam.Item_HeroExp_ID,
  140. itemCount = 10000,
  141. };
  142. playerData.ItemListData.Add(coin);
  143. playerData.ItemListData.Add(diamond);
  144. playerData.ItemListData.Add(heroExp);
  145. HeroData heroData1 = new HeroData
  146. {
  147. heroModelId = 105,
  148. heroPowerId = 1,
  149. heroPromoteId = 3
  150. };
  151. HeroData heroData2 = new HeroData
  152. {
  153. heroModelId = 107,
  154. heroPowerId = 1,
  155. heroPromoteId = 3
  156. };
  157. HeroData heroData3 = new HeroData
  158. {
  159. heroModelId = 116,
  160. heroPowerId = 1,
  161. heroPromoteId = 3
  162. };
  163. HeroData heroData4 = new HeroData
  164. {
  165. heroModelId = 113,
  166. heroPowerId = 1,
  167. heroPromoteId = 3
  168. };
  169. playerData.HeroListInLeadDatas.Add(heroData1);
  170. playerData.HeroListInLeadDatas.Add(heroData2);
  171. playerData.HeroListInLeadDatas.Add(heroData3);
  172. playerData.HeroListInLeadDatas.Add(heroData4);
  173. SavePlayerData();
  174. }
  175. [System.Serializable]
  176. public class HeroData
  177. {
  178. /// <summary>
  179. /// 英雄基础信息
  180. /// </summary>
  181. public int heroModelId;
  182. /// <summary>
  183. /// 英雄等级
  184. /// </summary>
  185. public int heroPowerId;
  186. /// <summary>
  187. /// 星级
  188. /// </summary>
  189. public int heroPromoteId;
  190. }
  191. [System.Serializable]
  192. public class ItemData
  193. {
  194. public int itemId;
  195. public long itemCount;
  196. public int guid;
  197. }
  198. }