Unity 发布WebGL的压缩构建和服务器配置

发布于:2025-07-23 ⋅ 阅读:(14) ⋅ 点赞:(0)

WebGL:压缩构建和服务器配置

要部署 WebGL 构建,必须配置服务器并确保使用正确的响应标头,以便浏览器可以接收正确的响应并正确处理响应。

Unity 中有两个主要设置会影响服务器的设置方式:

  1. Compression Format:确定 Unity 在构建步骤中如何压缩文件。
  2. Decompression Fallback:确定当构建在浏览器中运行时 Unity 如何处理下载的文件。

Compression Format

在这里插入图片描述

压缩方法 描述
gzip 这是默认选项。gzip 文件比 Brotli 文件更大,但构建速度更快,且所有浏览器都基于 HTTP 和 HTTPS 实现此格式的本机支持。
Brotli Brotli 压缩提供最佳压缩比。Brotli 压缩文件小于 gzip,但需要更长的压缩时间,因此会增加发布版本的迭代时间。Chrome 和 Firefox 仅原生支持基于 HTTPS 的 Brotli 压缩。
Disabled 禁用压缩。如果要在后期处理脚本中实现您自己的压缩,请使用此选项。如果计划在托管服务器上使用静态压缩,也应该使用此选项。

Decompression Fallback

在这里插入图片描述

解压缩回退选项使 Unity 能够自动将 JavaScript 解压缩器嵌入到您的构建中。该解压缩器与您选择的压缩方法相对应,它在浏览器无法解压缩内容时执行解压缩。

启用解压缩回退

从 Player Settings 窗口启用解压缩回退(菜单:Edit > Project Settings > Player,然后选择 WebGL 并展开 Publishing Settings 部分):

如果启用解压缩回退,Unity 会向构建文件添加一个 .unityweb 扩展名。 如果不熟悉服务器配置,或者无法使用服务器配置,应考虑使用 Decompression Fallback。

注意:使用此选项会导致加载器大小增大和构建文件加载方案的效率降低

禁用解压缩回退

默认情况下,Decompression Fallback 选项处于禁用状态。因此,默认情况下,构建文件的扩展名与您选择的压缩方法相对应。

有两种压缩方法可供选择:gzip 或 Brotli。

要让浏览器在 Unity 构建文件下载时进行原生解压缩,需要配置 Web 服务器以使用适当的 HTTP 标头提供压缩文件。这称为原生浏览器解压缩。它的优点是比 JavaScript 解压缩回退更快,可以减少应用程序的启动时间

内容编码标头

Content-Encoding 标头告诉浏览器 Unity 用于压缩文件的压缩类型。这样,浏览器可以原生解压缩文件。

将 Content-Encoding 响应标头设置为在 Player Settings 中选择的压缩方法。

压缩方法 文件扩展名 响应标头
gzip .gz Content-Encoding: gzip
Brotli .br Content-Encoding: br

附加标头

如果文件包含 JavaScript,则应添加 application/javascript Content-Type 标头。某些服务器可能会自动包含此内容,另一些服务器则不会。

文件扩展名 响应标头
.js、.js.gz、js.br Content-Type: application/javascript

WebAssembly 串流(更高级别的标头)

WebAssembly 串流允许浏览器在下载代码的同时编译 WebAssembly 代码。这可以显著缩短加载时间。

为了进行 WebAssembly 串流编译,服务器需要返回 application/wasm MIME 类型的 WebAssembly 文件。 要使用 WebAssembly 串流,需要使用带 Content-Type: application/wasm 响应标头的 WebAssembly 文件。 Content-Type 标头告诉服务器内容是什么媒体类型。对于 WebAssembly 文件,该值应该设置为 application/wasm

文件扩展名 响应标头
.wasm、.wasm.gz、.wasm.br Content-Type: application/wasm

WebGL:服务器配置代码示例

Nginx

http {
# ...

server {
# 在http服务器配置中添加以下配置 
# ...
 
# 磁盘上的brotli预压缩数据文件应该在启用压缩的情况下提供: 
location ~ .+\.(data|symbols\.json)\.br$ {
    # 因为这个文件已经在磁盘上进行了预压缩,所以禁用对它的按需压缩。 
    # 否则nginx将尝试双重压缩。 
    gzip off;
    add_header Content-Encoding br;
    default_type application/octet-stream;
}

# 磁盘上的brotli预压缩JavaScript代码文件: 
location ~ .+\.js\.br$ {
    gzip off; # 不要尝试对已经压缩的文件进行动态gzip压缩 
    add_header Content-Encoding br;
    default_type application/javascript;
}

# 磁盘上的brotli预压缩WebAssembly文件: 
location ~ .+\.wasm\.br$ {
    gzip off; # Do not attempt dynamic gzip compression on an already compressed file
    add_header Content-Encoding br;
	#通过指定正确的MIME类型来启用流式WebAssembly编译  
	# Wasm文件。 
    default_type application/wasm;
}

# 磁盘上的gzip预压缩数据文件应该在启用压缩的情况下提供: 
location ~ .+\.(data|symbols\.json)\.gz$ {
    gzip off; # 不要尝试对已经压缩的文件进行动态gzip压缩 
    add_header Content-Encoding gzip;
    default_type application/octet-stream;
}

# 磁盘上的gzip-预压缩JavaScript代码文件: 
location ~ .+\.js\.gz$ {
    gzip off; # 不要尝试对已经压缩的文件进行动态gzip压缩 
    add_header Content-Encoding gzip;
    default_type application/javascript;
}

# 磁盘上的gzip-预压缩WebAssembly文件: 
location ~ .+\.wasm\.gz$ {
    gzip off; # 不要尝试对已经压缩的文件进行动态gzip压缩 
    add_header Content-Encoding gzip;
	#通过指定正确的MIME类型来启用流式WebAssembly编译  
	# Wasm文件。 
    default_type application/wasm;
}
}
}

Apache

# This configuration file should be uploaded to the server as "<Application Folder>/Build/.htaccess"
# NOTE: "mod_mime" Apache module must be enabled for this configuration to work.
<IfModule mod_mime.c>

# 以下几行是没有开启decompression fallback,使用gzip进行压缩 
RemoveType .gz
AddEncoding gzip .gz
AddType application/octet-stream .data.gz
AddType application/wasm .wasm.gz
AddType application/javascript .js.gz
AddType application/octet-stream .symbols.json.gz

# 以下几行是没有开启decompression fallback,使用Brotli进行压缩 
RemoveType .br
RemoveLanguage .br
AddEncoding br .br
AddType application/octet-stream .data.br
AddType application/wasm .wasm.br
AddType application/javascript .js.br
AddType application/octet-stream .symbols.json.br

# 以下几行提高了未压缩构建的加载性能 
AddType application/wasm .wasm

# 取消下面一行的注释可以提高开启decompression fallback的gzip压缩构建的加载性能 
# AddEncoding gzip .unityweb

# 取消下面一行的注释可以提高开启decompression fallback的brotli压缩构建的加载性能 
# AddEncoding br .unityweb

IIS(Decompression Fallback 未开启)

<?xml version="1.0" encoding="UTF-8"?>
<!--
 The following server configuration can be used for compressed WebGL builds without decompression fallback.
 This configuration file should be uploaded to the server as "<Application Folder>/Build/web.config".

NOTE: To host compressed WebGL builds without decompression fallback, you need to install the "URL Rewrite" IIS module on the server.
Otherwise, IIS will throw an exception when using this configuration file.
This module is available at https://www.iis.net/downloads/microsoft/url-rewrite.
-->


<configuration>
 <system.webServer>
   <!--
     Compressed Unity builds without decompression fallback can't be properly hosted on a server which
     has static compression enabled because this might result in the build files being compressed twice.
     The following line disables static server compression.
   -->
   <urlCompression doStaticCompression="false" />
   <!-- To host compressed Unity builds, the correct mimeType should be set for the compressed build files. -->
   <staticContent>
     <!--
       NOTE: IIS will throw an exception if a mimeType is specified multiple times for the same extension.
       To avoid possible conflicts with configurations that are already on the server, you should remove the mimeType for the corresponding extension using the <remove> element,
       before adding mimeType using the <mimeMap> element.
     -->
     <!-- The following lines are required for builds compressed with gzip, which don't include decompression fallback. -->
     <remove fileExtension=".data.gz" />
     <mimeMap fileExtension=".data.gz" mimeType="application/octet-stream" />
     <remove fileExtension=".wasm.gz" />
     <mimeMap fileExtension=".wasm.gz" mimeType="application/wasm" />
     <remove fileExtension=".js.gz" />
     <mimeMap fileExtension=".js.gz" mimeType="application/javascript" />
     <remove fileExtension=".symbols.json.gz" />
     <mimeMap fileExtension=".symbols.json.gz" mimeType="application/octet-stream" />
     <!-- The following lines are required for builds compressed with Brotli, which don't include decompression fallback. -->
     <remove fileExtension=".data.br" />
     <mimeMap fileExtension=".data.br" mimeType="application/octet-stream" />
     <remove fileExtension=".wasm.br" />
     <mimeMap fileExtension=".wasm.br" mimeType="application/wasm" />
     <remove fileExtension=".js.br" />
     <mimeMap fileExtension=".js.br" mimeType="application/javascript" />
     <remove fileExtension=".symbols.json.br" />
     <mimeMap fileExtension=".symbols.json.br" mimeType="application/octet-stream" />
   </staticContent>

   <!--
     Hosting compressed Unity builds without decompression fallback relies on native browser decompression,
     therefore a proper "Content-Encoding" response header should be added for the compressed build files.
     NOTE: IIS will throw an exception if the following section is used without the "URL Rewrite" module installed.
     Download the "URL Rewrite" module from https://www.iis.net/downloads/microsoft/url-rewrite
   -->
   <rewrite>
     <outboundRules>
       <!--
         NOTE: IIS will throw an exception if the same rule name is used multiple times.
         To avoid possible conflicts with configurations that are already on the server, you should remove the mimeType for the corresponding extension using the <remove> element,
       before adding mimeType using the <mimeMap> element.
       -->
       <!-- The following section is required for builds compressed with gzip, which don't include decompression fallback. -->
       <remove name="Append gzip Content-Encoding header" />
       <rule name="Append gzip Content-Encoding header">
         <match serverVariable="RESPONSE_Content-Encoding" pattern=".*" />
         <conditions>
           <add input="{REQUEST_FILENAME}" pattern="\.gz$" />
         </conditions>
         <action type="Rewrite" value="gzip" />
       </rule>
       <!-- The following section is required for builds compressed with Brotli, which don't include decompression fallback. -->
       <remove name="Append brotli Content-Encoding header" />
       <rule name="Append brotli Content-Encoding header">
         <match serverVariable="RESPONSE_Content-Encoding" pattern=".*" />
         <conditions>
           <add input="{REQUEST_FILENAME}" pattern="\.br$" />
         </conditions>
         <action type="Rewrite" value="br" />
       </rule>
     </outboundRules>
   </rewrite>
 </system.webServer>
</configuration>

IIS(未压缩)

<?xml version="1.0" encoding="UTF-8"?>
<!--
 The following server configuration can be used for uncompressed WebGL builds.
 This configuration file should be uploaded to the server as "<Application Folder>/Build/web.config"
-->
<configuration>
 <system.webServer>
   <!--
     IIS does not provide default handlers for .data and .wasm files (and in some cases .json files),
     therefore these files won’t be served unless their mimeType is explicitly specified.
   -->
   <staticContent>
     <!--
       NOTE: IIS will throw an exception if a mimeType is specified multiple times for the same extension.
       To avoid possible conflicts with configurations that are already on the server, you should remove the mimeType for the corresponding extension using the <remove> element,
       before adding mimeType using the <mimeMap> element.
     -->
     <remove fileExtension=".data" />
     <mimeMap fileExtension=".data" mimeType="application/octet-stream" />
     <remove fileExtension=".wasm" />
     <mimeMap fileExtension=".wasm" mimeType="application/wasm" />
     <remove fileExtension=".symbols.json" />
     <mimeMap fileExtension=".symbols.json" mimeType="application/octet-stream" />
   </staticContent>
 </system.webServer>
</configuration>

网站公告

今日签到

点亮在社区的每一天
去签到