该脚本使用环境:
删除StreamingAssets文件夹下多余文件:
例如:当前项目可打包多个岗位的webgl包,但Build当前岗位webgl包中不需要包含其他岗位中的ab和video
相关资源,则需要在打包完成后自动删除无用资源
(int)gameEntry.MEnumProcessType:表示当前岗位的枚举类型
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
public class WebGLBuildProcessor: IPostprocessBuildWithReport, IPreprocessBuildWithReport
{
public static void OnPostprocessBuild(BuildReport report)
{
if (report.summary.platform != BuildTarget.WebGL)
{
Debug.LogWarning("This processor is designed for WebGL platform only");
return;
}
Debug.Log("WebGL build processor started");
int processType = EditorPrefs.GetInt(EDITOR_PREF_KEY, -1);
EditorPrefs.DeleteKey(EDITOR_PREF_KEY);
Debug.Log($"Process type: {processType}");
DeleteSpecificFiles(report, processType);
CleanVideoFolders(report, processType);
Debug.Log("WebGL build processor completed");
}
private static int GetProcessTypeFromScene()
{
GameEntry gameEntry = Object.FindObjectOfType<GameEntry>();
if (gameEntry != null)
{
Debug.Log($"Found GameEntry with process type: {gameEntry.MEnumProcessType}");
return (int)gameEntry.MEnumProcessType;
}
Debug.LogWarning("未找到GameEntry对象");
return -1;
}
private static void DeleteSpecificFiles(BuildReport report, int processType)
{
try
{
string processTypeStr = processType.ToString();
string rootPath = Path.Combine(
report.summary.outputPath
);
Debug.Log($"Searching in: {rootPath}");
string[] allFiles = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);
int deletedCount = 0;
int keptCount = 0;
foreach (string file in allFiles)
{
string fileName = Path.GetFileName(file);
if (fileName.Contains($"anli_"))
{
if (fileName.Contains($"anli_{processTypeStr}"))
{
keptCount++;
Debug.Log($"Keeping file: {file}");
}
else
{
File.Delete(file);
Debug.Log($"Deleted file: {file}");
deletedCount++;
}
}
}
Debug.Log($"共删除了 {deletedCount} 个文件,保留了 {keptCount} 个文件");
}
catch (System.Exception e)
{
Debug.LogError($"删除文件时出错: {e.Message}");
}
}
private static void CleanVideoFolders(BuildReport report, int processType)
{
string videoFolderPath = Path.Combine(
report.summary.outputPath,
"StreamingAssets",
"Video"
);
Debug.Log($"Checking video folder: {videoFolderPath}");
if (!Directory.Exists(videoFolderPath))
{
Debug.LogWarning($"Video文件夹不存在: {videoFolderPath}");
return;
}
string[] folders = Directory.GetDirectories(videoFolderPath);
int deletedCount = 0;
int keptCount = 0;
foreach (string folder in folders)
{
string folderName = Path.GetFileName(folder);
if (folderName == processType.ToString())
{
keptCount++;
Debug.Log($"Keeping folder: {folder}");
}
else
{
Directory.Delete(folder, true);
Debug.Log($"Deleted folder: {folder}");
deletedCount++;
}
}
Debug.Log($"共删除了 {deletedCount} 个文件夹,保留了 {keptCount} 个文件夹");
}
public int callbackOrder { get; }
private static string EDITOR_PREF_KEY = "WebGLBuildProcessor_ProcessType";
public void OnPreprocessBuild(BuildReport report)
{
if (report.summary.platform != BuildTarget.WebGL)
return;
Debug.Log("WebGL build preprocessor started");
int processType = GetProcessTypeFromScene();
EditorPrefs.SetInt(EDITOR_PREF_KEY, processType);
Debug.Log($"Saved process type: {processType} to EditorPrefs");
}
void IPostprocessBuildWithReport.OnPostprocessBuild(BuildReport report)
{
OnPostprocessBuild(report);
}
}