Skip to content

Commit 6c70cb1

Browse files
committed
Add submodule stripping to minsize builds
1 parent d7bc782 commit 6c70cb1

File tree

3 files changed

+109
-76
lines changed

3 files changed

+109
-76
lines changed

Assets/DefaultSubmoduleStrippingSettings.asset

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ MonoBehaviour:
1313
m_Name: DefaultSubmoduleStrippingSettings
1414
m_EditorClassIdentifier:
1515
OptimizeCodeAfterStripping: 1
16-
RemoveEmbeddedDebugSymbols: 0
17-
MissingSubmoduleErrorHandling: 2
16+
RemoveEmbeddedDebugSymbols: 1
17+
MissingSubmoduleErrorHandling: 1
1818
SubmodulesToStrip:
1919
- 2D Rendering
2020
- Freetype2
21-
- GPU Instancing
2221
- IMGUI
23-
- ParticleSystem
2422
- System.Xml
2523
- TetGen
24+
- Text Rendering
25+
- Texture Decompression BC
26+
- Texture Decompression ETC
27+
- Texture Decompression Crunch
2628
- UI Toolkit

Assets/Scripts/Editor/BuildScript.cs

Lines changed: 102 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
using UnityEditor.Compilation;
2020
using UnityEngine;
2121
using UnityEngine.Rendering;
22+
#if UNITY_6000_1_OR_NEWER
23+
using Unity.Web.Stripping.Editor;
24+
#endif
2225

2326
namespace UnityBuilderAction
2427
{
@@ -118,112 +121,140 @@ public static void Build(string[] args)
118121
if (options.TryGetValue("tag", out string tagVersion) &&
119122
!string.IsNullOrEmpty(tagVersion))
120123
{
121-
string[] tagParameters = tagVersion.Split('-');
122-
if (tagParameters.Contains("minsize"))
123-
{
124-
PlayerSettings.WebGL.template = "PROJECT:Release";
125-
buildPlayerOptions.options |= BuildOptions.CompressWithLz4HC;
126-
PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.None;
127-
SetWebGlOptimization(CodeOptimizationSize);
124+
HandleTagParameters(tagVersion, namedBuildTarget);
125+
}
126+
127+
break;
128+
}
129+
130+
// Additional options for local builds
131+
if (!Application.isBatchMode)
132+
{
133+
if (options.TryGetValue("autorunplayer", out string _))
134+
{
135+
buildPlayerOptions.options |= BuildOptions.AutoRunPlayer;
136+
}
137+
138+
var projectPath = Application.dataPath.Substring(0, Application.dataPath.Length - "/Assets".Length);
139+
BackupLastBuild($"{projectPath}/{options["customBuildPath"]}");
140+
}
141+
142+
errorLogMessages = new List<string>();
143+
Application.logMessageReceived += OnLogMessageReceived;
144+
145+
// Custom build
146+
Build(buildTarget, options["customBuildPath"]);
147+
}
148+
149+
private static void HandleTagParameters(string tagVersion, NamedBuildTarget namedBuildTarget)
150+
{
151+
string[] tagParameters = tagVersion.Split('-');
152+
if (tagParameters.Contains("minsize"))
153+
{
154+
PlayerSettings.WebGL.template = "PROJECT:Release";
155+
buildPlayerOptions.options |= BuildOptions.CompressWithLz4HC;
156+
PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.None;
157+
SetWebGlOptimization(CodeOptimizationSize);
128158
#if UNITY_2022_1_OR_NEWER
129-
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSize);
159+
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSize);
130160
#endif
131161
#if UNITY_2021_2_OR_NEWER
132-
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Master);
162+
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Master);
133163
#else
134164
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Master);
135165
#endif
136-
}
137-
else if (tagParameters.Contains("debug"))
138-
{
139-
PlayerSettings.WebGL.template = "PROJECT:Develop";
140-
PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.FullWithStacktrace;
141-
// For debug builds this setting will always be build times no matter what is set, setting this more as a documentation of the behavior
142-
SetWebGlOptimization(CodeOptimizationBuildTimes);
166+
}
167+
else if (tagParameters.Contains("debug"))
168+
{
169+
PlayerSettings.WebGL.template = "PROJECT:Develop";
170+
PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.FullWithStacktrace;
171+
// For debug builds this setting will always be build times no matter what is set, setting this more as a documentation of the behavior
172+
SetWebGlOptimization(CodeOptimizationBuildTimes);
143173
#if UNITY_2022_1_OR_NEWER
144-
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSize);
174+
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSize);
145175
#endif
146176
#if UNITY_2021_2_OR_NEWER
147-
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Debug);
148-
PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Embedded;
177+
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Debug);
178+
PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Embedded;
149179
#else
150180
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Debug);
151181
PlayerSettings.WebGL.debugSymbols = true;
152182
#endif
153183

154184
#if UNITY_2022_2_OR_NEWER
155-
PlayerSettings.WebGL.showDiagnostics = true;
185+
PlayerSettings.WebGL.showDiagnostics = true;
156186
#endif
157-
buildPlayerOptions.options |= BuildOptions.Development;
158-
}
159-
else
160-
{
161-
PlayerSettings.WebGL.template = "PROJECT:Develop";
162-
// By default use the speed setting
163-
SetWebGlOptimization(CodeOptimizationSpeed);
187+
buildPlayerOptions.options |= BuildOptions.Development;
188+
}
189+
else
190+
{
191+
PlayerSettings.WebGL.template = "PROJECT:Develop";
192+
// By default use the speed setting
193+
SetWebGlOptimization(CodeOptimizationSpeed);
164194
#if UNITY_2022_1_OR_NEWER
165-
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSpeed);
195+
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSpeed);
166196
#endif
167197
#if UNITY_2021_2_OR_NEWER
168-
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Master);
198+
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Master);
169199
#else
170200
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Master);
171201
#endif
172-
}
202+
}
203+
204+
HandleSubmoduleStrippingParameters(tagParameters);
205+
SetGraphicsApi(tagParameters);
206+
}
207+
208+
private static void HandleSubmoduleStrippingParameters(string[] tagParameters)
209+
{
210+
#if UNITY_6000_1_OR_NEWER
211+
bool enableStripping = tagParameters.Contains("minsize") || tagParameters.Contains("stripping");
212+
PlayerSettings.WebGL.enableSubmoduleStrippingCompatibility = enableStripping;
213+
214+
if (enableStripping)
215+
{
216+
PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Embedded;
217+
}
218+
StrippingProjectSettings.StripAutomaticallyAfterBuild = enableStripping;
219+
Log("Web submodule stripping is set to " + enableStripping);
220+
#else
221+
Log("Skipping Web submodule stripping since is not supported in this Unity version");
222+
#endif
223+
}
173224

174-
List<GraphicsDeviceType> graphicsAPIs = new List<GraphicsDeviceType>();
175-
if (tagParameters.Contains("webgl1"))
176-
{
225+
private static void SetGraphicsApi(string[] tagParameters)
226+
{
227+
List<GraphicsDeviceType> graphicsAPIs = new List<GraphicsDeviceType>();
228+
if (tagParameters.Contains("webgl1"))
229+
{
177230
#if !UNITY_2023_1_OR_NEWER
178231
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES2);
179232
#else
180-
LogWarning("WebGL1 not supported anymore, choosing WebGL2 instead");
181-
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES3);
233+
LogWarning("WebGL1 not supported anymore, choosing WebGL2 instead");
234+
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES3);
182235
#endif
183-
}
184-
if(tagParameters.Contains("webgl2"))
185-
{
186-
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES3);
187-
}
188-
if(tagParameters.Contains("webgpu"))
189-
{
236+
}
237+
if(tagParameters.Contains("webgl2"))
238+
{
239+
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES3);
240+
}
241+
if(tagParameters.Contains("webgpu"))
242+
{
190243
#if UNITY_2023_2_OR_NEWER
191-
graphicsAPIs.Add(GraphicsDeviceType.WebGPU);
192-
#if UNITY_6000_0_OR_NEWER
193-
// Enable wasm2023 for WebGPU, since if webGPU is supported everything from 2023 is supported as well
194-
PlayerSettings.WebGL.wasm2023 = true;
195-
#endif
244+
graphicsAPIs.Add(GraphicsDeviceType.WebGPU);
245+
#if UNITY_6000_0_OR_NEWER
246+
// Enable wasm2023 for WebGPU, since if webGPU is supported everything from 2023 is supported as well
247+
PlayerSettings.WebGL.wasm2023 = true;
248+
#endif
196249
#else
197250
LogError("WebGPU not supported yet");
198251
#endif
199-
}
200-
201-
PlayerSettings.SetGraphicsAPIs(BuildTarget.WebGL, graphicsAPIs.ToArray());
202-
}
203-
204-
break;
205252
}
206253

207-
// Additional options for local builds
208-
if (!Application.isBatchMode)
209-
{
210-
if (options.TryGetValue("autorunplayer", out string _))
211-
{
212-
buildPlayerOptions.options |= BuildOptions.AutoRunPlayer;
213-
}
214-
215-
var projectPath = Application.dataPath.Substring(0, Application.dataPath.Length - "/Assets".Length);
216-
BackupLastBuild($"{projectPath}/{options["customBuildPath"]}");
217-
}
218-
219-
errorLogMessages = new List<string>();
220-
Application.logMessageReceived += OnLogMessageReceived;
221-
222-
// Custom build
223-
Build(buildTarget, options["customBuildPath"]);
254+
PlayerSettings.SetGraphicsAPIs(BuildTarget.WebGL, graphicsAPIs.ToArray());
224255
}
225256

226-
private static void OnLogMessageReceived(string logString, string stackTrace, LogType type)
257+
private static void OnLogMessageReceived(string logString, string stackTrace, LogType type)
227258
{
228259
if(type == LogType.Error || type == LogType.Exception)
229260
{

ProjectSettings/Packages/com.unity.web.stripping-tool/Settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{
1010
"type": "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
1111
"key": "StrippingProjectSettings.ActiveSettings.AssetData",
12-
"value": "{\"m_Value\":\"{\\\"MonoBehaviour\\\":{\\\"m_Enabled\\\":true,\\\"m_EditorHideFlags\\\":0,\\\"m_Name\\\":\\\"DefaultSubmoduleStrippingSettings\\\",\\\"m_EditorClassIdentifier\\\":\\\"\\\",\\\"OptimizeCodeAfterStripping\\\":false,\\\"RemoveEmbeddedDebugSymbols\\\":false,\\\"MissingSubmoduleErrorHandling\\\":0,\\\"SubmodulesToStrip\\\":[]}}\"}"
12+
"value": "{\"m_Value\":\"{\\\"MonoBehaviour\\\":{\\\"m_Enabled\\\":true,\\\"m_EditorHideFlags\\\":0,\\\"m_Name\\\":\\\"DefaultSubmoduleStrippingSettings\\\",\\\"m_EditorClassIdentifier\\\":\\\"\\\",\\\"OptimizeCodeAfterStripping\\\":true,\\\"RemoveEmbeddedDebugSymbols\\\":true,\\\"MissingSubmoduleErrorHandling\\\":1,\\\"SubmodulesToStrip\\\":[\\\"2D Rendering\\\",\\\"Freetype2\\\",\\\"IMGUI\\\",\\\"System.Xml\\\",\\\"TetGen\\\",\\\"Text Rendering\\\",\\\"Texture Decompression BC\\\",\\\"Texture Decompression ETC\\\",\\\"Texture Decompression Crunch\\\",\\\"UI Toolkit\\\"]}}\"}"
1313
},
1414
{
1515
"type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",

0 commit comments

Comments
 (0)