Skip to content

Instantly share code, notes, and snippets.

@radiatoryang
Created December 11, 2022 00:58
Show Gist options
  • Save radiatoryang/6330d9f5feb83d09ae63162a59422b09 to your computer and use it in GitHub Desktop.
Save radiatoryang/6330d9f5feb83d09ae63162a59422b09 to your computer and use it in GitHub Desktop.
Fixed version of Unity WebGL dual build editor script example (https://docs.unity3d.com/2022.2/Documentation/Manual/webgl-texture-compression.html) which lets you make WebGL builds with both DXT and ASTC compressed textures... Don't forget to update your WebGL template HTML too.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
public class BuildWebGLDual
{
[MenuItem("Build/WebGL Dual Build")]
public static void BuildGame()
{
// based on https://docs.unity3d.com/2022.2/Documentation/Manual/webgl-texture-compression.html
// This builds the player twice: a build with desktop-specific texture settings (WebGL_Build)
// as well as mobile-specific texture settings (WebGL_Mobile), and combines the necessary files into one directory (WebGL_Build)
// Don't forget to edit your WebGL player template HTML to choose the right data bundle.
string dualBuildPath = EditorUtility.SaveFolderPanel("Choose location of WebGL builds", "", "");
if (!Directory.Exists(dualBuildPath)) {
Directory.CreateDirectory(dualBuildPath);
}
string desktopBuildName = "WebGL_Build";
string mobileBuildName = "WebGL_Mobile";
string desktopPath = Path.Combine(dualBuildPath, desktopBuildName);
string mobilePath = Path.Combine(dualBuildPath, mobileBuildName);
string dataFileExt = ".data";
switch(PlayerSettings.WebGL.compressionFormat) {
case WebGLCompressionFormat.Gzip:
dataFileExt = ".data.gz";
break;
case WebGLCompressionFormat.Brotli:
dataFileExt = ".data.br";
break;
default:
break;
}
var buildOptions = new BuildPlayerOptions();
buildOptions.scenes = GetScenePaths();
buildOptions.target = BuildTarget.WebGL;
buildOptions.options = BuildOptions.None;
// build mobile
buildOptions.locationPathName = mobilePath;
EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.ASTC;
buildOptions.subtarget = (int)WebGLTextureSubtarget.ASTC;
BuildPipeline.BuildPlayer(buildOptions);
// build desktop
buildOptions.locationPathName = desktopPath;
buildOptions.options = BuildOptions.ShowBuiltPlayer;
EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.DXT;
buildOptions.subtarget = (int)WebGLTextureSubtarget.DXT;
BuildPipeline.BuildPlayer(buildOptions);
// Copy the mobile.data file to the desktop build directory to consolidate them both
FileUtil.CopyFileOrDirectory(Path.Combine(mobilePath, "Build", mobileBuildName + dataFileExt), Path.Combine(desktopPath, "Build", mobileBuildName + dataFileExt));
}
static string[] GetScenePaths()
{
string[] scenes = new string[EditorBuildSettings.scenes.Length];
for (int i = 0; i < scenes.Length; i++)
{
scenes[i] = EditorBuildSettings.scenes[i].path;
}
return scenes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment