当前位置:   article > 正文

Unity3D资源管理——Excel表自动生成代码文件_unity excel 自动生成代码

unity excel 自动生成代码

优化版:

https://blog.csdn.net/qq_28474981/article/details/98076465

1.运行环境

1.1编辑器版本

使用Unity2017.3.1f

1.2 dll

(1)EPPlus

(2)Excel

(3)ICSharpCode.SharpZipLib

(4)System.Data(是mono文件夹底下的dll)

2.流程

1.读取目标excel文件,获取到它的前三行数据(分别为变量名,数据类型,中文名)

2.通过对前三行数据进行组合,形成可序列化的结构类以及存储多个具体数据的数据类

 

3.代码实现

3.1窗口

代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEditor;
  7. using UnityEngine;
  8. public class ConfigEditorWindow: EditorWindow
  9. {
  10. List<FileInfo> files = new List<FileInfo>();
  11. int lastSelectFileCount = 0;
  12. string lastSelectAssetGUID;
  13. [MenuItem("ResTools/Config/ConfigEditorWindow")]
  14. static void OpenConfigEditorWindow()
  15. {
  16. EditorWindow.GetWindow<ConfigEditorWindow>();
  17. }
  18. private void OnGUI()
  19. {
  20. GUILayout.BeginVertical();
  21. GUILayout.Space(10);
  22. GUILayout.Label("需要生成代码的配置文件列表");
  23. GUILayout.Space(15);
  24. if (Selection.assetGUIDs.Length != lastSelectFileCount ||
  25. (Selection.assetGUIDs.Length > 0 && lastSelectAssetGUID != Selection.assetGUIDs[Selection.assetGUIDs.Length - 1]))
  26. {
  27. files.Clear();
  28. lastSelectFileCount = Selection.assetGUIDs.Length;
  29. for (int i = 0; i < Selection.assetGUIDs.Length; i++)
  30. {
  31. FileInfo fileInfo =
  32. new FileInfo(AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[i]));
  33. if (fileInfo.Exists)
  34. {
  35. if (!files.Contains(fileInfo))
  36. files.Add(fileInfo);
  37. }
  38. }
  39. lastSelectAssetGUID = Selection.assetGUIDs.Last<string>();
  40. }
  41. for (int i = 0; i < files.Count; i++)
  42. {
  43. if (files[i].Exists)
  44. {
  45. GUILayout.BeginHorizontal();
  46. GUILayout.Label("名称 :" + files[i].Name);
  47. GUILayout.EndHorizontal();
  48. }
  49. }
  50. GUILayout.BeginHorizontal();
  51. if (GUILayout.Button("生成"))
  52. {
  53. ConfigUtils.GenerateConfigsToCode(ConfigFileType.Excel, files,Application.dataPath+"/Scripts/Config");
  54. }
  55. GUILayout.EndHorizontal();
  56. GUILayout.EndVertical();
  57. }
  58. }

实现效果:

 

3.2核心逻辑

代码:

  1. using System.Text;
  2. using ResourceSystem;
  3. using System.Data;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using System.Reflection;
  8. public enum ConfigFileType
  9. {
  10. Excel,
  11. }
  12. public class ConfigUtils
  13. {
  14. #region 自动生成配置代码
  15. static List<string> argsNames = new List<string>();
  16. static List<string> argsType = new List<string>();
  17. static List<string> argsChineseName = new List<string>();
  18. public static void GenerateConfigsToCode(ConfigFileType fileType, List<FileInfo> fileInfos, string targetPath)
  19. {
  20. for(int i=0;i< fileInfos.Count;i++)
  21. {
  22. GenerateConfigToCode(fileType, fileInfos[i], targetPath);
  23. }
  24. }
  25. public static void GenerateConfigToCode(ConfigFileType fileType,FileInfo fileInfo,string targetPath)
  26. {
  27. if (fileType== ConfigFileType.Excel)
  28. {
  29. GenerateExcelToCode(fileInfo.FullName, targetPath);
  30. }
  31. }
  32. private static void GenerateExcelToCode(string path,string targetPath)
  33. {
  34. DataTableCollection tables = ResourceSystemFacade.Inst.ReadExcel(path);
  35. argsNames.Clear();
  36. argsType.Clear();
  37. argsChineseName.Clear();
  38. int columns = tables[0].Columns.Count;//获取列数
  39. int rows = tables[0].Rows.Count;
  40. for (int n = 0; n < columns; n++)
  41. {
  42. argsNames.Add(tables[0].Rows[0][n].ToString());
  43. argsType.Add(tables[0].Rows[1][n].ToString());
  44. argsChineseName.Add(tables[0].Rows[2][n].ToString());
  45. }
  46. string fileName = Path.GetFileNameWithoutExtension(path);
  47. string code = GenerateCode(fileName);
  48. if (!string.IsNullOrEmpty(code))
  49. {
  50. DirectoryInfo info = new DirectoryInfo(targetPath);
  51. if (!info.Exists)
  52. {
  53. Directory.CreateDirectory(targetPath);
  54. }
  55. File.WriteAllText(targetPath +"/"+ fileName + "Info.cs", code);
  56. }
  57. else
  58. {
  59. DebugUtils.DebugError("生成代码有误");
  60. }
  61. }
  62. /// <summary>
  63. ///
  64. /// </summary>
  65. /// <param name="fileName"></param>
  66. /// <returns></returns>
  67. private static string GenerateCode(string fileName)
  68. {
  69. StringBuilder code = new StringBuilder();
  70. code.Append("using System;\nusing UnityEngine;\nusing System.Collections.Generic;\nusing ResourceSystem;\n\n");
  71. code.Append("public class ");
  72. code.Append(fileName);
  73. code.Append("Info:ScriptableObject,IDataCollectionTemplete\n{");
  74. code.Append("\n public List<");
  75. code.Append(fileName);
  76. code.Append("Data> Data = new List<");
  77. code.Append(fileName);
  78. code.Append("Data>();");
  79. code.Append("\n /// <summary>\n ");
  80. code.Append(" /// 获得内容Type");
  81. code.Append("\n /// </summary>\n public Type GetContenType()\n {");
  82. code.Append("\n return typeof(");
  83. code.Append(fileName);
  84. code.Append("Data);");
  85. code.Append("\n }");
  86. code.Append("\n /// <summary>\n ");
  87. code.Append(" /// 转换成当前内容类型的列表");
  88. code.Append("\n /// </summary>\n public void ConvertToContentList(List<IDataTemplete> contents)\n {");
  89. code.Append("\n for(int i = 0; i < contents.Count; i++)\n {");
  90. code.Append("\n Data.Add((");
  91. code.Append(fileName);
  92. code.Append("Data)contents[i]);");
  93. code.Append("\n }");
  94. code.Append("\n }");
  95. code.Append("\n}\n\n");
  96. code.Append("\n[Serializable]\n");
  97. code.Append("public class ");
  98. code.Append(fileName);
  99. code.Append("Data:IDataTemplete\n{");
  100. for(int i = 0; i < argsNames.Count; i++)
  101. {
  102. code.Append("\n /// <summary>\n ");
  103. code.Append(" /// ");
  104. code.Append(argsChineseName[i]);
  105. code.Append("\n /// </summary>");
  106. code.Append("\n public ");
  107. code.Append(argsType[i]);
  108. code.Append(" ");
  109. code.Append(argsNames[i]);
  110. code.Append("= default(");
  111. code.Append(argsType[i]);
  112. code.Append(");");
  113. }
  114. code.Append("\n /// <summary>\n ");
  115. code.Append(" /// 反序列化");
  116. code.Append("\n /// </summary>\n public void DeSerialize(object[] content)\n {");
  117. for (int i = 0; i < argsNames.Count; i++)
  118. {
  119. code.Append("\n ");
  120. code.Append(argsNames[i]);
  121. if (argsType[i] == "int")
  122. {
  123. code.Append(" = Int32.Parse(content[");
  124. code.Append(i);
  125. code.Append("].ToString());");
  126. }
  127. else
  128. {
  129. code.Append(" = content[");
  130. code.Append(i);
  131. code.Append("].ToString();");
  132. }
  133. }
  134. code.Append("\n }");
  135. code.Append("\n}");
  136. return code.ToString();
  137. }
  138. #endregion
  139. }

实现效果:

由文件Test.xlsx

转化为数据文件

  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using ResourceSystem;
  5. public class TestInfo:ScriptableObject,IDataCollectionTemplete
  6. {
  7. public List<TestData> Data = new List<TestData>();
  8. /// <summary>
  9. /// 获得内容Type
  10. /// </summary>
  11. public Type GetContenType()
  12. {
  13. return typeof(TestData);
  14. }
  15. /// <summary>
  16. /// 转换成当前内容类型的列表
  17. /// </summary>
  18. public void ConvertToContentList(List<IDataTemplete> contents)
  19. {
  20. for(int i = 0; i < contents.Count; i++)
  21. {
  22. Data.Add((TestData)contents[i]);
  23. }
  24. }
  25. }
  26. [Serializable]
  27. public class TestData:IDataTemplete
  28. {
  29. /// <summary>
  30. /// 索引号
  31. /// </summary>
  32. public int ID= default(int);
  33. /// <summary>
  34. /// 名称
  35. /// </summary>
  36. public string Name= default(string);
  37. /// <summary>
  38. /// 反序列化
  39. /// </summary>
  40. public void DeSerialize(object[] content)
  41. {
  42. ID = Int32.Parse(content[0].ToString());
  43. Name = content[1].ToString();
  44. }
  45. }

PS:嫌Excel上两行的英文丑的话可以选择右键隐藏上面两行

可运行工程可以看:https://blog.csdn.net/qq_28474981/article/details/82749021

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/115309
推荐阅读
相关标签
  

闽ICP备14008679号