如何在unity内使用StreamWriter类将Dictionary<int, CSV>存储为CSV?
时间: 2023-03-26 07:02:43 浏览: 200
可以使用StreamWriter类的WriteLine方法将Dictionary<int, CSV>中的每个键值对转换为CSV格式的字符串,然后写入CSV文件中。具体实现可以参考以下代码:
```csharp
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class CSV
{
public string name;
public int age;
public float score;
public CSV(string name, int age, float score)
{
this.name = name;
this.age = age;
this.score = score;
}
public override string ToString()
{
return string.Format("{},{1},{2}", name, age, score);
}
}
public class CSVWriter : MonoBehaviour
{
private Dictionary<int, CSV> data = new Dictionary<int, CSV>();
private void Start()
{
// 添加测试数据
data.Add(1, new CSV("张三", 18, 90.5f));
data.Add(2, new CSV("李四", 20, 85.f));
data.Add(3, new CSV("王五", 22, 95.5f));
// 写入CSV文件
using (StreamWriter writer = new StreamWriter("test.csv"))
{
foreach (KeyValuePair<int, CSV> pair in data)
{
writer.WriteLine(pair.Value.ToString());
}
}
}
}
```
以上代码将Dictionary<int, CSV>中的每个CSV对象转换为CSV格式的字符串,然后写入名为"test.csv"的文件中。
阅读全文
相关推荐


















