123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using ChivaXR;
- using ChivaXR.Op;
- using UnityEngine;
- using Sirenix.OdinInspector;
- //using UnityEditor.Build.Content;
- using ExcelDataReader;
- /// <summary>
- /// 快速生成操作
- /// </summary>
- public class FastGenerationOperation : MonoBehaviour
- {
- [BoxGroup("基础数据设定")]
- [Title("指定文件路径(Excel文件)")]
- [LabelText("文件路径")]
- [FilePath(ParentFolder = "", Extensions = "xlsx", AbsolutePath = true)]
- [InlineButton("Clear", "清除")]
- [SerializeField]
- private string m_FilePath;
- [BoxGroup("基础数据设定")]
- [Title("读取数据设定")]
- [LabelText("忽略行数")]
- [SerializeField]
- private int m_IgnoreRows;
- [BoxGroup("基础数据设定")]
- [LabelText("加载列数(第一列为默认)")]
- [SerializeField]
- private int m_SpecifiedNumOfCol;
- [BoxGroup("基础数据设定")]
- [Title("设定读取当前表格的工作表索引(默认第一张表,即0)")]
- [LabelText("工作表索引")]
- [SerializeField]
- private int m_WorksheetIndex;
- [BoxGroup("基础数据设定")]
- [Title("根节点设定(为空时表示当前节点为根节点)")]
- [LabelText("根节点")]
- [SerializeField]
- private GameObject m_RootGameObject;
- [LabelText("是否自定义名称")]
- [InfoBox("默认:num_name,例:001_拧开螺丝")]
- [SerializeField]
- private bool m_IsCustomName;
- [LabelText("是否禁用生成判定")]
- [SerializeField]
- private bool m_IsDisableDecision;
- [ShowIfGroup("m_IsCustomName")]
- [BoxGroup("m_IsCustomName/名称设定")]
- [InfoBox("范例:第{i}步:")]
- [SerializeField]
- private string m_NodeName = "第{i}步:";
- [Button("清除所有数据(慎用)", ButtonSizes.Large), GUIColor(1, 0, 0)]
- private void ClearAllData()
- {
- Transform targetTransform = GetTargetTransform();
- if (targetTransform == null)
- {
- return;
- }
- if (targetTransform.GetComponent<AnimationManager>() != null)
- {
- ClearAllChild(targetTransform, 1);
- }
- else
- {
- ClearAllChild(targetTransform, 0);
- }
- }
- [Button("开始生成", ButtonSizes.Large), GUIColor(0, 1, 0)]
- private void StartGeneration()
- {
- if (string.IsNullOrEmpty(m_FilePath))
- {
- Debug.Log("指定文件不能为空");
- return;
- }
- Transform targetTransform = GetTargetTransform();
- if (targetTransform == null)
- {
- return;
- }
- CreateNode(targetTransform);
- }
- /// <summary>
- /// 获取目标Transform
- /// </summary>
- /// <returns>返回目标Transform,若条件不满足则返回null</returns>
- private Transform GetTargetTransform()
- {
- Transform targetTransform = m_RootGameObject != null ? m_RootGameObject.transform : transform;
- if (!m_IsDisableDecision)
- {
- if (targetTransform.GetComponent<AnimationManager>() == null && targetTransform.GetComponent<OperationManager>() == null)
- {
- return null;
- }
- }
- return targetTransform;
- }
- /// <summary>
- /// 创建节点
- /// </summary>
- /// <param name="_parentNode"></param>
- private void CreateNode(Transform _parentNode)
- {
- List<string> allData = ParseReadData();
- if (allData.Count <= 0)
- {
- return;
- }
- for (int i = 0; i < allData.Count; i++)
- {
- string[] lineData = allData[i].Split('|');
- if (lineData.Length < m_SpecifiedNumOfCol)
- {
- return;
- }
- GameObject tempGameObject = new GameObject();
- tempGameObject.name = (!m_IsCustomName
- ? lineData[0].PadLeft(3, '0') + "_"
- : m_NodeName.Replace("{i}", (i + 1).ToString())) + lineData[m_SpecifiedNumOfCol - 1];
- tempGameObject.transform.SetParent(_parentNode);
- if (_parentNode.GetComponent<AnimationManager>() != null)
- {
- AniData tmpData = tempGameObject.AddComponent<AniData>();
- tmpData.aniName = tempGameObject.name;
- tmpData.aniDescriptioin = lineData[m_SpecifiedNumOfCol - 1];
- }
- else if (_parentNode.GetComponent<OperationManager>() != null)
- {
- OpTrigger_ToolPack toolPack = tempGameObject.AddComponent<OpTrigger_ToolPack>();
- toolPack.operationName = tempGameObject.name;
- toolPack.operationDescription = lineData[m_SpecifiedNumOfCol - 1];
- }
- else
- {
- }
- }
- }
- /// <summary>
- /// 清除路径信息
- /// </summary>
- private void Clear()
- {
- if (string.IsNullOrEmpty(m_FilePath))
- {
- return;
- }
- m_FilePath = String.Empty;
- }
- /// <summary>
- /// 清除所有子对象
- /// </summary>
- /// <param name="_rootNode">根节点</param>
- /// <param name="_ignoreIndex">忽略索引</param>
- private void ClearAllChild(Transform _rootNode, int _ignoreIndex)
- {
- for (int i = _rootNode.childCount - 1; i >= _ignoreIndex; i--)
- {
- DestroyImmediate(_rootNode.GetChild(i).gameObject);
- }
- }
- #region Excel文件处理
- /// <summary>
- /// 解析读取到的数据
- /// </summary>
- /// <param name="_folderInfo"></param>
- /// <param name="_fileName"></param>
- /// <param name="_headerCount">表头数,即不需要进行解析的表头行总数</param>
- /// <param name="_worksheetIndex">默认是第一张表</param>
- /// <returns>返回总体数据集合,数据的连接符为'|'</returns>
- private List<string> ParseReadData()
- {
- DataSet dataSet = null;
- using (FileStream fileStream = File.Open(m_FilePath, FileMode.Open, FileAccess.Read))
- {
- using (IExcelDataReader dataReader = ExcelReaderFactory.CreateReader(fileStream))
- {
- // 获取整张表数据
- dataSet = dataReader.AsDataSet();
- }
- }
- DataTable dataTable = dataSet.Tables[m_WorksheetIndex];
- if (dataTable == null)
- {
- return null;
- }
- return ParsingTable(dataTable);
- }
- /// <summary>
- /// 解析表格
- /// </summary>
- /// <param name="_dataTable"></param>
- /// <param name="_headerCount"></param>
- /// <param name="_type">文件类型</param>
- /// <returns></returns>
- private List<string> ParsingTable(DataTable _dataTable)
- {
- if (_dataTable == null)
- {
- return null;
- }
- List<string> result = new List<string>();
- // 获取行数
- int rows = _dataTable.Rows.Count;
- // 获取列数
- int cols = _dataTable.Columns.Count;
- string recordRowData = String.Empty;
- for (int i = m_IgnoreRows; i < rows; i++)
- {
- recordRowData = string.Empty;
- for (int j = 0; j < cols; j++)
- {
- recordRowData += _dataTable.Rows[i][j].ToString() + "|";
- }
- if (!string.IsNullOrEmpty(recordRowData))
- {
- // 剔除每一行的最后一个分隔符
- recordRowData = recordRowData.Remove(recordRowData.Length - 1);
- result.Add(recordRowData);
- }
- }
- return result;
- }
- #endregion
- }
|