123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.IO;
- #if UNITY_EDITOR
- public class CSVUpdateTool
- {
- // 更新的文件名
- private static string fileName = "测试";
- // 文件格式
- private const string EXTENSION = ".csv";
- private static string path = Application.streamingAssetsPath + "/Load/";
- private static CSVTable table;
- /// <summary>
- /// 根据当前ToolPack列表更新csv文件中的每步骤对应的正确工具名称
- /// </summary>
- [MenuItem("GameObject/Chiva/PC检修工具/更新csv文件中的工具名称", priority = 0)]
- private static void UpdateCSVToolName()
- {
- GameObject currentObject = Selection.activeGameObject.transform.gameObject;
- if (currentObject.GetComponent<OpTrigger_ToolPack>())
- {
- //GetFileName();
- Load();
- Transform selectTranform = currentObject.transform.parent;
- for (int i = 0; i < selectTranform.childCount; i++)
- {
- Transform item = selectTranform.GetChild(i);
- if (item.GetComponent<OpTrigger_ToolPack>())
- {
- OpTrigger_ToolPack tempToolPack = item.GetComponent<OpTrigger_ToolPack>();
- List<string> choseToolNames = tempToolPack.choseToolNames;
- string toolname = "";
- if (choseToolNames == null || choseToolNames.Count == 0)
- {
- toolname = "";
- }
- else
- {
- toolname = choseToolNames[0];
- }
- // 修改数据
- table[(i + 1).ToString()]["toolname"] = toolname;
- }
- }
- Save();
- }
- }
- private static void GetFileName()
- {
- if (GameObject.FindObjectOfType<OperationPanelLogic>())
- {
- OperationPanelLogic operationPanelLogic = GameObject.FindObjectOfType<OperationPanelLogic>();
- string courseName = operationPanelLogic.courseName;
- fileName = courseName;
- Debug.Log("文件名获取成功");
- }
- }
- /// <summary>
- /// 加载文件
- /// </summary>
- private static void Load()
- {
- if (!Directory.Exists(path))
- {
- Debug.LogError("The file not be found in this path. path:" + path);
- return;
- }
- string fullFileName = path + fileName + EXTENSION;
- StreamReader sr;
- sr = File.OpenText(fullFileName);
- string content = sr.ReadToEnd();
- sr.Close();
- sr.Dispose();
- Debug.Log(content);
- table = CSVTable.CreateTable(fileName, content);
- }
- /// <summary>
- /// 存储文件
- /// </summary>
- private static void Save()
- {
- if (table == null)
- {
- Debug.LogError("The table is null.");
- return;
- }
- string tableContent = table.GetContent();
- if (!Directory.Exists(path))
- {
- Debug.Log("未找到路径, 已自动创建");
- Directory.CreateDirectory(path);
- }
- string fullFileName = path + fileName + EXTENSION;
- using (StreamWriter sw = new StreamWriter(File.OpenWrite(fullFileName),System.Text.Encoding.UTF8))
- {
- sw.Write(tableContent);
- }
- table = null;
- Debug.Log("Save");
- }
- }
- #endif
|