Files
plugin-library/Assets/05.TableTextConversion/Editor/ConvertedExslDataMessage.cs

263 lines
8.4 KiB
C#
Raw Normal View History

2025-04-01 18:26:48 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Excel;
using UnityEditor;
using UnityEngine;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using System.Linq;
using Stary.Evo.TableTextConversion;
namespace Stary.Evo.TableTextConversion
{
public class ConvertedExslDataMessage : OdinEditorWindow
{
private static ConvertedExslDataMessage window;
// 读取的位置
private static string DocFolderPath;
// 生成配置文件的位置
private static string Assetpath;
// 转化表的名称
private static string newDataName;
[MenuItem("Exsl/信息表转换")]
static void ShowWindows()
{
window = (ConvertedExslDataMessage)EditorWindow.GetWindow(typeof(ConvertedExslDataMessage));
window.Show();
}
[Title("转化配置的Exsl文件", titleAlignment: TitleAlignments.Centered)]
[HorizontalGroup("BuildPipeline"), HideLabel]
[ValueDropdown("GetBuildExslNames")]
[OnValueChanged("SetBuildExslNames")]
public string selectedExslNames;
[Title("转化文件存储的作用域:", titleAlignment: TitleAlignments.Centered)]
[HorizontalGroup("BuildPipeline"), HideLabel]
[ValueDropdown("GetCreatDomainAllName")]
[OnValueChanged("SetCurrentDomainName")]
public string selectedDomainNames;
#region
//[MenuItem("Exsl/信息表转换")]
[Button("转化Exsl表格", ButtonSizes.Large)]
public static void CrtateMessage()
{
#region Excel表格并转成DataSet类型的数据
if (DocFolderPath == null)
{
Debug.LogError("UnityEvo:请先选择Exsl文件");
return;
}
if (Assetpath == null)
{
Debug.LogError("UnityEvo:请先选择生成配置文件的位置");
return;
}
/*if (!Directory.Exists(DocFolderPath))
{
Debug.LogError("UnityEvo:Exsl文件不存在");
return;
}*/
if (!Directory.Exists(Assetpath))
{
Debug.LogError("UnityEvo:生成配置文件的位置不存在");
return;
}
FileStream stream = File.Open(DocFolderPath, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//获取结果
DataSet result = excelReader.AsDataSet();
int oneRows = result.Tables[0].Rows.Count; //行数
Dictionary<int, int> dict_Question = new Dictionary<int, int>();
Dictionary<int, int> dict_Menu = new Dictionary<int, int>();
#endregion
#region
int VoiceCount = 0;
for (int i = 0; i < oneRows; i++)
{
string key = FilterTo(result.Tables[0].Rows[i][0].ToString());
if (!string.IsNullOrEmpty(key))
{
VoiceCount++;
}
}
#endregion
// 条件判断
/*if (newDataName == null)
{
return;
}
else if(newDataName == "")
{
}*/
#region AudioTableData
#region
List<AudioTableData.MessageInfo> messageInfos = new List<AudioTableData.MessageInfo>();
for (int i = 1; i < VoiceCount; i++)
{
AudioTableData.MessageInfo messageInfo = new AudioTableData.MessageInfo();
//获取对话块
messageInfo.index = i - 1;
//文件名称
messageInfo.name = FilterTo(result.Tables[0].Rows[i][0].ToString());
//获取内容
messageInfo.nameMessage = FilterTo(result.Tables[0].Rows[i][1].ToString());
//添加数据
messageInfos.Add(messageInfo);
}
#endregion
ScriptObjectSave<AudioTableData>("AudioTableData", (data) =>
{
data.infos = new List<AudioTableData.MessageInfo>();
data.infos = messageInfos;
});
#endregion
}
/// <summary>
/// 过滤:去掉首尾空格和换行符【\n】
/// </summary>
private static string FilterTo(string self)
{
self = self.TrimStart();
self = self.TrimEnd();
self = self.Replace("\n", "");
return self;
}
/// <summary>
/// ScriptObject数据保存类
/// </summary>
/// <param name="assetName"></param>
/// <param name="action">写入数据的一个回调</param>
/// <typeparam name="T"></typeparam>
private static void ScriptObjectSave<T>(string assetName, Action<T> action) where T : ScriptableObject
{
string jsonPath = $"{Assetpath}{assetName}.asset";
if (File.Exists(jsonPath))
{
EditorFrameworkUtils.DeleteFile(Assetpath, assetName + ".asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
jsonPath = jsonPath.Substring(jsonPath.IndexOf("Assets"));
// Debug.Log($"要创建的为{jsonPath}");
var data = ScriptableObject.CreateInstance<T>();
//放写入数据操作
action.Invoke(data);
AssetDatabase.CreateAsset(data, jsonPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
#endregion
/// <summary>
/// 获取Doc文件夹的路径
/// </summary>
/// <returns></returns>
private string GetDocFolderPath()
{
// 获取 Assets 文件夹的路径
string assetsPath = Application.dataPath;
// 获取项目的根目录路径
string projectRootPath = Path.GetDirectoryName(assetsPath);
// 构造 Doc 文件夹的路径
string docFolderPath = Path.Combine(projectRootPath, "Doc");
return docFolderPath;
}
/// <summary>
/// 获取Doc文件夹下所有的Exsl名
/// </summary>
/// <returns></returns>
private List<string> GetBuildExslNames()
{
string docFolderPath = GetDocFolderPath();
// 检查 Doc 文件夹是否存在
if (!Directory.Exists(docFolderPath))
{
Debug.LogError($"UnityEvo:Doc 文件夹不存在,请检查路径:{docFolderPath}");
return null;
}
List<string> result = new List<string>();
// 遍历 Doc 文件夹中的所有 .xlsx 文件
string[] xlsxFiles = Directory.GetFiles(docFolderPath, "*.xlsx");
result = xlsxFiles.Select(file => Path.GetFileNameWithoutExtension(file)).ToList();
return result;
}
private void SetBuildExslNames(string newValue)
{
DocFolderPath = Path.Combine(GetDocFolderPath(), newValue + ".xlsx");
newDataName = newValue;
}
/// <summary>
/// 获取全部作用域名
/// </summary>
public static List<string> GetCreatDomainAllName()
{
string domainPath = $"{Application.dataPath}/Domain";
string[] domains;
// 新增目录获取代码
if (Directory.Exists(domainPath))
{
var dirInfo = new DirectoryInfo(domainPath);
// 获取直接子目录(不递归)
domains = dirInfo.GetDirectories("*", SearchOption.TopDirectoryOnly)
.Select(d => d.Name)
.ToArray();
}
else
{
domains = Array.Empty<string>();
}
return domains.Select(file => Path.GetFileNameWithoutExtension(file)).ToList();
}
private void SetCurrentDomainName(string newValue)
{
Assetpath = Application.dataPath + "/Domain/" + newValue + "/AddressableRes/Config/";
}
}
}