CSVUpdateTool.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6. #if UNITY_EDITOR
  7. public class CSVUpdateTool
  8. {
  9. // 更新的文件名
  10. private static string fileName = "测试";
  11. // 文件格式
  12. private const string EXTENSION = ".csv";
  13. private static string path = Application.streamingAssetsPath + "/Load/";
  14. private static CSVTable table;
  15. /// <summary>
  16. /// 根据当前ToolPack列表更新csv文件中的每步骤对应的正确工具名称
  17. /// </summary>
  18. [MenuItem("GameObject/Chiva/PC检修工具/更新csv文件中的工具名称", priority = 0)]
  19. private static void UpdateCSVToolName()
  20. {
  21. GameObject currentObject = Selection.activeGameObject.transform.gameObject;
  22. if (currentObject.GetComponent<OpTrigger_ToolPack>())
  23. {
  24. //GetFileName();
  25. Load();
  26. Transform selectTranform = currentObject.transform.parent;
  27. for (int i = 0; i < selectTranform.childCount; i++)
  28. {
  29. Transform item = selectTranform.GetChild(i);
  30. if (item.GetComponent<OpTrigger_ToolPack>())
  31. {
  32. OpTrigger_ToolPack tempToolPack = item.GetComponent<OpTrigger_ToolPack>();
  33. List<string> choseToolNames = tempToolPack.choseToolNames;
  34. string toolname = "";
  35. if (choseToolNames == null || choseToolNames.Count == 0)
  36. {
  37. toolname = "";
  38. }
  39. else
  40. {
  41. toolname = choseToolNames[0];
  42. }
  43. // 修改数据
  44. table[(i + 1).ToString()]["toolname"] = toolname;
  45. }
  46. }
  47. Save();
  48. }
  49. }
  50. private static void GetFileName()
  51. {
  52. if (GameObject.FindObjectOfType<OperationPanelLogic>())
  53. {
  54. OperationPanelLogic operationPanelLogic = GameObject.FindObjectOfType<OperationPanelLogic>();
  55. string courseName = operationPanelLogic.courseName;
  56. fileName = courseName;
  57. Debug.Log("文件名获取成功");
  58. }
  59. }
  60. /// <summary>
  61. /// 加载文件
  62. /// </summary>
  63. private static void Load()
  64. {
  65. if (!Directory.Exists(path))
  66. {
  67. Debug.LogError("The file not be found in this path. path:" + path);
  68. return;
  69. }
  70. string fullFileName = path + fileName + EXTENSION;
  71. StreamReader sr;
  72. sr = File.OpenText(fullFileName);
  73. string content = sr.ReadToEnd();
  74. sr.Close();
  75. sr.Dispose();
  76. Debug.Log(content);
  77. table = CSVTable.CreateTable(fileName, content);
  78. }
  79. /// <summary>
  80. /// 存储文件
  81. /// </summary>
  82. private static void Save()
  83. {
  84. if (table == null)
  85. {
  86. Debug.LogError("The table is null.");
  87. return;
  88. }
  89. string tableContent = table.GetContent();
  90. if (!Directory.Exists(path))
  91. {
  92. Debug.Log("未找到路径, 已自动创建");
  93. Directory.CreateDirectory(path);
  94. }
  95. string fullFileName = path + fileName + EXTENSION;
  96. using (StreamWriter sw = new StreamWriter(File.OpenWrite(fullFileName),System.Text.Encoding.UTF8))
  97. {
  98. sw.Write(tableContent);
  99. }
  100. table = null;
  101. Debug.Log("Save");
  102. }
  103. }
  104. #endif