| id | petName | petAvt | description |
| string | string | string | string |
| 1 | 软绵绵 | PetAvt1 | 一只可爱的小绵羊,但是千万不要被它的外表所迷惑 |
| 2 | 鸵鸟-KL | PetAvt2 | 理想城中的机械宠物,能够辅助赛车手进行比赛 |
| 3 | 军火猪 | PetAvt4 | 一只暴躁的虎皮猪,总是横冲直撞的破坏着 |
表格格式固定,第一行数据名 ,第二行数据类型,
第一行和第一列为非重复数据,防止读取冲突
1、选取表格,另存为.csv文件,生成的.csv需要转换为UTF-8格式,不然读取会有问题

2、生成解析csv窗口
Editor/CreatConfigDataFile.cs
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- ///
- /// Excel表格的解析窗口
- ///
- public class CreatConfigDataFile : EditorWindow {
-
- static string writePath = "/Scripts/GameConfigs/";
- static Object selectObj;
-
-
- [MenuItem("framework/打开配置Excel表格的解析窗口")]
- static void ByWindow()
- {
- CreatConfigDataFile window = EditorWindow.GetWindow
(); - }
-
- private void OnGUI()
- {
- GUILayout.Label("设置配置数据文件的生成路径");
- writePath = GUILayout.TextField(writePath);
- GUILayout.Label("请选择一个合法的CSV文件");
-
- if (GUILayout.Button("生成C#协议文件"))
- {
- Debug.Log("生成C#协议文件----------");
- if (selectObj != null)
- {
- CreatConfigUitl.CreatConfigFile(selectObj, writePath);
- }
-
- }
-
- if (Selection.activeObject != null)
- {
- string path = AssetDatabase.GetAssetPath(Selection.activeObject);
- if (path.ToLower().Substring(path.Length - 4, 4) == ".csv")
- {
- selectObj = Selection.activeObject;
- GUILayout.Label(path);
-
- }
- }
- }
-
- private void OnSelectionChange()
- {
- Repaint();
- }
-
-
-
- }

3、读取csv,并自动生成对应数据类
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Text;
- using System.IO;
-
- ///
- /// CSV 读写
- ///
- public class CsvStreamReader
- {
- private ArrayList rowAL; //行链表,CSV文件的每一行就是一个链
- private string fileName; //文件名
-
- private Encoding encoding; //编码
-
- ///
- ///
- ///
- /// 文件名,包括文件路径
- public CsvStreamReader(string fileName)
- {
- this.rowAL = new ArrayList();
- this.fileName = fileName;
- this.encoding = Encoding.Default;
- LoadCsvFile();
- }
-
- ///
- ///
- ///
- /// 文件名,包括文件路径
- /// 文件编码
- public CsvStreamReader(string fileName,Encoding encoding)
- {
- this.rowAL = new ArrayList();
- this.fileName = fileName;
- this.encoding = encoding;
- LoadCsvFile();
- }
-
- ///
- /// 文件名,包括文件路径
- ///
- public string FileName
- {
- set
- {
- this.fileName = value;
- LoadCsvFile();
- }
- }
-
- ///
- /// 文件编码
- ///
-
- public Encoding FileEncoding
- {
- set
- {
- this.encoding = value;
- }
- }
-
- ///
- /// 获取行数
- ///
- public int RowCount
- {
- get
- {
- return this.rowAL.Count;
- }
- }
-
- ///
- /// 获取列数
- ///
- public int ColCount
- {
- get
- {
- int maxCol;
-
- maxCol = 0;
- for (int i = 0;i<this.rowAL.Count;i++)
- {
- ArrayList colAL = (ArrayList) this.rowAL[i]; //rowAL中的某个元素依然是ArrayList类型
-
- maxCol = (maxCol > colAL.Count)?maxCol:colAL.Count; //取最大值
- }
-
- return maxCol;
- }
- }
-
-
- ///
- /// 获取某行某列的数据
-
- /// row:行,row = 1代表第一行
-
- /// col:列,col = 1代表第一列
- ///
- public string this[int row,int col]
- {
- get
- {
- //数据有效性验证
-
- CheckRowValid(row);
- CheckColValid(col);
- ArrayList colAL = (ArrayList) this.rowAL[row-1];
-
- //如果请求列数据大于当前行的列时,返回空值
-
- if (colAL.Count < col)
- {
- return "";
- }
-
- return colAL[col-1].ToString();
- }
- }
-
-
- ///
- /// 根据最小行,最大行,最小列,最大列,来生成一个DataTable类型的数据
-
- /// 行等于1代表第一行
-
- ///
- /// 检查行数是否是有效的
-
- ///
- ///
- private void CheckRowValid(int row)
- {
- if (row <= 0)
- {
- throw new Exception("行数不能小于0");
- }
- if (row > RowCount)
- {
- throw new Exception("没有当前行的数据");
- }
- }
-
- ///
- /// 检查最大行数是否是有效的
-
- ///
- ///
- private void CheckMaxRowValid(int maxRow)
- {
- if (maxRow <= 0 && maxRow != -1)
- {
- throw new Exception("行数不能等于0或小于-1");
- }
- if (maxRow > RowCount)
- {
- throw new Exception("没有当前行的数据");
- }
- }
-
- ///
- /// 检查列数是否是有效的
-
- ///
- ///
- private void CheckColValid(int col)
- {
- if (col <= 0)
- {
- throw new Exception("列数不能小于0");
- }
- if (col > ColCount)
- {
- throw new Exception("没有当前列的数据");
- }
- }
-
- ///
- /// 检查检查最大列数是否是有效的
-
- ///
- ///
- private void CheckMaxColValid(int maxCol)
- {
- if (maxCol <= 0 && maxCol != -1)
- {
- throw new Exception("列数不能等于0或小于-1");
- }
- if (maxCol > ColCount)
- {
- throw new Exception("没有当前列的数据");
- }
- }
-
- ///
- /// 载入CSV文件
- ///
- private void LoadCsvFile()
- {
- //对数据的有效性进行验证
-
- if (this.fileName == null)
- {
- throw new Exception("请指定要载入的CSV文件名");
- }
- else if (!File.Exists(this.fileName))
- {
- throw new Exception("指定的CSV文件不存在");
- }
- else
- {
- }
- if (this.encoding == null)
- {
- this.encoding = Encoding.Default;
- }
-
- StreamReader sr = new StreamReader(this.fileName,this.encoding);
- string csvDataLine;
-
- csvDataLine = "";
- while (true)
- {
- string fileDataLine;
-
- fileDataLine = sr.ReadLine();
- if (fileDataLine == null)
- {
- break;
- }
- if (csvDataLine == "")
- {
- csvDataLine = fileDataLine;//GetDeleteQuotaDataLine(fileDataLine);
- }
- else
- {
- csvDataLine += "/r/n" + fileDataLine;//GetDeleteQuotaDataLine(fileDataLine);
- }
- //如果包含偶数个引号,说明该行数据中出现回车符或包含逗号
- if (!IfOddQuota(csvDataLine))
- {
- AddNewDataLine(csvDataLine);
- csvDataLine = "";
- }
- }
-
- sr.Close();
- //数据行出现奇数个引号
- if (csvDataLine.Length > 0)
- {
- throw new Exception("CSV文件的格式有错误");
- }
- }
-
- ///
- /// 获取两个连续引号变成单个引号的数据行
- ///
- /// 文件数据行
- ///
- private string GetDeleteQuotaDataLine(string fileDataLine)
- {
- return fileDataLine.Replace("\"\"","\"");
- }
-
- ///
- /// 判断字符串是否包含奇数个引号
- ///
- /// 数据行
- ///
为奇数时,返回为真;否则返回为假 - private bool IfOddQuota(string dataLine)
- {
- int quotaCount;
- bool oddQuota;
-
- quotaCount = 0;
- for (int i = 0;i < dataLine.Length;i++)
- {
- if (dataLine[i] == '\"')
- {
- quotaCount++;
- }
- }
-
- oddQuota = false;
- if (quotaCount % 2 == 1)
- {
- oddQuota = true;
- }
-
- return oddQuota;
- }
-
- ///
- /// 判断是否以奇数个引号开始
-
- ///
- ///
- ///
- private bool IfOddStartQuota(string dataCell)
- {
- int quotaCount;
- bool oddQuota;
-
- quotaCount = 0;
- for (int i = 0;i < dataCell.Length;i++)
- {
- if (dataCell[i] == '\"')
- {
- quotaCount++;
- }
- else
- {
- break;
- }
- }
-
- oddQuota = false;
- if (quotaCount % 2 == 1)
- {
- oddQuota = true;
- }
-
- return oddQuota;
- }
-
- ///
- /// 判断是否以奇数个引号结尾
- ///
- ///
- ///
- private bool IfOddEndQuota(string dataCell)
- {
- int quotaCount;
- bool oddQuota;
-
- quotaCount = 0;
- for (int i = dataCell.Length -1;i >= 0;i--)
- {
- if (dataCell[i] == '\"')
- {
- quotaCount++;
- }
- else
- {
- break;
- }
- }
-
- oddQuota = false;
- if (quotaCount % 2 == 1)
- {
- oddQuota = true;
- }
-
- return oddQuota;
- }
-
- ///
- /// 加入新的数据行
-
- ///
- /// 新的数据行
- private void AddNewDataLine(string newDataLine)
- {
- Debug.Log("NewLine:" + newDataLine);
-
- //return;
-
- ArrayList colAL = new ArrayList();
- string[] dataArray = newDataLine.Split(',');
- bool oddStartQuota; //是否以奇数个引号开始
-
- string cellData;
-
- oddStartQuota = false;
- cellData = "";
- for (int i = 0 ;i < dataArray.Length;i++)
- {
- if (oddStartQuota)
- {
- //因为前面用逗号分割,所以要加上逗号
- cellData += "," + dataArray[i];
- //是否以奇数个引号结尾
- if (IfOddEndQuota(dataArray[i]))
- {
- colAL.Add(GetHandleData(cellData));
- oddStartQuota = false;
- continue;
- }
- }
- else
- {
- //是否以奇数个引号开始
-
- if (IfOddStartQuota(dataArray[i]))
- {
- //是否以奇数个引号结尾,不能是一个双引号,并且不是奇数个引号
-
- if (IfOddEndQuota(dataArray[i]) && dataArray[i].Length > 2 && !IfOddQuota(dataArray[i]))
- {
- colAL.Add(GetHandleData(dataArray[i]));
- oddStartQuota = false;
- continue;
- }
- else
- {
-
- oddStartQuota = true;
- cellData = dataArray[i];
- continue;
- }
- }
- else
- {
- colAL.Add(GetHandleData(dataArray[i]));
- }
- }
- }
-
- if (oddStartQuota)
- {
- throw new Exception("数据格式有问题");
- }
- this.rowAL.Add(colAL);
- }
-
-
- ///
- /// 去掉格子的首尾引号,把双引号变成单引号
-
- ///
- ///
- ///
- private string GetHandleData(string fileCellData)
- {
- if (fileCellData == "")
- {
- return "";
- }
- if (IfOddStartQuota(fileCellData))
- {
- if (IfOddEndQuota(fileCellData))
- {
- return fileCellData.Substring(1,fileCellData.Length-2).Replace("\"\"","\""); //去掉首尾引号,然后把双引号变成单引号
- }
- else
- {
- throw new Exception("数据引号无法匹配" + fileCellData);
- }
- }
- else
- {
- //考虑形如"" """" """"""
- if (fileCellData.Length >2 && fileCellData[0] == '\"')
- {
- fileCellData = fileCellData.Substring(1,fileCellData.Length-2).Replace("\"\"","\""); //去掉首尾引号,然后把双引号变成单引号
- }
- }
-
- return fileCellData;
- }
- }
CreatConfigUitl.cs
-
- using UnityEngine;
- using System.IO;
- using UnityEditor;
-
- ///
- /// 自动化生成csv文件对应数据类
- ///
- public class CreatConfigUitl {
- public static void CreatConfigFile(Object selectObj, string writePath)
- {
- string fileName = selectObj.name;
- string className = fileName;
-
- if (Directory.Exists(Application.dataPath + writePath) == false)
- {
- Directory.CreateDirectory(Application.dataPath + writePath);
- }
-
- StreamWriter sw = new StreamWriter(Application.dataPath + writePath + className + ".cs");
-
- sw.WriteLine("using UnityEngine;\nusing System.Collections;\n");
- sw.WriteLine("public partial class " + className + " : GameConfigDataBase");
- sw.WriteLine("{");
-
- string filePath = AssetDatabase.GetAssetPath(selectObj);
- CsvStreamReader csr = new CsvStreamReader(filePath);
- for (int colNum = 1; colNum < csr.ColCount + 1; colNum++)
- {
- string fieldName = csr[1, colNum];
- string fieldType = csr[2, colNum];
- sw.WriteLine("\t" + "public " + fieldType + " " + fieldName + ";" + "");
- }
- sw.WriteLine("\t" + "protected override string getFilePath ()");
- sw.WriteLine("\t" + "{");
- // filePath=filePath.Replace("Assets/Resources/","");
- // filePath=filePath.Substring(0,filePath.LastIndexOf('.'));
- sw.WriteLine("\t\t" + "return " + "\"" + fileName + "\";");
- sw.WriteLine("\t" + "}");
- sw.WriteLine("}");
-
- sw.Flush();
- sw.Close();
- AssetDatabase.Refresh(); //这里是一个点
- }
- }


CsvStreamReader.cs 游戏内数据读取解析
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Text;
- using System.IO;
-
- ///
- /// CSV 读写
- ///
- public class CsvStreamReader
- {
- private ArrayList rowAL; //行链表,CSV文件的每一行就是一个链
- private string fileName; //文件名
-
- private Encoding encoding; //编码
-
- ///
- ///
- ///
- /// 文件名,包括文件路径
- public CsvStreamReader(string fileName)
- {
- this.rowAL = new ArrayList();
- this.fileName = fileName;
- this.encoding = Encoding.Default;
- LoadCsvFile();
- }
-
- ///
- ///
- ///
- /// 文件名,包括文件路径
- /// 文件编码
- public CsvStreamReader(string fileName,Encoding encoding)
- {
- this.rowAL = new ArrayList();
- this.fileName = fileName;
- this.encoding = encoding;
- LoadCsvFile();
- }
-
- ///
- /// 文件名,包括文件路径
- ///
- public string FileName
- {
- set
- {
- this.fileName = value;
- LoadCsvFile();
- }
- }
-
- ///
- /// 文件编码
- ///
-
- public Encoding FileEncoding
- {
- set
- {
- this.encoding = value;
- }
- }
-
- ///
- /// 获取行数
- ///
- public int RowCount
- {
- get
- {
- return this.rowAL.Count;
- }
- }
-
- ///
- /// 获取列数
- ///
- public int ColCount
- {
- get
- {
- int maxCol;
-
- maxCol = 0;
- for (int i = 0;i<this.rowAL.Count;i++)
- {
- ArrayList colAL = (ArrayList) this.rowAL[i]; //rowAL中的某个元素依然是ArrayList类型
-
- maxCol = (maxCol > colAL.Count)?maxCol:colAL.Count; //取最大值
- }
-
- return maxCol;
- }
- }
-
-
- ///
- /// 获取某行某列的数据
-
- /// row:行,row = 1代表第一行
-
- /// col:列,col = 1代表第一列
- ///
- public string this[int row,int col]
- {
- get
- {
- //数据有效性验证
-
- CheckRowValid(row);
- CheckColValid(col);
- ArrayList colAL = (ArrayList) this.rowAL[row-1];
-
- //如果请求列数据大于当前行的列时,返回空值
-
- if (colAL.Count < col)
- {
- return "";
- }
-
- return colAL[col-1].ToString();
- }
- }
-
-
- ///
- /// 根据最小行,最大行,最小列,最大列,来生成一个DataTable类型的数据
-
- /// 行等于1代表第一行
-
- ///
- /// 检查行数是否是有效的
-
- ///
- ///
- private void CheckRowValid(int row)
- {
- if (row <= 0)
- {
- throw new Exception("行数不能小于0");
- }
- if (row > RowCount)
- {
- throw new Exception("没有当前行的数据");
- }
- }
-
- ///
- /// 检查最大行数是否是有效的
-
- ///
- ///
- private void CheckMaxRowValid(int maxRow)
- {
- if (maxRow <= 0 && maxRow != -1)
- {
- throw new Exception("行数不能等于0或小于-1");
- }
- if (maxRow > RowCount)
- {
- throw new Exception("没有当前行的数据");
- }
- }
-
- ///
- /// 检查列数是否是有效的
-
- ///
- ///
- private void CheckColValid(int col)
- {
- if (col <= 0)
- {
- throw new Exception("列数不能小于0");
- }
- if (col > ColCount)
- {
- throw new Exception("没有当前列的数据");
- }
- }
-
- ///
- /// 检查检查最大列数是否是有效的
-
- ///
- ///
- private void CheckMaxColValid(int maxCol)
- {
- if (maxCol <= 0 && maxCol != -1)
- {
- throw new Exception("列数不能等于0或小于-1");
- }
- if (maxCol > ColCount)
- {
- throw new Exception("没有当前列的数据");
- }
- }
-
- ///
- /// 载入CSV文件
- ///
- private void LoadCsvFile()
- {
- //对数据的有效性进行验证
-
- if (this.fileName == null)
- {
- throw new Exception("请指定要载入的CSV文件名");
- }
- else if (!File.Exists(this.fileName))
- {
- throw new Exception("指定的CSV文件不存在");
- }
- else
- {
- }
- if (this.encoding == null)
- {
- this.encoding = Encoding.Default;
- }
-
- StreamReader sr = new StreamReader(this.fileName,this.encoding);
- string csvDataLine;
-
- csvDataLine = "";
- while (true)
- {
- string fileDataLine;
-
- fileDataLine = sr.ReadLine();
- if (fileDataLine == null)
- {
- break;
- }
- if (csvDataLine == "")
- {
- csvDataLine = fileDataLine;//GetDeleteQuotaDataLine(fileDataLine);
- }
- else
- {
- csvDataLine += "/r/n" + fileDataLine;//GetDeleteQuotaDataLine(fileDataLine);
- }
- //如果包含偶数个引号,说明该行数据中出现回车符或包含逗号
- if (!IfOddQuota(csvDataLine))
- {
- AddNewDataLine(csvDataLine);
- csvDataLine = "";
- }
- }
-
- sr.Close();
- //数据行出现奇数个引号
- if (csvDataLine.Length > 0)
- {
- throw new Exception("CSV文件的格式有错误");
- }
- }
-
- ///
- /// 获取两个连续引号变成单个引号的数据行
- ///
- /// 文件数据行
- ///
- private string GetDeleteQuotaDataLine(string fileDataLine)
- {
- return fileDataLine.Replace("\"\"","\"");
- }
-
- ///
- /// 判断字符串是否包含奇数个引号
- ///
- /// 数据行
- ///
为奇数时,返回为真;否则返回为假 - private bool IfOddQuota(string dataLine)
- {
- int quotaCount;
- bool oddQuota;
-
- quotaCount = 0;
- for (int i = 0;i < dataLine.Length;i++)
- {
- if (dataLine[i] == '\"')
- {
- quotaCount++;
- }
- }
-
- oddQuota = false;
- if (quotaCount % 2 == 1)
- {
- oddQuota = true;
- }
-
- return oddQuota;
- }
-
- ///
- /// 判断是否以奇数个引号开始
-
- ///
- ///
- ///
- private bool IfOddStartQuota(string dataCell)
- {
- int quotaCount;
- bool oddQuota;
-
- quotaCount = 0;
- for (int i = 0;i < dataCell.Length;i++)
- {
- if (dataCell[i] == '\"')
- {
- quotaCount++;
- }
- else
- {
- break;
- }
- }
-
- oddQuota = false;
- if (quotaCount % 2 == 1)
- {
- oddQuota = true;
- }
-
- return oddQuota;
- }
-
- ///
- /// 判断是否以奇数个引号结尾
- ///
- ///
- ///
- private bool IfOddEndQuota(string dataCell)
- {
- int quotaCount;
- bool oddQuota;
-
- quotaCount = 0;
- for (int i = dataCell.Length -1;i >= 0;i--)
- {
- if (dataCell[i] == '\"')
- {
- quotaCount++;
- }
- else
- {
- break;
- }
- }
-
- oddQuota = false;
- if (quotaCount % 2 == 1)
- {
- oddQuota = true;
- }
-
- return oddQuota;
- }
-
- ///
- /// 加入新的数据行
-
- ///
- /// 新的数据行
- private void AddNewDataLine(string newDataLine)
- {
- Debug.Log("NewLine:" + newDataLine);
-
- //return;
-
- ArrayList colAL = new ArrayList();
- string[] dataArray = newDataLine.Split(',');
- bool oddStartQuota; //是否以奇数个引号开始
-
- string cellData;
-
- oddStartQuota = false;
- cellData = "";
- for (int i = 0 ;i < dataArray.Length;i++)
- {
- if (oddStartQuota)
- {
- //因为前面用逗号分割,所以要加上逗号
- cellData += "," + dataArray[i];
- //是否以奇数个引号结尾
- if (IfOddEndQuota(dataArray[i]))
- {
- colAL.Add(GetHandleData(cellData));
- oddStartQuota = false;
- continue;
- }
- }
- else
- {
- //是否以奇数个引号开始
-
- if (IfOddStartQuota(dataArray[i]))
- {
- //是否以奇数个引号结尾,不能是一个双引号,并且不是奇数个引号
-
- if (IfOddEndQuota(dataArray[i]) && dataArray[i].Length > 2 && !IfOddQuota(dataArray[i]))
- {
- colAL.Add(GetHandleData(dataArray[i]));
- oddStartQuota = false;
- continue;
- }
- else
- {
-
- oddStartQuota = true;
- cellData = dataArray[i];
- continue;
- }
- }
- else
- {
- colAL.Add(GetHandleData(dataArray[i]));
- }
- }
- }
-
- if (oddStartQuota)
- {
- throw new Exception("数据格式有问题");
- }
- this.rowAL.Add(colAL);
- }
-
-
- ///
- /// 去掉格子的首尾引号,把双引号变成单引号
-
- ///
- ///
- ///
- private string GetHandleData(string fileCellData)
- {
- if (fileCellData == "")
- {
- return "";
- }
- if (IfOddStartQuota(fileCellData))
- {
- if (IfOddEndQuota(fileCellData))
- {
- return fileCellData.Substring(1,fileCellData.Length-2).Replace("\"\"","\""); //去掉首尾引号,然后把双引号变成单引号
- }
- else
- {
- throw new Exception("数据引号无法匹配" + fileCellData);
- }
- }
- else
- {
- //考虑形如"" """" """"""
- if (fileCellData.Length >2 && fileCellData[0] == '\"')
- {
- fileCellData = fileCellData.Substring(1,fileCellData.Length-2).Replace("\"\"","\""); //去掉首尾引号,然后把双引号变成单引号
- }
- }
-
- return fileCellData;
- }
- }
GameConfigDataBase.cs 通用数据读取管理模板
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using System.Reflection;
-
- ///
- /// csv 通用数据读取管理模板
- ///
- public class GameConfigDataBase
- {
- protected virtual string getFilePath ()
- {
- return "";
- }
- //PetConfigData
- static Dictionary
string,GameConfigDataBase>> dataDic = new Dictionarystring, GameConfigDataBase>> (); -
- public static T GetConfigData<T> (object key) where T:GameConfigDataBase
- {
- Type setT = typeof(T);
- if (!dataDic.ContainsKey (setT)) {
- ReadConfigData
(); - }
- Dictionary<string,GameConfigDataBase> objDic = dataDic [setT];
- Debug.Log("test (" + key + ")" + objDic.Count);
- if (!objDic.ContainsKey (key.ToString())) {
- throw new Exception ("no this config");
- }
- return (T)(objDic [key.ToString()]);
- }
-
- public static List<T> GetConfigDatas<T> () where T:GameConfigDataBase
- {
- List
returnList = new List (); - Type setT = typeof(T);
- if (!dataDic.ContainsKey (setT)) {
- ReadConfigData
(); - }
- Dictionary<string,GameConfigDataBase> objDic = dataDic [setT];
- foreach (KeyValuePair<string,GameConfigDataBase> kvp in objDic) {
- returnList.Add ((T)(kvp.Value));
- }
- return returnList;
- }
-
- static void ReadConfigData<T> () where T:GameConfigDataBase
- {
- T obj = Activator.CreateInstance
(); - string fileName = obj.getFilePath ();
-
- string getString = Resources.Load
("GameConfig/" + fileName).text; -
- CsvReaderByString csr = new CsvReaderByString (getString);
-
- Dictionary<string,GameConfigDataBase> objDic = new Dictionary<string, GameConfigDataBase> ();
-
- FieldInfo[] fis = new FieldInfo[csr.ColCount];
- for (int colNum=1; colNum
1; colNum++) { - fis [colNum - 1] = typeof(T).GetField (csr [1, colNum]);
- }
-
- for (int rowNum=3; rowNum
1; rowNum++) { - T configObj = Activator.CreateInstance
(); - for (int i=0; i
- string fieldValue = csr [rowNum, i + 1];
- object setValue = new object ();
- switch (fis [i].FieldType.ToString ()) {
- case "System.Int32":
- setValue = int.Parse (fieldValue);
- break;
- case "System.Int64":
- setValue = long.Parse (fieldValue);
- break;
- case "System.String":
- setValue = fieldValue;
- break;
- default:
- Debug.Log ("error data type");
- break;
- }
- fis [i].SetValue (configObj, setValue);
- if (fis [i].Name == "key" || fis [i].Name == "id") {
- //只检测key和id的值,然后添加到objDic 中
- objDic.Add (setValue.ToString (), configObj);
- }
- }
- }
- dataDic.Add (typeof(T), objDic); //可以作为参数
- }
- }
读取示例
//数组读取
var level_configs = LevelConfig.GetConfigDatas();
//单条读取
var pet = LevelConfig.GetConfigData(1);
var pet2 = LevelConfig.GetConfigData("1");