You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.4 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;
public class PlayerData : MonoBehaviour
{
public CardStore CardStore;
public int playerCoins;
public int[] playerCards;//编号为i的卡有几张
public int[] playerDeck;
public TextAsset playerData;
// Start is called before the first frame update
void Start()
{
CardStore.LoadCardData();
LoadPlayerData();
}
// Update is called once per frame
void Update()
{
}
public void LoadPlayerData()
{
playerCards = new int[CardStore.cardList.Count];//因为是数组固定长度,所以提前声明多大
playerDeck = new int[CardStore.cardList.Count];
string[] dataRow = playerData.text.Split('\n');
foreach (var row in dataRow)
{
string[] rowArray = row.Split(",");
if (rowArray[0] == "#")
{
continue;
}
else if (rowArray[0] == "coins")//金币值/法力值...
{
playerCoins = int.Parse(rowArray[1]);
}
else if (rowArray[0] == "cards")
{
int id = int.Parse(rowArray[1]);
int num = int.Parse(rowArray[2]);
//载入玩家数据,即含有的卡牌
playerCards[id] = num;
}
else if(rowArray[0] == "deck")
{
int id = int.Parse(rowArray[1]);
int num = int.Parse(rowArray[2]);
//载入卡组
playerDeck[id] = num;
}
}
}
public void SavePlayerData()
{
string path = Application.dataPath + "/Datas/playerdata.csv";
List<string> datas = new List<string> ();
datas.Add ("coins,"+playerCoins.ToString());
for (int i = 0; i < playerCards.Length; i++)
{
if (playerCards[i] != 0)
{
datas.Add("cards," + i.ToString() + "," + playerCards[i].ToString());
}
}
//卡组
for (int i = 0; i < playerDeck.Length; i++)
{
if (playerDeck[i] != 0)
{
datas.Add("deck," + i.ToString() + "," + playerDeck[i].ToString());
}
}
File.WriteAllLines (path, datas);
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
}
}