|
|
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.IO;
- using System.Text;
-
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class CSVOptionTool
- {
- //csv文件写入
- public static void WriteCsv(string[] strs, string path)
- {
- if (!File.Exists(path))
- {
- File.Create(path).Dispose();
- }
- using (StreamWriter stream = new StreamWriter(path, false, Encoding.UTF8))
- {
- for (int i = 0; i < strs.Length; i++)
- {
- if (strs[i] != null)
- stream.WriteLine(strs[i]);
- }
- }
- }
-
- /// <summary>
- /// 读取CSV文件
- /// 结果保存到字典集合,以ID作为Key值,对应每一行的数据,每一行的数据也用字典集合保存。
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static Dictionary<string, Dictionary<string, string>> LoadCsvFile(string filePath)
- {
- Dictionary<string, Dictionary<string, string>> result = new Dictionary<string, Dictionary<string, string>>();
- string[] fileData = File.ReadAllLines(filePath);
- /* CSV文件的第一行为Key字段,第二行开始是数据。第一个字段一定是ID。 */
- string[] keys = fileData[0].Split(',');
- for (int i = 1; i < fileData.Length; i++)
- {
- string[] line = fileData[i].Split(',');
- /* 以ID为key值,创建一个新的集合,用于保存当前行的数据 */
- string ID = line[0];
- result[ID] = new Dictionary<string, string>();
- for (int j = 0; j < line.Length; j++)
- {
- /* 每一行的数据存储规则:Key字段-Value值 */
- result[ID][keys[j]] = line[j];
- }
- }
- return result;
- }
-
- /// <summary>
- /// 读取CSV文件数据(利用反射)
- /// </summary>
- /// <typeparam name="CsvData">CSV数据对象的类型</typeparam>
- /// <param name="csvFilePath">CSV文件路径</param>
- /// <param name="csvDatas">用于缓存数据的字典</param>
- /// <returns>CSV文件所有行内容的数据对象</returns>
- public static Dictionary<int, SaveWaitQueueCsvData> LoadCsvData<SaveWaitQueueCsvData>(string csvFilePath)
- {
- Dictionary<int, SaveWaitQueueCsvData> dic = new Dictionary<int, SaveWaitQueueCsvData>();
- /* 从CSV文件读取数据 */
- Dictionary<string, Dictionary<string, string>> result = LoadCsvFile(csvFilePath);
- /* 遍历每一行数据 */
- foreach (string ID in result.Keys)
- {
- /* CSV的一行数据 */
- Dictionary<string, string> datas = result[ID];
- /* 读取Csv数据对象的属性 */
- PropertyInfo[] props = typeof(SaveWaitQueueCsvData).GetProperties();
- /* 使用反射,将CSV文件的数据赋值给CSV数据对象的相应字段,要求CSV文件的字段名和CSV数据对象的字段名完全相同 */
- SaveWaitQueueCsvData obj = Activator.CreateInstance<SaveWaitQueueCsvData>();
- foreach (PropertyInfo pi in props)
- {
- pi.SetValue(obj, Convert.ChangeType(datas[pi.Name], pi.PropertyType), null);
- }
- /* 按ID-数据的形式存储 */
- dic[Convert.ToInt32(ID)] = obj;
- }
- return dic;
- }
- }
-
- /// <summary>
- /// 对待检测的特效列表临时保存
- /// </summary>
- public class SaveWaitQueueCsvData:CSVOptionTool
- {
- public int ID { get; set; }
- public string Name { get; set; }
- }
-
- /// <summary>
- /// 检测结果保存
- /// </summary>
- public class SaveResultCsvData:CSVOptionTool
- {
- public string Name { get; set; }//特效名
- public float PictureMemory { get; set; }//贴图所占用的内存
- public long PictureNum { get; set; }//贴图数量
- public long GrainCompNum { get; set; }//所有粒子系统组件数量
- public int DrawCallNum { get; set; }//DrawCall
- public long GrainNum { get; set; }//粒子数量
- public string ExtraDes { get; set; }//额外描述
- // public float PixelNum1 { get; set; }//特效原填充像素点
- // public float PixelNum2 { get; set; }//特效实际填充像素点
- // public float PixelOverdraw { get; set; }//平均每像素overdraw率
- }
|