AccountFileInfo.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Fort23.UTool;
  5. using UnityEngine;
  6. using Utility;
  7. public class AccountFileInfo : Singleton<AccountFileInfo>
  8. {
  9. public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
  10. public PlayerData playerData = new PlayerData();
  11. [System.Serializable]
  12. public class PlayerData
  13. {
  14. /// <summary>
  15. /// 主力英雄数据(主力等级(=afk2的共鸣等级))
  16. /// </summary>
  17. public List<HeroData> HeroListInLeadDatas = new List<HeroData>();
  18. /// <summary>
  19. /// 后备英雄(共享等级)
  20. /// </summary>
  21. public List<HeroData> HeroListInBackDatas = new List<HeroData>();
  22. }
  23. public void LoadPlayerData()
  24. {
  25. // #if UNITY_EDITOR || UNITY_STANDALONE_WIN
  26. // persistentDataPath = Application.streamingAssetsPath + $"/{playerId}playerSetting.txt";
  27. // #elif UNITY_ANDROID
  28. // playerSettingPath = Application.persistentDataPath+$"/{playerId}playerSetting.txt";
  29. // #elif UNITY_IPHONE
  30. // playerSettingPath = Application.persistentDataPath+$"/{playerId}playerSetting.txt";
  31. // #endif
  32. if (!File.Exists(persistentDataPath))
  33. {
  34. LogTool.Log("没有文件: " + persistentDataPath);
  35. ClearInitPlayerData();
  36. // File.Create(persistentDataPath).Close();
  37. }
  38. LogTool.Log("读取=文件: " + persistentDataPath);
  39. StreamReader sr = File.OpenText(persistentDataPath);
  40. string data = sr.ReadToEnd();
  41. sr.Close();
  42. playerData = new PlayerData();
  43. JsonUtility.FromJsonOverwrite(data, playerData);
  44. }
  45. public void SavePlayerData()
  46. {
  47. if (!string.IsNullOrEmpty(persistentDataPath))
  48. {
  49. string playerSettingJson = JsonManager.ToJson(playerData);
  50. File.WriteAllText(persistentDataPath, playerSettingJson);
  51. }
  52. }
  53. public void DeleteFile(string filePath)
  54. {
  55. if (File.Exists(filePath))
  56. {
  57. File.Delete(filePath); // 删除文件
  58. LogTool.Log($"文件已删除:{filePath}");
  59. }
  60. else
  61. {
  62. LogTool.Log($"文件不存在:{filePath}");
  63. }
  64. }
  65. /// <summary>
  66. /// 不要服务器的话,这里初始化玩家的起始数据
  67. /// </summary>
  68. public void ClearInitPlayerData()
  69. {
  70. HeroData heroData1 = new HeroData
  71. {
  72. heroModelId = 105,
  73. heroPowerId = 1,
  74. heroPromoteId = 3
  75. };
  76. HeroData heroData2 = new HeroData
  77. {
  78. heroModelId = 107,
  79. heroPowerId = 1,
  80. heroPromoteId = 3
  81. };
  82. HeroData heroData3 = new HeroData
  83. {
  84. heroModelId = 116,
  85. heroPowerId = 1,
  86. heroPromoteId = 3
  87. };
  88. HeroData heroData4 = new HeroData
  89. {
  90. heroModelId = 113,
  91. heroPowerId = 1,
  92. heroPromoteId = 3
  93. };
  94. playerData.HeroListInLeadDatas.Add(heroData1);
  95. playerData.HeroListInLeadDatas.Add(heroData2);
  96. playerData.HeroListInLeadDatas.Add(heroData3);
  97. playerData.HeroListInLeadDatas.Add(heroData4);
  98. SavePlayerData();
  99. }
  100. [System.Serializable]
  101. public class HeroData
  102. {
  103. /// <summary>
  104. /// 英雄基础信息
  105. /// </summary>
  106. public int heroModelId;
  107. /// <summary>
  108. /// 英雄等级
  109. /// </summary>
  110. public int heroPowerId;
  111. /// <summary>
  112. /// 星级
  113. /// </summary>
  114. public int heroPromoteId;
  115. }
  116. }