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;
///
/// 根据当前ToolPack列表更新csv文件中的每步骤对应的正确工具名称
///
[MenuItem("GameObject/Chiva/PC检修工具/更新csv文件中的工具名称", priority = 0)]
private static void UpdateCSVToolName()
{
GameObject currentObject = Selection.activeGameObject.transform.gameObject;
if (currentObject.GetComponent())
{
//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 tempToolPack = item.GetComponent();
List 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 = GameObject.FindObjectOfType();
string courseName = operationPanelLogic.courseName;
fileName = courseName;
Debug.Log("文件名获取成功");
}
}
///
/// 加载文件
///
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);
}
///
/// 存储文件
///
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