直接上代码
Part1:
using UnityEngine;
using System.IO;
using System.Collections.Generic; public class CSV
{
static CSV csv;
public List<string[]> m_ArrayData;
public static CSV GetInstance()
{
if (csv == null)
{
csv = new CSV();
}
return csv;
}
private CSV() { m_ArrayData = new List<string[]>(); }
public string GetString(int row, int col)
{
return m_ArrayData[row][col];
}
public int GetInt(int row, int col)
{
return int.Parse(m_ArrayData[row][col]);
}
public void LoadFile(string path, string fileName)
{
m_ArrayData.Clear();
StreamReader sr = null;
try
{
sr = File.OpenText(path + "//" + fileName);
Debug.Log("file finded!");
}
catch
{
Debug.Log("file don't finded!");
return;
}
string line;
while ((line = sr.ReadLine()) != null)
{
m_ArrayData.Add(line.Split(','));
}
sr.Close();
sr.Dispose();
}
}
Part2:
using UnityEngine; public class FileController : MonoBehaviour
{ // Use this for initialization
void Start ()
{
CSV.GetInstance().LoadFile(Application.dataPath + "/Res", "myTest.csv"); Debug.Log("GetString : " + CSV.GetInstance().GetString(, ));
Debug.Log("GetInt : " + CSV.GetInstance().GetString(, ));
}
}
补充
关于路径有4个类型:
Application.dataPath:该路径指向我们Unity编辑器的Asset文件夹
Application.persistentDataPath:该路径指向iOS和Android的沙盒路径
Application.streamingAssetsPath:streamingAsset文件夹路径,在任何平台都可以通过这个路径读取到文件夹里的内容
Application.temporaryCachePath:临时数据文件路径