using System.Collections; using System.Collections.Generic; using System.IO; using Core.Utility; using Fort23.UTool; using GameLogic.Bag; using GameLogic.Hero; using UnityEngine; using Utility; using WeChatWASM; public class AccountFileInfo : Singleton { public string persistentDataPath = Application.persistentDataPath + "/playerData.txt"; public PlayerData playerData = new PlayerData(); public string fileName = "/playerData.txt"; [System.Serializable] public class PlayerData { public List ItemListData = new List(); /// /// 英雄数据 /// public List HeroListData = new List(); /// /// 装备列表 /// // public List EqListData = new List(); /// /// 关卡进度 /// public int levelBattle = 1; /// /// 是否全部阵亡一次 /// public bool isAllHeroDie; /// /// 装备的GUID /// public long eqGUID = 0; // /// // /// 后备英雄(共享等级) // /// // public List HeroListInBackDatas = new List(); } public void LoadPlayerData() { #if UNITY_WEIXINMINIGAME && !UNITY_EDITOR persistentDataPath = WX.env.USER_DATA_PATH + fileName; WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager(); if (wxFileSystemManager.AccessSync(persistentDataPath).Equals("access:ok")) { string data = wxFileSystemManager.ReadFileSync(persistentDataPath, "utf8"); playerData = new PlayerData(); JsonUtility.FromJsonOverwrite(data, playerData); } else { ClearInitPlayerData(); SavePlayerData(); } #else if (!File.Exists(persistentDataPath)) { LogTool.Log("没有文件: " + persistentDataPath); ClearInitPlayerData(); // File.Create(persistentDataPath).Close(); } LogTool.Log("读取=文件: " + persistentDataPath); StreamReader sr = File.OpenText(persistentDataPath); string data = sr.ReadToEnd(); sr.Close(); playerData = new PlayerData(); JsonUtility.FromJsonOverwrite(data, playerData); #endif } private int lastHeroIdx = 0; /// /// 保存英雄数据 /// /// public void SaveHeroData(HeroInfo heroInfo) { var lastHeroData = playerData.HeroListData[lastHeroIdx]; if (heroInfo.modelID == lastHeroData.heroModelId) { playerData.HeroListData[lastHeroIdx] = heroInfo.ToHeroData(); SavePlayerData(); return; } for (int i = 0; i < playerData.HeroListData.Count; i++) { HeroData heroData = playerData.HeroListData[i]; if (heroData.heroModelId == heroInfo.modelID) { playerData.HeroListData[i] = heroInfo.ToHeroData(); //存下来,用于快速查找 lastHeroIdx = i; SavePlayerData(); return; } } } private int lastItemIdx = 0; /// /// 保存item数据 /// /// public void SaveItemData(ItemInfo itemInfo) { if (itemInfo.guid == playerData.ItemListData[lastItemIdx].guid) { playerData.ItemListData[lastItemIdx] = itemInfo.ToItemData(); SavePlayerData(); return; } for (int i = 0; i < playerData.ItemListData.Count; i++) { ItemData itemData = playerData.ItemListData[i]; if (itemData.guid == itemInfo.guid) { playerData.ItemListData[i] = itemInfo.ToItemData(); //存下来,用于快速查找 lastItemIdx = i; SavePlayerData(); return; } } playerData.ItemListData.Add(itemInfo.ToItemData()); SavePlayerData(); } public void SaveEqGUID() { SavePlayerData(); } public void SavePlayerData() { if (!string.IsNullOrEmpty(persistentDataPath)) { string playerSettingJson = JsonManager.ToJson(playerData); #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager(); wxFileSystemManager.WriteFileSync(persistentDataPath, playerSettingJson, "utf8"); #else File.WriteAllText(persistentDataPath, playerSettingJson); #endif } } public void DeleteFile(string filePath) { #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR playerData = new PlayerData(); SavePlayerData(); ClearInitPlayerData(); #else if (File.Exists(filePath)) { File.Delete(filePath); // 删除文件 LogTool.Log($"文件已删除:{filePath}"); } else { LogTool.Log($"文件不存在:{filePath}"); } #endif } /// /// 不要服务器的话,这里初始化玩家的起始数据 /// public void ClearInitPlayerData() { ItemData coin = new ItemData(GlobalParam.Item_Coin_ID); ItemData diamond = new ItemData(GlobalParam.Item_Diamond_ID); ItemData heroExp = new ItemData(GlobalParam.Item_HeroExp_ID); playerData.ItemListData.Add(coin); playerData.ItemListData.Add(diamond); playerData.ItemListData.Add(heroExp); HeroData heroData1 = new HeroData { heroModelId = 105, heroPowerId = 1, heroPromoteId = 3, isLead = true, }; HeroData heroData2 = new HeroData { heroModelId = 107, heroPowerId = 1, heroPromoteId = 3, isLead = true, }; HeroData heroData3 = new HeroData { heroModelId = 116, heroPowerId = 1, heroPromoteId = 3, isLead = true, }; HeroData heroData4 = new HeroData { heroModelId = 113, heroPowerId = 1, heroPromoteId = 3, isLead = true, }; playerData.HeroListData.Add(heroData1); playerData.HeroListData.Add(heroData2); playerData.HeroListData.Add(heroData3); playerData.HeroListData.Add(heroData4); SavePlayerData(); } [System.Serializable] public class HeroData { /// /// 英雄基础信息 /// public int heroModelId; /// /// 英雄等级 /// public int heroPowerId; /// /// 星级 /// public int heroPromoteId; /// /// 是否为主力 /// public bool isLead; } [System.Serializable] public class ItemData { public int itemId; public long itemCount; public string guid; public EqData eqData; public ItemData(int itemId, long itemCount = 0, string guid = "") { this.itemId = itemId; this.itemCount = itemCount; if (string.IsNullOrEmpty(guid)) { this.guid = itemId.ToString(); } else { this.guid = guid; } // eqData = null; } } /// /// 装备数据 /// [System.Serializable] public class EqData { // public string guid; // public int count; public int itemConfigID; public int level; public int quality; /// /// 穿在谁身上 /// public int ownerID; } }