using UnityEngine;
using System.Collections;
using System.IO;
public class CSVFile {
public string [][]Array;
public string GetDataByRowAndCol(int nRow, int nCol)
//通过 行号和列号 获得值
{
if (Array.Length <= 0 || nRow >= Array.Length)
return "";
if (nCol >= Array[0].Length)
return "";
return Array[nRow][nCol];
}
public string GetDataByIdAndName(int nId, string strName)
//通过id 和 字符名 获得值
{
if (Array.Length <= 0)
return "";
int nRow = Array.Length;
int nCol = Array[0].Length;
for (int i = 1; i < nRow; ++i) {
//string strId = string.Format("\n{0}", nId);
string strId = string.Format("{0}", nId);
if (Array[i][0] == strId) {
for (int j = 0; j < nCol; ++j) {
if (Array[0][j] == strName) {
return Array[i][j];
}
}
}
}
return "";
}
public string GetDataByRowandName(int x,string strName) //
依次返回该字符串名下的所有值
{
int curcol = -1 ;
if (Array.Length <= 0)
return "";
int nRow = Array.Length;
int nCol = Array[0].Length;
for (int j = 0; j < nCol; j++)
{
if (Array[0][j] == strName)
{
curcol =j;
}
}
for (int i = x; i < nRow; i++)
{
return Array[i][curcol];
}
return "";
}
public string GetDataByStringIdAndName(string nId, string strName)
//通过 id 和字符串名 返回数据
{
if (Array.Length <= 0)
return "";
int nRow = Array.Length;
int nCol = Array[0].Length;
for (int i = 1; i < nRow; ++i) {
//string strId = string.Format("\n{0}", nId);
string strId = string.Format(nId);
if (Array[i][0] == strId) {
for (int j = 0; j < nCol; ++j) {
if (Array[0][j] == strName) {
return Array[i][j];
}
}
}
}
return "";
}
public int GetDataByIdAndNameToInt(int nId, string strName){
return int.Parse (GetDataByIdAndName (nId, strName));
}
public float GetDataByIdAndNameToFloat(int nId, string strName){
return float.Parse (GetDataByIdAndName (nId, strName));
}
}
public class CVS {
//单例
private static CVS _instance;
public static CVS Instance
{
get{
if (_instance == null) {
_instance = new CVS();
}
return _instance;
}
}
private Hashtable files = new Hashtable();
public CSVFile Read (string fileName)
//输出csv文件的名字
{
CSVFile file = (CSVFile)files[fileName];
if (file != null)
return file;
file = new CSVFile ();
string filePath =Application.dataPath +"/Resources/"+ fileName+".csv";
Debug.Log("filePath--------------------------------->>"+ filePath);
if(!File.Exists(filePath)){
filePath = Application.dataPath + "/Resources/config/" + fileName + ".csv";
}
string text = File.ReadAllText(filePath);
/*
FileStream fs1 = new FileStream( filePath,FileMode.Open);
StreamReader sw = new StreamReader(fs1);
string text = sw.ReadToEnd();
sw.Close();
sw.Dispose();
*/
//读取csv二进制文件
/*
TextAsset binAsset = Resources.Load (fileName, typeof(TextAsset)) as TextAsset;
string text = binAsset.text;
*/
//读取每一行的内容
//string [] lineArray = binAsset.text.Split ("\r"[0]);
string [] lineArray = text.Trim().Replace(",",";").Replace("\"","").Split ("\n"[0]);
//创建二维数组
file.Array = new string [lineArray.Length][];
//把csv中的数据储存在二位数组中
for(int i =0;i < lineArray.Length; i++)
{
//Array[i] = lineArray[i].Split (',');
//lineArray[i] = lineArray[i].Trim();
file.Array[i] = lineArray[i].Trim().Split (';');
}
files [fileName] = file;
return file;
}
public CSVFile getFile(string fileName ="config/role"){
return Read (fileName);
}
// Use this for initialization
void Start () {
//测试 将 MyText.csv文件导入到正确路径filePath
//调用上面写好的方法,也可以根据需求自己添加方法;
Read ("MyText");
Debug.Log (Read ("Text").GetDataByIdAndName (1, "name"));
//根据id和字符串名字获取值
//以下为MyText.csv的内容
id,name,level,attack,del,type
1,多蓝盾,10,0,0,2
2,多兰剑,20,10,5,2
3,多兰戒,30,5,10,2
}
本文介绍了一个用于Unity项目的CSV文件读取类,该类提供了多种方法来获取CSV文件中的数据,包括按行列索引、ID和名称等进行数据检索。

1166





