260 lines
6.8 KiB
C#
260 lines
6.8 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : Kimch
|
|
// Created :
|
|
//
|
|
// Last Modified By : Kimch
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "ExcelPostprocessor" company=""></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
namespace F
|
|
{
|
|
using System.IO;
|
|
using UnityEditor;
|
|
|
|
/// <summary>
|
|
/// ExcelPostprocessor
|
|
/// </summary>
|
|
class ExcelPostprocessor : AssetPostprocessor
|
|
{
|
|
[MenuItem("Game/生成数据类")]
|
|
[MenuItem("Assets/Game/生成数据类")]
|
|
public static void GenerateItemCs()
|
|
{
|
|
const string SAVE_PATH = @"Assets\AGame\Scripts\Item\Static";
|
|
var selectObj = Selection.activeObject;
|
|
if (selectObj)
|
|
{
|
|
var assetPath = AssetDatabase.GetAssetPath(selectObj);
|
|
if (File.Exists(assetPath))
|
|
{
|
|
if (!DecidePathName(assetPath))
|
|
{
|
|
if (CheckExcelExtension(assetPath))
|
|
{
|
|
ExcelToCs(assetPath, SAVE_PATH);
|
|
}
|
|
}
|
|
}
|
|
else if (Directory.Exists(assetPath))
|
|
{
|
|
var files = Directory.GetFiles(assetPath, "*.xlsx");
|
|
float progress = 0f;
|
|
foreach (var file in files)
|
|
{
|
|
if (!DecidePathName(file))
|
|
{
|
|
EditorUtility.DisplayProgressBar("生成数据类", file, progress++ / files.Length);
|
|
ExcelToCs(file, SAVE_PATH);
|
|
}
|
|
}
|
|
EditorUtility.ClearProgressBar();
|
|
}
|
|
else
|
|
{
|
|
// 都不是
|
|
}
|
|
AssetDatabase.Refresh();
|
|
}
|
|
}
|
|
|
|
#region STATIC
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
private static bool DecidePathName(string path)
|
|
{
|
|
var fn = Path.GetFileNameWithoutExtension(path);
|
|
if (fn == "language")
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>Checks the sheet.</summary>
|
|
/// <param name="sheet">The sheet.</param>
|
|
/// <returns></returns>
|
|
private static bool CheckSheetName(string sheet)
|
|
{
|
|
if (!string.IsNullOrEmpty(sheet))
|
|
{
|
|
if (sheet[0] == '_')
|
|
return false;
|
|
if (char.IsUpper(sheet[0]))
|
|
return true;
|
|
UnityEngine.Debug.LogWarning("Sheet name is not support.");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>Gets the file name without extension.</summary>
|
|
/// <param name="path">The path.</param>
|
|
/// <returns></returns>
|
|
private static string GetFileNameWithoutExtension(string path)
|
|
{
|
|
return Path.GetFileNameWithoutExtension(path).ToLower();
|
|
}
|
|
|
|
/// <summary>Gets the text full path.</summary>
|
|
/// <param name="assetPath">The asset path.</param>
|
|
/// <returns></returns>
|
|
private static string GetJsonFullPath(string assetPath)
|
|
{
|
|
string fileName = Path.GetFileNameWithoutExtension(assetPath);
|
|
return Path.Combine("Assets/AGame/Configs/Client", fileName + ".json");
|
|
}
|
|
|
|
private static string GetJsonFullPath2(string assetPath)
|
|
{
|
|
string fileName = Path.GetFileNameWithoutExtension(assetPath);
|
|
return Path.Combine("Assets/AGame/Configs/Server", fileName + ".json");
|
|
}
|
|
|
|
/// <summary>Gets the text full path.</summary>
|
|
/// <param name="assetPath">The asset path.</param>
|
|
/// <returns></returns>
|
|
private static string GetXmlFullPath(string assetPath)
|
|
{
|
|
string fileName = Path.GetFileNameWithoutExtension(assetPath);
|
|
|
|
return Path.ChangeExtension(assetPath, ".xml").ToLower();
|
|
}
|
|
|
|
private static bool CheckExcelExtension(string fileName)
|
|
{
|
|
var fn = Path.GetFileName(fileName);
|
|
var ext = Path.GetExtension(fn);
|
|
return ".xlsx" == ext && !fn.StartsWith("~$");
|
|
}
|
|
|
|
private static bool CheckJsonExtension(string fileName)
|
|
{
|
|
var ext = Path.GetExtension(fileName);
|
|
return ".json" == ext;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对齐列
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
private static void ExcelToJson(string fileName)
|
|
{
|
|
if (EditorApplication.isPlaying)
|
|
return;
|
|
|
|
string saveFilePath = GetJsonFullPath(fileName);
|
|
if (DecidePathName(fileName))
|
|
ExcelUtils.ExcelToJson1(fileName, saveFilePath, true, CheckSheetName);
|
|
else
|
|
ExcelUtils.ExcelToJson1(fileName, saveFilePath, false, CheckSheetName);
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 标准json格式
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
private static void ExcelToJson2(string fileName)
|
|
{
|
|
string saveFilePath = GetJsonFullPath2(fileName);
|
|
ExcelUtils.ExcelToJson2(fileName, saveFilePath, CheckSheetName);
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
/// <summary>
|
|
/// xml
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
public static void ExcelToXml(string fileName)
|
|
{
|
|
var saveFilePath = Path.GetDirectoryName(fileName);
|
|
ExcelUtils.ExcelToXml(fileName, saveFilePath, CheckSheetName);
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="savePath"></param>
|
|
public static void ExcelToCs(string fileName, string savePath)
|
|
{
|
|
ExcelUtils.ExcelToCs1(fileName, savePath, CheckSheetName, "G", "Item", "", "Item, IItem", "", false, false);
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
public static bool VerificationJson(string fileName)
|
|
{
|
|
var jsonText = File.ReadAllText(fileName);
|
|
try
|
|
{
|
|
EditorJsonUtility.FromJsonOverwrite(jsonText, "");
|
|
UnityEngine.Debug.Log($"<color={"lime"}>[{Path.GetFileNameWithoutExtension(fileName)}] 格式正确.</color>");
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
UnityEngine.Debug.Log($"<color={"red"}>[{Path.GetFileNameWithoutExtension(fileName)}] 格式错误.</color>");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region UNITY
|
|
|
|
/// <summary>Called when [postprocess all assets].</summary>
|
|
/// <param name="importedAssets">The imported assets.</param>
|
|
/// <param name="deletedAssets">The deleted assets.</param>
|
|
/// <param name="movedAssets">The moved assets.</param>
|
|
/// <param name="movedFromAssetPaths">The moved from asset paths.</param>
|
|
public static void OnPostprocessAllAssets(
|
|
string[] importedAssets,
|
|
string[] deletedAssets,
|
|
string[] movedAssets,
|
|
string[] movedFromAssetPaths)
|
|
{
|
|
foreach (var importedAsset in importedAssets)
|
|
{
|
|
if (CheckExcelExtension(importedAsset))
|
|
{
|
|
ExcelToJson(importedAsset);
|
|
}
|
|
else if (CheckJsonExtension(importedAsset))
|
|
{
|
|
VerificationJson(importedAsset);
|
|
}
|
|
}
|
|
|
|
foreach (var deletedAsset in deletedAssets)
|
|
{
|
|
if (CheckExcelExtension(deletedAsset))
|
|
{
|
|
string txtFile = GetJsonFullPath(deletedAsset);
|
|
AssetDatabase.DeleteAsset(txtFile);
|
|
AssetDatabase.Refresh();
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < movedAssets.Length; i++)
|
|
{
|
|
if (CheckExcelExtension(movedAssets[i]))
|
|
{
|
|
AssetDatabase.RenameAsset(GetJsonFullPath(movedFromAssetPaths[i]), GetFileNameWithoutExtension(movedAssets[i]));
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |