358 lines
14 KiB
C#
358 lines
14 KiB
C#
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("Evo/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();
|
||
if (result.Tables.Count > 0)
|
||
{
|
||
for (int i = 0; i < result.Tables.Count; i++)
|
||
{
|
||
//开始遍历说有table表
|
||
int oneRows = result.Tables[i].Rows.Count; //行数
|
||
Dictionary<int, int> dict_Question = new Dictionary<int, int>();
|
||
Dictionary<int, int> dict_Menu = new Dictionary<int, int>();
|
||
|
||
#region 通过遍历一遍表格所有行的第一列【序号】不为空
|
||
|
||
int VoiceCount = 0;
|
||
for (int j = 0; j < oneRows; j++)
|
||
{
|
||
string key = FilterTo(result.Tables[i].Rows[j][0].ToString());
|
||
if (!string.IsNullOrEmpty(key))
|
||
{
|
||
VoiceCount++;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region AudioTableData
|
||
|
||
if (result.Tables[i].TableName == "audio")
|
||
{
|
||
#region 遍历表格相应的数据转换成数据
|
||
|
||
List<AudioTableData.MessageInfo> messageInfos = new List<AudioTableData.MessageInfo>();
|
||
for (int j = 1; j < VoiceCount; j++)
|
||
{
|
||
AudioTableData.MessageInfo messageInfo = new AudioTableData.MessageInfo();
|
||
//获取对话块
|
||
messageInfo.index = j - 1;
|
||
//文件名称
|
||
messageInfo.auid = FilterTo(result.Tables[i].Rows[j][0].ToString());
|
||
//获取内容
|
||
messageInfo.autype = Enum.TryParse(FilterTo(result.Tables[i].Rows[j][1].ToString()), out AudioTableData.AudioType auType)? auType : AudioTableData.AudioType.Null;
|
||
|
||
//获取描述
|
||
messageInfo.filename = FilterTo(result.Tables[i].Rows[j][2].ToString());
|
||
|
||
messageInfo.voice = FilterTo(result.Tables[i].Rows[j][3].ToString());
|
||
|
||
messageInfo.trigger = FilterTo(result.Tables[i].Rows[j][4].ToString());
|
||
messageInfo.uirelated =FilterTo(result.Tables[i].Rows[j][5].ToString());
|
||
messageInfo.description = FilterTo(result.Tables[i].Rows[j][6].ToString());
|
||
|
||
messageInfo.format = FilterTo(result.Tables[i].Rows[j][7].ToString());
|
||
messageInfo.notes = FilterTo(result.Tables[i].Rows[j][8].ToString());
|
||
//添加数据
|
||
messageInfos.Add(messageInfo);
|
||
}
|
||
|
||
#endregion
|
||
|
||
ScriptObjectSave<AudioTableData>("AudioTableData", (data) =>
|
||
{
|
||
data.infos = new List<AudioTableData.MessageInfo>();
|
||
data.infos = messageInfos;
|
||
});
|
||
}
|
||
|
||
#endregion
|
||
#region VideoTableData
|
||
|
||
if (result.Tables[i].TableName == "video")
|
||
{
|
||
#region 遍历表格相应的数据转换成数据
|
||
|
||
List<VideoTableData.MessageInfo> messageInfos = new List<VideoTableData.MessageInfo>();
|
||
for (int j = 1; j < VoiceCount; j++)
|
||
{
|
||
VideoTableData.MessageInfo messageInfo = new VideoTableData.MessageInfo();
|
||
//获取对话块
|
||
messageInfo.index = j - 1;
|
||
//文件名称
|
||
messageInfo.vidid = FilterTo(result.Tables[i].Rows[j][0].ToString());
|
||
//获取内容
|
||
messageInfo.filename = FilterTo(result.Tables[i].Rows[j][1].ToString());
|
||
|
||
//获取描述
|
||
messageInfo.vidtype = FilterTo(result.Tables[i].Rows[j][2].ToString());
|
||
|
||
messageInfo.location = FilterTo(result.Tables[i].Rows[j][3].ToString());
|
||
|
||
messageInfo.time = float.TryParse(FilterTo(result.Tables[i].Rows[j][4].ToString()), out float start) ? start : 0;
|
||
messageInfo.subtitle = FilterTo(result.Tables[i].Rows[j][5].ToString()).Equals("是")? true : false;
|
||
messageInfo.notes = FilterTo(result.Tables[i].Rows[j][6].ToString());
|
||
//添加数据
|
||
messageInfos.Add(messageInfo);
|
||
}
|
||
|
||
#endregion
|
||
|
||
ScriptObjectSave<VideoTableData>("VideoTableData", (data) =>
|
||
{
|
||
data.infos = new List<VideoTableData.MessageInfo>();
|
||
data.infos = messageInfos;
|
||
});
|
||
}
|
||
|
||
#endregion
|
||
#region UITableData
|
||
|
||
if (result.Tables[i].TableName == "ui")
|
||
{
|
||
#region 遍历表格相应的数据转换成数据
|
||
|
||
List<UITableData.MessageInfo> messageInfos = new List<UITableData.MessageInfo>();
|
||
for (int j = 1; j < VoiceCount; j++)
|
||
{
|
||
UITableData.MessageInfo messageInfo = new UITableData.MessageInfo();
|
||
//获取对话块
|
||
messageInfo.index = j - 1;
|
||
//文件名称
|
||
messageInfo.uiid = FilterTo(result.Tables[i].Rows[j][0].ToString());
|
||
//获取内容
|
||
messageInfo.filename = FilterTo(result.Tables[i].Rows[j][1].ToString());
|
||
|
||
//获取描述
|
||
messageInfo.uitype = FilterTo(result.Tables[i].Rows[j][2].ToString());
|
||
|
||
messageInfo.node = FilterTo(result.Tables[i].Rows[j][3].ToString());
|
||
|
||
messageInfo.location = FilterTo(result.Tables[i].Rows[j][4].ToString());
|
||
messageInfo.description = FilterTo(result.Tables[i].Rows[j][5].ToString());
|
||
messageInfo.displaycondition = FilterTo(result.Tables[i].Rows[j][6].ToString());
|
||
|
||
messageInfo.animationrequirement = FilterTo(result.Tables[i].Rows[j][7].ToString());
|
||
messageInfo.size = FilterTo(result.Tables[i].Rows[j][8].ToString());
|
||
messageInfo.format = FilterTo(result.Tables[i].Rows[j][9].ToString());
|
||
messageInfo.notes = FilterTo(result.Tables[i].Rows[j][10].ToString());
|
||
//添加数据
|
||
messageInfos.Add(messageInfo);
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
ScriptObjectSave<UITableData>("UITableData", (data) =>
|
||
{
|
||
data.infos = new List<UITableData.MessageInfo>();
|
||
data.infos = messageInfos;
|
||
});
|
||
}
|
||
|
||
#endregion
|
||
#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/";
|
||
}
|
||
}
|
||
} |