Webgl打包后删除StreamingAssets文件夹下多余资源

csharp 复制代码
该脚本使用环境:
删除StreamingAssets文件夹下多余文件:
例如:当前项目可打包多个岗位的webgl包,但Build当前岗位webgl包中不需要包含其他岗位中的ab和video
相关资源,则需要在打包完成后自动删除无用资源

(int)gameEntry.MEnumProcessType:表示当前岗位的枚举类型
csharp 复制代码
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)
    {
        // 检查是否是 WebGL 平台
        if (report.summary.platform != BuildTarget.WebGL)
        {
            Debug.LogWarning("This processor is designed for WebGL platform only");
            return;
        }

        Debug.Log("WebGL build processor started");

        // 从EditorPrefs获取processType
        int processType = EditorPrefs.GetInt(EDITOR_PREF_KEY, -1);
        // 获取后删除
        EditorPrefs.DeleteKey(EDITOR_PREF_KEY);


        Debug.Log($"Process type: {processType}");

        // 删除对应条件的文件
        DeleteSpecificFiles(report, processType);

        // 删除Video文件夹中不符合条件的文件夹
        CleanVideoFolders(report, processType);

        Debug.Log("WebGL build processor completed");
    }

    private static int GetProcessTypeFromScene()
    {
        // 查找场景中的GameEntry对象
        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
        {
            // 将processType转换为字符串
            string processTypeStr = processType.ToString();

            // WebGL构建的特殊路径处理
            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}"))
                    {
                        // 保留包含正确processType的文件
                        keptCount++;
                        Debug.Log($"Keeping file: {file}");
                    }
                    else
                    {
                        // 删除不包含正确processType的文件
                        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)
    {
        // WebGL构建的特殊路径处理
        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);

            // 如果文件夹名等于processType的字符串表示,则保留
            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)
    {
        // 只在WebGL平台预处理
        if (report.summary.platform != BuildTarget.WebGL)
            return;

        Debug.Log("WebGL build preprocessor started");

        // 从当前场景中获取processType
        int processType = GetProcessTypeFromScene();

        // 保存到EditorPrefs
        EditorPrefs.SetInt(EDITOR_PREF_KEY, processType);

        Debug.Log($"Saved process type: {processType} to EditorPrefs");
    }

    void IPostprocessBuildWithReport.OnPostprocessBuild(BuildReport report)
    {
        OnPostprocessBuild(report);
    }
}
相关推荐
JQLvopkk10 小时前
C# 轻量级工业温湿度监控系统(含数据库与源码)
开发语言·数据库·c#
在路上看风景12 小时前
31. Unity 异步加载的底层细节
unity
wxin_VXbishe12 小时前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php
天人合一peng13 小时前
Unity中做表头时像work中整个调整宽窄
unity
bugcome_com1 天前
零基础入门C#:一篇搞懂核心知识点
c#
小李也疯狂1 天前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的1 天前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y1 天前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤1 天前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里1 天前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎