123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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;
- public class AccountFileInfo : Singleton<AccountFileInfo>
- {
- public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
- public PlayerData playerData = new PlayerData();
- [System.Serializable]
- public class PlayerData
- {
- public List<ItemData> ItemListData = new List<ItemData>();
- /// <summary>
- /// 英雄数据
- /// </summary>
- public List<HeroData> HeroListData = new List<HeroData>();
- /// <summary>
- /// 关卡进度
- /// </summary>
- public int levelBattle = 1;
- // /// <summary>
- // /// 后备英雄(共享等级)
- // /// </summary>
- // public List<HeroData> HeroListInBackDatas = new List<HeroData>();
- }
- public void LoadPlayerData()
- {
- // #if UNITY_EDITOR || UNITY_STANDALONE_WIN
- // persistentDataPath = Application.streamingAssetsPath + $"/{playerId}playerSetting.txt";
- // #elif UNITY_ANDROID
- // playerSettingPath = Application.persistentDataPath+$"/{playerId}playerSetting.txt";
- // #elif UNITY_IPHONE
- // playerSettingPath = Application.persistentDataPath+$"/{playerId}playerSetting.txt";
- // #endif
- 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);
- }
- private int lastHeroIdx = 0;
- /// <summary>
- /// 保存英雄数据
- /// </summary>
- /// <param name="heroInfo"></param>
- 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;
- /// <summary>
- /// 保存item数据
- /// </summary>
- /// <param name="itemInfo"></param>
- public void SaveItemData(ItemInfo itemInfo)
- {
- if (itemInfo.ID == playerData.ItemListData[lastItemIdx].itemId)
- {
- playerData.ItemListData[lastItemIdx] = itemInfo.ToItemData();
- SavePlayerData();
- return;
- }
- for (int i = 0; i < playerData.ItemListData.Count; i++)
- {
- ItemData itemData = playerData.ItemListData[i];
- if (itemData.itemId == itemInfo.ID)
- {
- playerData.ItemListData[i] = itemInfo.ToItemData();
- //存下来,用于快速查找
- lastItemIdx = i;
- SavePlayerData();
- return;
- }
- }
- }
- public void SavePlayerData()
- {
- if (!string.IsNullOrEmpty(persistentDataPath))
- {
- string playerSettingJson = JsonManager.ToJson(playerData);
- File.WriteAllText(persistentDataPath, playerSettingJson);
- }
- }
- public void DeleteFile(string filePath)
- {
- if (File.Exists(filePath))
- {
- File.Delete(filePath); // 删除文件
- LogTool.Log($"文件已删除:{filePath}");
- }
- else
- {
- LogTool.Log($"文件不存在:{filePath}");
- }
- }
- /// <summary>
- /// 不要服务器的话,这里初始化玩家的起始数据
- /// </summary>
- public void ClearInitPlayerData()
- {
- ItemData coin = new ItemData()
- {
- itemId = GlobalParam.Item_Coin_ID,
- itemCount = 100,
- };
- ItemData diamond = new ItemData()
- {
- itemId = GlobalParam.Item_Diamond_ID,
- itemCount = 1000,
- };
- ItemData heroExp = new ItemData()
- {
- itemId = GlobalParam.Item_HeroExp_ID,
- itemCount = 100000,
- };
- 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
- {
- /// <summary>
- /// 英雄基础信息
- /// </summary>
- public int heroModelId;
- /// <summary>
- /// 英雄等级
- /// </summary>
- public int heroPowerId;
- /// <summary>
- /// 星级
- /// </summary>
- public int heroPromoteId;
- /// <summary>
- /// 是否为主力
- /// </summary>
- public bool isLead;
- }
- [System.Serializable]
- public class ItemData
- {
- public int itemId;
- public long itemCount;
- public int guid;
- }
- }
|