遊戲中的數據持久化可以使用csv來保存。
csv是一種以逗號爲分隔符的文本文件,可以直接用excel打開,或者轉換成excel,並且使用excel的操作。
所以,在c#中可以使用流寫入,將需要寫入的數據用逗號拼接好。
路徑方面,一般使用Application.persistentDataPath,方便查看文件。關於文件存儲路徑的說明:Unity資源存放與加載-本地資源 更新資源 存放路徑與注意事項
public void WriteCsv(string[] strs, string path)
{
if (!File.Exists(path))
{
File.Create(path).Dispose();
}
//UTF-8方式保存
using (StreamWriter stream = new StreamWriter(path, false, Encoding.UTF8))
{
for (int i = 0; i < strs.Length; i++)
{
if (strs[i] != null)
stream.WriteLine(strs[i]);
}
}
}