FastGenerationOperation.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using ChivaXR;
  7. using ChivaXR.Op;
  8. using UnityEngine;
  9. using Sirenix.OdinInspector;
  10. //using UnityEditor.Build.Content;
  11. using ExcelDataReader;
  12. /// <summary>
  13. /// 快速生成操作
  14. /// </summary>
  15. public class FastGenerationOperation : MonoBehaviour
  16. {
  17. [BoxGroup("基础数据设定")]
  18. [Title("指定文件路径(Excel文件)")]
  19. [LabelText("文件路径")]
  20. [FilePath(ParentFolder = "", Extensions = "xlsx", AbsolutePath = true)]
  21. [InlineButton("Clear", "清除")]
  22. [SerializeField]
  23. private string m_FilePath;
  24. [BoxGroup("基础数据设定")]
  25. [Title("读取数据设定")]
  26. [LabelText("忽略行数")]
  27. [SerializeField]
  28. private int m_IgnoreRows;
  29. [BoxGroup("基础数据设定")]
  30. [LabelText("加载列数(第一列为默认)")]
  31. [SerializeField]
  32. private int m_SpecifiedNumOfCol;
  33. [BoxGroup("基础数据设定")]
  34. [Title("设定读取当前表格的工作表索引(默认第一张表,即0)")]
  35. [LabelText("工作表索引")]
  36. [SerializeField]
  37. private int m_WorksheetIndex;
  38. [BoxGroup("基础数据设定")]
  39. [Title("根节点设定(为空时表示当前节点为根节点)")]
  40. [LabelText("根节点")]
  41. [SerializeField]
  42. private GameObject m_RootGameObject;
  43. [LabelText("是否自定义名称")]
  44. [InfoBox("默认:num_name,例:001_拧开螺丝")]
  45. [SerializeField]
  46. private bool m_IsCustomName;
  47. [LabelText("是否禁用生成判定")]
  48. [SerializeField]
  49. private bool m_IsDisableDecision;
  50. [ShowIfGroup("m_IsCustomName")]
  51. [BoxGroup("m_IsCustomName/名称设定")]
  52. [InfoBox("范例:第{i}步:")]
  53. [SerializeField]
  54. private string m_NodeName = "第{i}步:";
  55. [Button("清除所有数据(慎用)", ButtonSizes.Large), GUIColor(1, 0, 0)]
  56. private void ClearAllData()
  57. {
  58. Transform targetTransform = GetTargetTransform();
  59. if (targetTransform == null)
  60. {
  61. return;
  62. }
  63. if (targetTransform.GetComponent<AnimationManager>() != null)
  64. {
  65. ClearAllChild(targetTransform, 1);
  66. }
  67. else
  68. {
  69. ClearAllChild(targetTransform, 0);
  70. }
  71. }
  72. [Button("开始生成", ButtonSizes.Large), GUIColor(0, 1, 0)]
  73. private void StartGeneration()
  74. {
  75. if (string.IsNullOrEmpty(m_FilePath))
  76. {
  77. Debug.Log("指定文件不能为空");
  78. return;
  79. }
  80. Transform targetTransform = GetTargetTransform();
  81. if (targetTransform == null)
  82. {
  83. return;
  84. }
  85. CreateNode(targetTransform);
  86. }
  87. /// <summary>
  88. /// 获取目标Transform
  89. /// </summary>
  90. /// <returns>返回目标Transform,若条件不满足则返回null</returns>
  91. private Transform GetTargetTransform()
  92. {
  93. Transform targetTransform = m_RootGameObject != null ? m_RootGameObject.transform : transform;
  94. if (!m_IsDisableDecision)
  95. {
  96. if (targetTransform.GetComponent<AnimationManager>() == null && targetTransform.GetComponent<OperationManager>() == null)
  97. {
  98. return null;
  99. }
  100. }
  101. return targetTransform;
  102. }
  103. /// <summary>
  104. /// 创建节点
  105. /// </summary>
  106. /// <param name="_parentNode"></param>
  107. private void CreateNode(Transform _parentNode)
  108. {
  109. List<string> allData = ParseReadData();
  110. if (allData.Count <= 0)
  111. {
  112. return;
  113. }
  114. for (int i = 0; i < allData.Count; i++)
  115. {
  116. string[] lineData = allData[i].Split('|');
  117. if (lineData.Length < m_SpecifiedNumOfCol)
  118. {
  119. return;
  120. }
  121. GameObject tempGameObject = new GameObject();
  122. tempGameObject.name = (!m_IsCustomName
  123. ? lineData[0].PadLeft(3, '0') + "_"
  124. : m_NodeName.Replace("{i}", (i + 1).ToString())) + lineData[m_SpecifiedNumOfCol - 1];
  125. tempGameObject.transform.SetParent(_parentNode);
  126. if (_parentNode.GetComponent<AnimationManager>() != null)
  127. {
  128. AniData tmpData = tempGameObject.AddComponent<AniData>();
  129. tmpData.aniName = tempGameObject.name;
  130. tmpData.aniDescriptioin = lineData[m_SpecifiedNumOfCol - 1];
  131. }
  132. else if (_parentNode.GetComponent<OperationManager>() != null)
  133. {
  134. OpTrigger_ToolPack toolPack = tempGameObject.AddComponent<OpTrigger_ToolPack>();
  135. toolPack.operationName = tempGameObject.name;
  136. toolPack.operationDescription = lineData[m_SpecifiedNumOfCol - 1];
  137. }
  138. else
  139. {
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// 清除路径信息
  145. /// </summary>
  146. private void Clear()
  147. {
  148. if (string.IsNullOrEmpty(m_FilePath))
  149. {
  150. return;
  151. }
  152. m_FilePath = String.Empty;
  153. }
  154. /// <summary>
  155. /// 清除所有子对象
  156. /// </summary>
  157. /// <param name="_rootNode">根节点</param>
  158. /// <param name="_ignoreIndex">忽略索引</param>
  159. private void ClearAllChild(Transform _rootNode, int _ignoreIndex)
  160. {
  161. for (int i = _rootNode.childCount - 1; i >= _ignoreIndex; i--)
  162. {
  163. DestroyImmediate(_rootNode.GetChild(i).gameObject);
  164. }
  165. }
  166. #region Excel文件处理
  167. /// <summary>
  168. /// 解析读取到的数据
  169. /// </summary>
  170. /// <param name="_folderInfo"></param>
  171. /// <param name="_fileName"></param>
  172. /// <param name="_headerCount">表头数,即不需要进行解析的表头行总数</param>
  173. /// <param name="_worksheetIndex">默认是第一张表</param>
  174. /// <returns>返回总体数据集合,数据的连接符为'|'</returns>
  175. private List<string> ParseReadData()
  176. {
  177. DataSet dataSet = null;
  178. using (FileStream fileStream = File.Open(m_FilePath, FileMode.Open, FileAccess.Read))
  179. {
  180. using (IExcelDataReader dataReader = ExcelReaderFactory.CreateReader(fileStream))
  181. {
  182. // 获取整张表数据
  183. dataSet = dataReader.AsDataSet();
  184. }
  185. }
  186. DataTable dataTable = dataSet.Tables[m_WorksheetIndex];
  187. if (dataTable == null)
  188. {
  189. return null;
  190. }
  191. return ParsingTable(dataTable);
  192. }
  193. /// <summary>
  194. /// 解析表格
  195. /// </summary>
  196. /// <param name="_dataTable"></param>
  197. /// <param name="_headerCount"></param>
  198. /// <param name="_type">文件类型</param>
  199. /// <returns></returns>
  200. private List<string> ParsingTable(DataTable _dataTable)
  201. {
  202. if (_dataTable == null)
  203. {
  204. return null;
  205. }
  206. List<string> result = new List<string>();
  207. // 获取行数
  208. int rows = _dataTable.Rows.Count;
  209. // 获取列数
  210. int cols = _dataTable.Columns.Count;
  211. string recordRowData = String.Empty;
  212. for (int i = m_IgnoreRows; i < rows; i++)
  213. {
  214. recordRowData = string.Empty;
  215. for (int j = 0; j < cols; j++)
  216. {
  217. recordRowData += _dataTable.Rows[i][j].ToString() + "|";
  218. }
  219. if (!string.IsNullOrEmpty(recordRowData))
  220. {
  221. // 剔除每一行的最后一个分隔符
  222. recordRowData = recordRowData.Remove(recordRowData.Length - 1);
  223. result.Add(recordRowData);
  224. }
  225. }
  226. return result;
  227. }
  228. #endregion
  229. }