Unity将数据保存到csv文件中

游戏中的数据持久化可以使用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]);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u013012420/article/details/106079430