AccountFileInfo.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /// 英雄数据
  20. /// </summary>
  21. public List<HeroData> HeroListData = 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.HeroListData[lastHeroIdx];
  57. if (heroInfo.modelID == lastHeroData.heroModelId)
  58. {
  59. playerData.HeroListData[lastHeroIdx] = heroInfo.ToHeroData();
  60. SavePlayerData();
  61. return;
  62. }
  63. for (int i = 0; i < playerData.HeroListData.Count; i++)
  64. {
  65. HeroData heroData = playerData.HeroListData[i];
  66. if (heroData.heroModelId == heroInfo.modelID)
  67. {
  68. playerData.HeroListData[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. isLead = true,
  151. };
  152. HeroData heroData2 = new HeroData
  153. {
  154. heroModelId = 107,
  155. heroPowerId = 1,
  156. heroPromoteId = 3,
  157. isLead = true,
  158. };
  159. HeroData heroData3 = new HeroData
  160. {
  161. heroModelId = 116,
  162. heroPowerId = 1,
  163. heroPromoteId = 3,
  164. isLead = true,
  165. };
  166. HeroData heroData4 = new HeroData
  167. {
  168. heroModelId = 113,
  169. heroPowerId = 1,
  170. heroPromoteId = 3,
  171. isLead = true,
  172. };
  173. playerData.HeroListData.Add(heroData1);
  174. playerData.HeroListData.Add(heroData2);
  175. playerData.HeroListData.Add(heroData3);
  176. playerData.HeroListData.Add(heroData4);
  177. SavePlayerData();
  178. }
  179. [System.Serializable]
  180. public class HeroData
  181. {
  182. /// <summary>
  183. /// 英雄基础信息
  184. /// </summary>
  185. public int heroModelId;
  186. /// <summary>
  187. /// 英雄等级
  188. /// </summary>
  189. public int heroPowerId;
  190. /// <summary>
  191. /// 星级
  192. /// </summary>
  193. public int heroPromoteId;
  194. /// <summary>
  195. /// 是否为主力
  196. /// </summary>
  197. public bool isLead;
  198. }
  199. [System.Serializable]
  200. public class ItemData
  201. {
  202. public int itemId;
  203. public long itemCount;
  204. public int guid;
  205. }
  206. }