AccountFileInfo.cs 6.3 KB

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