{"id":14960453,"url":"https://github.com/zeroultra/excludefrombuild","last_synced_at":"2025-10-24T18:31:45.868Z","repository":{"id":214859112,"uuid":"737516618","full_name":"ZeroUltra/ExcludeFromBuild","owner":"ZeroUltra","description":"Exclude From Build in Unity（打包时排除不要的资源）","archived":false,"fork":false,"pushed_at":"2025-09-01T07:08:40.000Z","size":32,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-01T09:51:38.910Z","etag":null,"topics":["unity3d-plugin"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ZeroUltra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-12-31T10:48:27.000Z","updated_at":"2025-09-01T07:08:44.000Z","dependencies_parsed_at":"2024-03-29T04:27:28.584Z","dependency_job_id":"68451603-4517-4a5d-988d-c55506199b87","html_url":"https://github.com/ZeroUltra/ExcludeFromBuild","commit_stats":{"total_commits":11,"total_committers":2,"mean_commits":5.5,"dds":"0.18181818181818177","last_synced_commit":"3dff08f5afd8f7c9e7155169af386816c6994c5f"},"previous_names":["zeroultra/excludefrombuild"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ZeroUltra/ExcludeFromBuild","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZeroUltra%2FExcludeFromBuild","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZeroUltra%2FExcludeFromBuild/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZeroUltra%2FExcludeFromBuild/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZeroUltra%2FExcludeFromBuild/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZeroUltra","download_url":"https://codeload.github.com/ZeroUltra/ExcludeFromBuild/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZeroUltra%2FExcludeFromBuild/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280844629,"owners_count":26401020,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-24T02:00:06.418Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["unity3d-plugin"],"created_at":"2024-09-24T13:22:19.650Z","updated_at":"2025-10-24T18:31:45.862Z","avatar_url":"https://github.com/ZeroUltra.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n# ExcludeFromBuild\n\n作用：在Build Unity工程的时候排除不参与打包的资源\n\n## 打包过程中经常遇到的问题\n\n* 在打包过程中，是不允许运行时脚本有Editor的代码的（除非使用#if UNITY_EDITOR包裹），但是在开发中经常会写一些测试脚本，经常在Build的时候发生错误，例如下面代码\n\n```c#\nusing System.Collections;\nusing UnityEditor;\nusing UnityEngine;\n\npublic class NewMonoBehaviour : MonoBehaviour\n{\n    private void Start()\n    {\n        AssetDatabase.GetAssetPath(this.gameObject);\n    }\n}\n```\n\n​\t\t（这段代码没有单独创建Assembly Definition）,那么打包的时候就会出现下面错误，错误也很明显，引用了Editor代码\n\n![image-20231231190825083](https://raw.githubusercontent.com/ZeroUltra/MediaLibrary/main/Imgs/202312311908857.png)\n\n当然Unity提供了一种特殊文件方式，用于忽略项目文件\n\n[Unity - Manual: Special folder names (unity3d.com)](https://docs.unity3d.com/Manual/SpecialFolders.html)\n\n- Hidden folders.\n- Files and folders which start with ‘**.**’.\n- Files and folders which end with ‘**~**’.\n- Files and folders named **cvs**.\n- Files with the extension **.tmp**.\n\n利用这种方式可以规避某些不要的文件进入打包。\n\n但是这种方式有两个麻烦点\n\n1. 要手动修改文件（文件夹）格式（这个容易）\n2. 第二就是如果把`文件`修改，如将`Test.cs`-\u003e`Test.cs~`,那么它的meta文件也会消失，就很蛋疼\n\n## 解决方案\n\n步骤：\n\n1. 标记需要排除的资源\n2. 在Build之前将资源和.meta移到`项目Assets之外`，并记录\n3. 打包Build完成或者Build失败之后要还原\n\n## 使用方法\n\n1. 使用UnityPackageManager,添加git项目地址\n2. 导入插件后，标记需要排除的资源（文件或文件夹）\n\n![111](https://raw.githubusercontent.com/ZeroUltra/MediaLibrary/main/Imgs/202312311932079.gif)\n\n![222](https://raw.githubusercontent.com/ZeroUltra/MediaLibrary/main/Imgs/202312311938476.gif)\n\n**鼠标右键-\u003eExclude From Build**，会将资源标记，如果已经标记了再次选择则会排除\n\n*tips:如果你的projects视图也是`two column layout`,不要选左边目录栏，unity将不认*\n\nOK已经完成了所有操作，接下来就可以打包了\n\n## 注意事项\n\n1. 只适用于内置Build，没有测试SBP\n\n2. 打包时只能使用Unity自带得Build（也就是Build Setting窗口得`Build`和`Build And Run`）,因为使用了`BuildPlayerWindow.RegisterBuildPlayerHandler`来获取打包失败回调，具体查看：[Unity - Scripting API: BuildPlayerWindow.RegisterBuildPlayerHandler (unity3d.com)](https://docs.unity3d.com/ScriptReference/BuildPlayerWindow.RegisterBuildPlayerHandler.html)\n\n3. 如果使用自定义代码打包,先注释`ExcludeFormBuilder.cs`代码中`BackupExcludeAssetsBeforeBuild`，然后可以使用类似如下代码，需用`try finally` 防止过程出错\n\n     ![image-20250627142323856](https://raw.githubusercontent.com/ZeroUltra/MediaLibrary/main/Imgs/202507231140597.png)\n\n```c#\n//先备份\nZeroUltra.ExcludeFormBuild.ExcludeFormBuilder.BackupExcludeAssetsBeforeBuild();\ntry\n{\n    //模拟打app \n   var buildReports = BuildPipeline.BuildPlayer(buildPlayerOptions);\n}\ncatch (Exception)\n{\n      throw;\n}\nfinally \n{ \n    //最后还原\n     ZeroUltra.ExcludeFormBuild.ExcludeFormBuilder.RestoreExcludeAssetsAfterBuild();\n}\n```\n\n\n\n4. 由于使用了`IPreprocessBuildWithReport, IPostprocessBuildWithReport`,这两个接口，但是它们在打包AssetBundle不起作用，参考：\n\n   https://docs.unity3d.com/ScriptReference/Build.IPreprocessBuildWithReport.OnPreprocessBuild.html，\n\n   https://docs.unity3d.com/ScriptReference/Build.IPostprocessBuildWithReport.OnPostprocessBuild.html\n\n   所有在打包AssetBundle的时候，也做如下类似修改\n\n```c#\nZeroUltra.ExcludeFormBuild.ExcludeFormBuilder.BackupExcludeAssetsBeforeBuild();\ntry\n{\n    BuildPipeline.BuildAssetBundles(outPath, BuildAssetBundleOptions.None, BuildTarget.Android);\n}\ncatch (Exception)\n{\n      throw;\n}\nfinally \n{ \n     ZeroUltra.ExcludeFormBuild.ExcludeFormBuilder.RestoreExcludeAssetsAfterBuild();\n}\n```\n\n\n\n**有问题欢迎提issues**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeroultra%2Fexcludefrombuild","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzeroultra%2Fexcludefrombuild","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeroultra%2Fexcludefrombuild/lists"}