BuildExcelEditor.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. public class BuildExcelWindow : EditorWindow
  8. {
  9. // 定义一个静态变量来持有选中的ScriptableObject引用
  10. public static TaskConfig selectedObject;
  11. [MenuItem("MyTools/addexcel")]
  12. public static void ExportScriptableObjectToExcel()
  13. {
  14. string path = EditorUtility.OpenFilePanel("Select File", "", "");
  15. if (!string.IsNullOrEmpty(path))
  16. {
  17. string pp = path.Split('/')[path.Split('/').Length - 2] +"/"+ path.Split('/')[path.Split('/').Length - 1];
  18. Debug.Log("Selected file: " + pp);
  19. var obj = AssetDatabase.LoadAssetAtPath<TaskConfig>(pp);
  20. if (obj != null)
  21. {
  22. Selection.activeObject = obj;
  23. Debug.Log("Selected MyScriptableObject: " + obj.name);
  24. StringBuilder csv = new StringBuilder();
  25. // 添加标题行
  26. // csv.AppendLine("Name,Number");
  27. for (int i = 0; i < obj.TaskList.Count; i++)
  28. {
  29. string xs = "";
  30. if (obj.TaskList[i].xiansuos != null && obj.TaskList[i].xiansuos.Length > 0)
  31. {
  32. for (int j = 0; j < obj.TaskList[i].xiansuos.Length; j++)
  33. {
  34. if (j >= obj.TaskList[i].xiansuos.Length - 1)
  35. {
  36. xs += obj.TaskList[i].xiansuos[j];
  37. }
  38. else
  39. {
  40. xs += obj.TaskList[i].xiansuos[j] + "|";
  41. }
  42. }
  43. }
  44. string dj = "";
  45. if (obj.TaskList[i].daojus != null && obj.TaskList[i].daojus.Length > 0)
  46. {
  47. for (int j = 0; j < obj.TaskList[i].daojus.Length; j++)
  48. {
  49. if (j >= obj.TaskList[i].daojus.Length - 1)
  50. {
  51. dj += obj.TaskList[i].daojus[j];
  52. }
  53. else
  54. {
  55. dj += obj.TaskList[i].daojus[j] + "|";
  56. }
  57. }
  58. }
  59. // 添加数据行
  60. csv.AppendLine($"{i + 1},{obj.TaskList[i].info},{obj.TaskList[i].info2}" +
  61. $",{xs}" +
  62. $",{obj.TaskList[i].type}" +
  63. $",{obj.TaskList[i].isNoshow}" +
  64. $",{obj.TaskList[i].bgName}" +
  65. $",{obj.TaskList[i].imgName}" +
  66. $",{obj.TaskList[i].taskcheck}" +
  67. $",{obj.TaskList[i].centerName}" +
  68. $",{obj.TaskList[i].lihui}" +
  69. $",{obj.TaskList[i].texiao}" +
  70. $",{obj.TaskList[i].guochang}" +
  71. $",{obj.TaskList[i].isguochangskip}" +
  72. $",{obj.TaskList[i].lihuisize}" +
  73. $",{obj.TaskList[i].centersize}" +
  74. $",{obj.TaskList[i].perbName}" +
  75. $",{obj.TaskList[i].isClose}" +
  76. $",{obj.TaskList[i].isTuo}" +
  77. $",{dj}" +
  78. $",{obj.TaskList[i].isClosebtShow}" +
  79. $",{obj.TaskList[i].isTextNext}" +
  80. $",{obj.TaskList[i].isARSao}" +
  81. $",{obj.TaskList[i].arid}");
  82. }
  83. string nameWithoutExtension = GetFileNameWithoutExtension(path);
  84. Debug.Log(nameWithoutExtension);
  85. // 写入文件
  86. File.WriteAllText(Application.streamingAssetsPath + "/" + nameWithoutExtension + ".csv", csv.ToString());
  87. }
  88. }
  89. }
  90. public static string GetFileNameWithoutExtension(string filePath)
  91. {
  92. return Path.GetFileNameWithoutExtension(filePath);
  93. }
  94. [MenuItem("MyTools/Excel Window",priority = 100)]
  95. public static void ShowReadExcelWindow()
  96. {
  97. BuildExcelWindow window = GetWindow<BuildExcelWindow>(true);
  98. window.Show();
  99. window.minSize = new Vector2(475,475);
  100. }
  101. //Excel读取路径,绝对路径,放在Assets同级路径
  102. private static string excelReadAbsolutePath;
  103. //自动生成C#类文件路径,绝对路径
  104. private static string scriptSaveAbsolutePath;
  105. private static string scriptSaveRelativePath;
  106. //自动生成Asset文件路径,相对路径
  107. private static string assetSaveRelativePath;
  108. private List<string> fileNameList = new List<string>();
  109. private List<string> filePathList = new List<string>();
  110. private void Awake()
  111. {
  112. titleContent.text = "Excel配置表读取";
  113. excelReadAbsolutePath = Application.dataPath.Replace("Assets","Excel");
  114. scriptSaveAbsolutePath = Application.dataPath + CheckEditorPath("/Script/Excel/AutoCreateCSCode");
  115. scriptSaveRelativePath = CheckEditorPath("Assets/Script/Excel/AutoCreateCSCode");
  116. assetSaveRelativePath = CheckEditorPath("Assets/AssetData/Excel/AutoCreateAsset");
  117. }
  118. private void OnEnable()
  119. {
  120. RefreshExcelFile();
  121. }
  122. private void OnDisable()
  123. {
  124. fileNameList.Clear();
  125. filePathList.Clear();
  126. }
  127. private Vector2 scrollPosition = Vector2.zero;
  128. private void OnGUI()
  129. {
  130. GUILayout.Space(10);
  131. scrollPosition = GUILayout.BeginScrollView(scrollPosition,GUILayout.Width(position.width),GUILayout.Height(position.height));
  132. //展示路径
  133. GUILayout.BeginHorizontal(GUILayout.Height(20));
  134. if(GUILayout.Button("Excel读取路径",GUILayout.Width(100)))
  135. {
  136. EditorUtility.OpenWithDefaultApp(excelReadAbsolutePath);
  137. Debug.Log(excelReadAbsolutePath);
  138. }
  139. if(GUILayout.Button("Script保存路径",GUILayout.Width(100)))
  140. {
  141. SelectObject(scriptSaveRelativePath);
  142. }
  143. if(GUILayout.Button("Asset保存路径",GUILayout.Width(100)))
  144. {
  145. SelectObject(assetSaveRelativePath);
  146. }
  147. GUILayout.EndHorizontal();
  148. GUILayout.Space(5);
  149. //Excel列表
  150. GUILayout.Label("Excel列表:");
  151. for(int i = 0; i < fileNameList.Count; i++)
  152. {
  153. GUILayout.BeginHorizontal("Box",GUILayout.Height(40));
  154. GUILayout.Label($"{i}:","Titlebar Foldout",GUILayout.Width(30),GUILayout.Height(35));
  155. GUILayout.Box(fileNameList[i],"MeTransitionBlock",GUILayout.MinWidth(200),GUILayout.Height(35));
  156. GUILayout.Space(10);
  157. //生成CS代码
  158. if(GUILayout.Button("Create Script",GUILayout.Width(100),GUILayout.Height(30)))
  159. {
  160. ExcelDataReader.ReadOneExcelToCode(filePathList[i],scriptSaveAbsolutePath);
  161. }
  162. //生成Asset文件
  163. if(GUILayout.Button("Create Asset",GUILayout.Width(100),GUILayout.Height(30)))
  164. {
  165. ExcelDataReader.CreateOneExcelAsset(filePathList[i],assetSaveRelativePath);
  166. }
  167. GUILayout.EndHorizontal();
  168. GUILayout.Space(5);
  169. }
  170. GUILayout.Space(10);
  171. //一键处理所有Excel
  172. GUILayout.Label("一键操作:");
  173. GUILayout.BeginHorizontal("Box",GUILayout.Height(40));
  174. GUILayout.Label("all","Titlebar Foldout",GUILayout.Width(30),GUILayout.Height(35));
  175. GUILayout.Box("All Excel","MeTransitionBlock",GUILayout.MinWidth(200),GUILayout.Height(35));
  176. GUILayout.Space(10);
  177. if(GUILayout.Button("Create Script",GUILayout.Width(100),GUILayout.Height(30)))
  178. {
  179. ExcelDataReader.ReadAllExcelToCode(excelReadAbsolutePath,scriptSaveAbsolutePath);
  180. }
  181. if(GUILayout.Button("Create Asset",GUILayout.Width(100),GUILayout.Height(30)))
  182. {
  183. ExcelDataReader.CreateAllExcelAsset(excelReadAbsolutePath,assetSaveRelativePath);
  184. }
  185. GUILayout.EndHorizontal();
  186. //
  187. GUILayout.Space(20);
  188. //
  189. GUILayout.EndScrollView();
  190. }
  191. //读取指定路径下的Excel文件名
  192. private void RefreshExcelFile()
  193. {
  194. fileNameList.Clear();
  195. filePathList.Clear();
  196. if(!Directory.Exists(excelReadAbsolutePath))
  197. {
  198. Debug.LogError("无效路径:" + excelReadAbsolutePath);
  199. return;
  200. }
  201. string[] excelFileFullPaths = Directory.GetFiles(excelReadAbsolutePath,"*.xlsx");
  202. if(excelFileFullPaths == null || excelFileFullPaths.Length == 0)
  203. {
  204. Debug.LogError(excelReadAbsolutePath + "路径下没有找到Excel文件");
  205. return;
  206. }
  207. filePathList.AddRange(excelFileFullPaths);
  208. for(int i = 0; i < filePathList.Count; i++)
  209. {
  210. fileNameList.Add(Path.GetFileName(filePathList[i]));
  211. }
  212. Debug.Log("找到Excel文件:" + fileNameList.Count + "个");
  213. }
  214. private void SelectObject(string targetPath)
  215. {
  216. Object targetObj = AssetDatabase.LoadAssetAtPath<Object>(targetPath);
  217. EditorGUIUtility.PingObject(targetObj);
  218. Selection.activeObject = targetObj;
  219. Debug.Log(targetPath);
  220. }
  221. private static string CheckEditorPath(string path)
  222. {
  223. #if UNITY_EDITOR_WIN
  224. return path.Replace("/","\\");
  225. #elif UNITY_EDITOR_OSX
  226. return path.Replace("\\","/");
  227. #else
  228. return path;
  229. #endif
  230. }
  231. }