
1.读取代码
string filePath = Application.streamingAssetsPath + "\\data.csv";
public List<MovieData> movieData = new List<MovieData>();
private void ReadCSV(string filePath)
{
List<List<string>> data = new List<List<string>>();
if (File.Exists(filePath))
{
using (StreamReader reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
data.Add(new List<string>(values));
}
}
}
else
{
Debug.LogError("File not found: " + filePath);
}
ProcessData(data);
}
private void ProcessData(List<List<string>> data)
{
foreach (var row in data)
{
MovieData dataMovie = new MovieData();
dataMovie.Type = row[0] + row[1] + row[2] + row[3];
dataMovie.Number = int.Parse(row[4]); //确保列的数据类型正确,这里假定第4列是整数类型
movieData.Add(dataMovie);
//Debug.Log(JsonUtility.ToJson(dataMovie));
}
}
2.数据结构
Serializable
public class MovieData
{
public string Type;
public int Number;
}