源战役客户端
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

112 linhas
4.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.IO;
  5. using System.Text;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. public class CSVOptionTool
  10. {
  11. //csv文件写入
  12. public static void WriteCsv(string[] strs, string path)
  13. {
  14. if (!File.Exists(path))
  15. {
  16. File.Create(path).Dispose();
  17. }
  18. using (StreamWriter stream = new StreamWriter(path, false, Encoding.UTF8))
  19. {
  20. for (int i = 0; i < strs.Length; i++)
  21. {
  22. if (strs[i] != null)
  23. stream.WriteLine(strs[i]);
  24. }
  25. }
  26. }
  27. /// <summary>
  28. /// 读取CSV文件
  29. /// 结果保存到字典集合,以ID作为Key值,对应每一行的数据,每一行的数据也用字典集合保存。
  30. /// </summary>
  31. /// <param name="filePath"></param>
  32. /// <returns></returns>
  33. public static Dictionary<string, Dictionary<string, string>> LoadCsvFile(string filePath)
  34. {
  35. Dictionary<string, Dictionary<string, string>> result = new Dictionary<string, Dictionary<string, string>>();
  36. string[] fileData = File.ReadAllLines(filePath);
  37. /* CSV文件的第一行为Key字段,第二行开始是数据。第一个字段一定是ID。 */
  38. string[] keys = fileData[0].Split(',');
  39. for (int i = 1; i < fileData.Length; i++)
  40. {
  41. string[] line = fileData[i].Split(',');
  42. /* 以ID为key值,创建一个新的集合,用于保存当前行的数据 */
  43. string ID = line[0];
  44. result[ID] = new Dictionary<string, string>();
  45. for (int j = 0; j < line.Length; j++)
  46. {
  47. /* 每一行的数据存储规则:Key字段-Value值 */
  48. result[ID][keys[j]] = line[j];
  49. }
  50. }
  51. return result;
  52. }
  53. /// <summary>
  54. /// 读取CSV文件数据(利用反射)
  55. /// </summary>
  56. /// <typeparam name="CsvData">CSV数据对象的类型</typeparam>
  57. /// <param name="csvFilePath">CSV文件路径</param>
  58. /// <param name="csvDatas">用于缓存数据的字典</param>
  59. /// <returns>CSV文件所有行内容的数据对象</returns>
  60. public static Dictionary<int, SaveWaitQueueCsvData> LoadCsvData<SaveWaitQueueCsvData>(string csvFilePath)
  61. {
  62. Dictionary<int, SaveWaitQueueCsvData> dic = new Dictionary<int, SaveWaitQueueCsvData>();
  63. /* 从CSV文件读取数据 */
  64. Dictionary<string, Dictionary<string, string>> result = LoadCsvFile(csvFilePath);
  65. /* 遍历每一行数据 */
  66. foreach (string ID in result.Keys)
  67. {
  68. /* CSV的一行数据 */
  69. Dictionary<string, string> datas = result[ID];
  70. /* 读取Csv数据对象的属性 */
  71. PropertyInfo[] props = typeof(SaveWaitQueueCsvData).GetProperties();
  72. /* 使用反射,将CSV文件的数据赋值给CSV数据对象的相应字段,要求CSV文件的字段名和CSV数据对象的字段名完全相同 */
  73. SaveWaitQueueCsvData obj = Activator.CreateInstance<SaveWaitQueueCsvData>();
  74. foreach (PropertyInfo pi in props)
  75. {
  76. pi.SetValue(obj, Convert.ChangeType(datas[pi.Name], pi.PropertyType), null);
  77. }
  78. /* 按ID-数据的形式存储 */
  79. dic[Convert.ToInt32(ID)] = obj;
  80. }
  81. return dic;
  82. }
  83. }
  84. /// <summary>
  85. /// 对待检测的特效列表临时保存
  86. /// </summary>
  87. public class SaveWaitQueueCsvData:CSVOptionTool
  88. {
  89. public int ID { get; set; }
  90. public string Name { get; set; }
  91. }
  92. /// <summary>
  93. /// 检测结果保存
  94. /// </summary>
  95. public class SaveResultCsvData:CSVOptionTool
  96. {
  97. public string Name { get; set; }//特效名
  98. public float PictureMemory { get; set; }//贴图所占用的内存
  99. public long PictureNum { get; set; }//贴图数量
  100. public long GrainCompNum { get; set; }//所有粒子系统组件数量
  101. public int DrawCallNum { get; set; }//DrawCall
  102. public long GrainNum { get; set; }//粒子数量
  103. public string ExtraDes { get; set; }//额外描述
  104. // public float PixelNum1 { get; set; }//特效原填充像素点
  105. // public float PixelNum2 { get; set; }//特效实际填充像素点
  106. // public float PixelOverdraw { get; set; }//平均每像素overdraw率
  107. }