126 lines
4.7 KiB
C#
126 lines
4.7 KiB
C#
/*
|
|
* ImportMonsterPackageAndClean
|
|
*
|
|
* 1) 导入工程根下的 monster.unitypackage / role.unitypackage (非交互式)
|
|
* 2) 导入完成回调里自动清理夹带的 GameClient 代码/插件冗余
|
|
* 只保留: Assets/Res (怪/特效/玩家)
|
|
* 清理: Code/Scripts/Editor/PSD2UGUI/Tools/ThirdParty + Plugins 多余子目录
|
|
*/
|
|
|
|
using System.IO;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace OctoberStudio.EditorTools
|
|
{
|
|
public static class ImportMonsterPackageAndClean
|
|
{
|
|
[MenuItem("Tools/OctoberStudio/VAT/Import Role Package Auto Clean", priority = 2)]
|
|
public static void ImportRoleAndClean() => ImportAndCleanInternal("role.unitypackage");
|
|
|
|
[MenuItem("Tools/OctoberStudio/VAT/Import Monster Package Auto Clean", priority = 1)]
|
|
public static void ImportAndClean() => ImportAndCleanInternal("monster.unitypackage");
|
|
|
|
// 整目录删除清单 (导入后立刻删,只删 unitypackage 夹带,不动 Survivors 自有)
|
|
private static readonly string[] DeleteAfterImport = new[]
|
|
{
|
|
"Assets/Code",
|
|
"Assets/Scripts",
|
|
"Assets/Editor",
|
|
"Assets/PSD2UGUI",
|
|
"Assets/Tools",
|
|
"Assets/ThirdParty",
|
|
// unitypackage 新加的 Plugins 子目录
|
|
"Assets/Plugins/Android",
|
|
"Assets/Plugins/BuglyPlugins",
|
|
"Assets/Plugins/Com",
|
|
"Assets/Plugins/DOTween",
|
|
"Assets/Plugins/IceMark",
|
|
"Assets/Plugins/SharpZipLib",
|
|
"Assets/Plugins/TexturePacker",
|
|
"Assets/Plugins/YangStudio",
|
|
"Assets/Plugins/Sirenix", // unitypackage 如果带了 Odin 也删
|
|
"Assets/Plugins/Demigiant", // 同样防御
|
|
"Assets/Plugins/YIUIFramework", // 防御
|
|
// role.unitypackage 夹带的额外目录
|
|
"Assets/Bundles",
|
|
"Assets/GameRes",
|
|
"Assets/Hotfix",
|
|
"Assets/HybridCLRData",
|
|
};
|
|
|
|
private static void ImportAndCleanInternal(string packageFileName)
|
|
{
|
|
var projRoot = Path.GetDirectoryName(Application.dataPath);
|
|
var pkg = Path.Combine(projRoot, packageFileName);
|
|
if (!File.Exists(pkg))
|
|
{
|
|
Debug.LogError($"[ImportPackage] 找不到 {packageFileName}: {pkg}");
|
|
return;
|
|
}
|
|
|
|
AssetDatabase.importPackageCompleted -= OnImportDone;
|
|
AssetDatabase.importPackageCompleted += OnImportDone;
|
|
AssetDatabase.importPackageFailed -= OnImportFailed;
|
|
AssetDatabase.importPackageFailed += OnImportFailed;
|
|
AssetDatabase.importPackageCancelled -= OnImportCancelled;
|
|
AssetDatabase.importPackageCancelled += OnImportCancelled;
|
|
|
|
Debug.Log($"[ImportPackage] 开始导入: {pkg}");
|
|
AssetDatabase.ImportPackage(pkg, false);
|
|
}
|
|
|
|
private static void OnImportDone(string packageName)
|
|
{
|
|
Debug.Log($"[ImportPackage] 导入完成: {packageName}. 开始自动清理冗余...");
|
|
Detach();
|
|
EditorApplication.delayCall += DoClean;
|
|
}
|
|
|
|
private static void OnImportFailed(string packageName, string errorMessage)
|
|
{
|
|
Debug.LogError($"[ImportPackage] 导入失败: {packageName}\n{errorMessage}");
|
|
Detach();
|
|
}
|
|
|
|
private static void OnImportCancelled(string packageName)
|
|
{
|
|
Debug.LogWarning($"[ImportPackage] 导入取消: {packageName}");
|
|
Detach();
|
|
}
|
|
|
|
private static void Detach()
|
|
{
|
|
AssetDatabase.importPackageCompleted -= OnImportDone;
|
|
AssetDatabase.importPackageFailed -= OnImportFailed;
|
|
AssetDatabase.importPackageCancelled -= OnImportCancelled;
|
|
}
|
|
|
|
private static void DoClean()
|
|
{
|
|
int ok = 0;
|
|
long savedBytes = 0;
|
|
foreach (var path in DeleteAfterImport)
|
|
{
|
|
if (!AssetDatabase.IsValidFolder(path)) continue;
|
|
long size = 0;
|
|
try
|
|
{
|
|
foreach (var f in Directory.GetFiles(Path.GetFullPath(path), "*", SearchOption.AllDirectories))
|
|
size += new FileInfo(f).Length;
|
|
}
|
|
catch { }
|
|
|
|
if (AssetDatabase.DeleteAsset(path))
|
|
{
|
|
ok++;
|
|
savedBytes += size;
|
|
Debug.Log($"[Clean] 删除 {path} (-{size / 1024 / 1024} MB)");
|
|
}
|
|
}
|
|
AssetDatabase.Refresh();
|
|
Debug.Log($"[Clean] 完成 — 删除 {ok} 目录,释放 {savedBytes / 1024f / 1024f:F1} MB");
|
|
}
|
|
}
|
|
}
|