├── README.md
└── BuildStripper.cs
/README.md:
--------------------------------------------------------------------------------
1 | [](http://www.stephenallengames.co.uk/games.php)
2 |
3 | [](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=9PUGQGE4XDE4C¤cy_code=GBP)
4 |
5 | # BuildStripper
6 | Build Stripper gets the build pipeline to totally ignore select folders, and reverts back after.
7 |
8 | Version 1.1 Credits:
9 |
10 | [JesusLuvsYooh](https://github.com/JesusLuvsYooh) StephenAllenGames.co.uk
11 | #WaterCooler gang for letting me Rubber Duck to them.
12 |
13 | 1: This file must be in an "Editor" folder (Unity/Assets/Editor) for example.
14 |
15 | 2: Edit the "folderPaths" list, making sure it only includes items that server build does not need, such as Audio and Textures.
16 |
17 | 3: If "Server Build" is ticked in Unitys build window, automatically strip folders listed during build process.
18 |
19 | This script should drastically lower your build size, and optimise your game;
20 |
21 | Build size differences in an example project:
22 | Regular server build: 1 GB
23 | An un-named Asset store plugin: 200 MB
24 | This BuildStripper: 60mb
25 |
26 | Image preview of build process.
27 | 
28 |
29 | A: Optional Unity Menu buttons, for manually calling, just un-comment them in the script.
30 | B: Be smart with which folders you list, ones like Scenes or Scripts etc will of course stop server/game from working. ;)
31 | C: Set autoDetectServerBuild to false to disable auto "Server Build" detection.
32 | D: Please be aware this is version 1, an Alpha/Beta, code needs to be optimised and tidied up.
33 |
34 | Enjoy! ^_^
35 |
36 |
37 |
--------------------------------------------------------------------------------
/BuildStripper.cs:
--------------------------------------------------------------------------------
1 | // JesusLuvsYooh StephenAllenGames.co.uk
2 | // https://github.com/JesusLuvsYooh/BuildStripper
3 | // Version 1.1
4 |
5 | using UnityEditor;
6 | using UnityEditor.Build;
7 | using UnityEditor.Build.Reporting;
8 | using UnityEngine;
9 |
10 | class BuildStripper : IPreprocessBuildWithReport
11 | {
12 | // - CHANGE -
13 | // If true, automatically sets buildStrippedServer = true If "Server Build" is ticked in Unitys build window.
14 | private bool autoDetectServerBuild = true;
15 | // Do not change, use the above boolean. Only Overwrite if you want to strip from a non "Server Build".
16 | private bool buildStrippedServer = false;
17 |
18 | // - CHANGE -
19 | // Example folders that are not needed on headless server build
20 | private string[] folderPaths = new string[] {
21 | "AddressablesFolder",
22 | "Animations",
23 | "Models",
24 | "Plugins",
25 | "Resources",
26 | "Scenes/Map3-Volcano", // light map folder, not the scene file itself
27 | "Shaders",
28 | "Sounds",
29 | "StreamingAssets",
30 | "Textures"
31 | };
32 |
33 | public int callbackOrder { get { return 0; } }
34 | private string stringValue;
35 |
36 | public void OnPreprocessBuild(BuildReport report)
37 | {
38 | if (autoDetectServerBuild && report.summary.options.HasFlag(BuildOptions.EnableHeadlessMode)) { buildStrippedServer = true; }
39 |
40 | if (buildStrippedServer)
41 | {
42 | EditorApplication.update += BuildCheck;
43 | StripBuild();
44 | UnityEngine.Debug.Log("OnPreprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath);
45 | }
46 | }
47 |
48 | public void OnPostprocessBuild(BuildReport report)
49 | {
50 | if (buildStrippedServer)
51 | {
52 | RevertStripBuild();
53 | UnityEngine.Debug.Log("OnPostprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath);
54 | }
55 | }
56 |
57 | private void StripBuild()
58 | {
59 | foreach (string _value in folderPaths)
60 | {
61 | AssetDatabase.MoveAsset("Assets/" + _value, "Assets/" + _value + "~");
62 | }
63 | }
64 |
65 | private void RevertStripBuild()
66 | {
67 | foreach (string _value in folderPaths)
68 | {
69 | stringValue = _value;
70 | //end character is used instead of "Replace", just incase people have "~" symbol throughout folder/file names.
71 | if (stringValue.Substring(stringValue.Length - 1) == "~") { stringValue = stringValue.Substring(0, stringValue.Length - 1); }
72 |
73 | AssetDatabase.MoveAsset("Assets/" + _value + "~", "Assets/" + _value);
74 | }
75 | }
76 |
77 | // We run this check to detect build failures or cancellations, to then apply the Revert function.
78 | private void BuildCheck()
79 | {
80 | // UnityEngine.Debug.Log("BuildCheck " + BuildPipeline.isBuildingPlayer);
81 | if (!BuildPipeline.isBuildingPlayer)
82 | {
83 | EditorApplication.update -= BuildCheck;
84 | RevertStripBuild();
85 | }
86 | }
87 |
88 |
89 | /*
90 | [MenuItem("File/Setup Stripped Folders", priority = 1)]
91 | public static void ButtonStrippedServer()
92 | {
93 | UnityEngine.Debug.Log("Button Stripped Server called.");
94 | StripBuild();
95 | }
96 | [MenuItem("File/Revert Stripped Folders", priority = 1)]
97 | public static void ButtonStrippedServerRevert()
98 | {
99 | UnityEngine.Debug.Log("Button Revert Stripped Server called.");
100 | RevertStripBuild();
101 | }
102 | */
103 | }
104 |
--------------------------------------------------------------------------------