# Web 中的纹理压缩格式

> Information about using texture compression in Web.

在 Web 平台中使用纹理压缩可根据平台支持的纹理压缩格式创建面向平台的构建。

## 纹理压缩格式##texture-compression-formats

桌面设备和移动设备支持不同的纹理压缩格式。如果希望 Web 应用程序在这两种类型的浏览器上使用压缩纹理，必须首先选择支持的纹理压缩格式。

要使用压缩纹理在桌面端和移动端浏览器上运行游戏，可能需要针对以下内容创建两个构建：

* DXT 设置为纹理压缩格式的桌面端浏览器。
* 将自适应可扩展纹理压缩 (ASTC) 设置为纹理压缩格式的移动端浏览器。

## 压缩格式设置的优先级##precedence-for-compression-format-settings

可从 [Web 构建设置 (Web Build Settings)](/engine/6000.0/manual/platform-specific/webgl/building-distribution/web-build-settings.md) 窗口或 Web [播放器设置 (Player Settings)](/engine/6000.0/manual/platform-specific/webgl/develop/class-player-settings-web-gl.md) 窗口为 Web 应用程序设置默认纹理压缩格式。在设置纹理压缩格式之前，必须确定这些设置中的哪一个优先。在构建设置 (Build Settings) 中设置的纹理压缩格式值优先于在播放器设置 (Player Settings) 中设置的值。默认情况下，Unity 编辑器将构建设置 (Build Settings) 值设置为**使用播放器设置 (Use Player Settings)**。

\*\*注意：\*\*编辑器在 `Library` 文件夹中的构建设置 (Build Settings) 中序列化纹理压缩。这意味着它不受 Version Control 管理。

您还可以自定义单个纹理的纹理压缩格式。为单个纹理设置的值会覆盖默认纹理压缩格式值。有关如何更改单个纹理的纹理格式的信息，请参阅[纹理导入设置](/engine/6000.0/manual/materials-and-shaders/textures/textures-reference/class-texture-importer.md)。

## 设置默认压缩格式##set-the-default-compression-format

您可以使用构建设置 (Build Settings) 或播放器设置 (Player Settings) 为 Web 应用程序设置默认纹理压缩格式。在构建设置 (Build Settings) 中设置的纹理压缩格式值优先于在播放器设置 (Player Settings) 中设置的值。默认情况下，Unity 编辑器将构建设置 (Build Settings) 值设置为**使用播放器设置 (Use Player Settings)**。

要使用构建设置 (Build Settings) 选择默认纹理压缩格式，请执行以下操作：

1. 选择**文件 (File)** > **构建设置 (Build Settings)**。
2. 从**平台 (Platform)** 窗格中的平台列表中，选择 **Web**。
3. 从**纹理压缩 (Texture Compression)** 下拉菜单中选择压缩格式。

要使用播放器设置选择默认纹理压缩格式，请执行以下操作：

1. 选择**文件 (File)** > **构建设置 (Build Settings)**。
2. 从**平台 (Platform)** 窗格中的平台列表中，选择 **Web**。
3. 选择**播放器设置 (Player Settings)** > **其他设置 (Other Settings)**。
4. 从**纹理压缩格式 (Texture compression format)** 下拉菜单中选择压缩格式。

有关如何同时为桌面端浏览器和移动端浏览器使用相应纹理压缩格式创建构建的示例，请参阅[通过脚本为桌面端和移动端浏览器创建构建](#desktop+mobile)。

## 通过脚本为桌面端和移动端浏览器创建构建##create-builds-for-desktop-and-mobile-browsers-from-a-scriptscrollpoint-id=desktop+mobile-/

您可以使用脚本同时为桌面端浏览器和移动端浏览器运行具有相应纹理压缩格式的构建。例如：

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Diagnostics;
using System.IO;
using UnityEditor.Build.Reporting;

public class comboBuild
{
    //This creates a menu item to trigger the dual builds https://docs.unity3d.com/ScriptReference/MenuItem.html 

    [MenuItem("Game Build Menu/Dual Build")]
    public static void BuildGame()
    {
      //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)
      
      string dualBuildPath    = "WebGLBuilds";
      string desktopBuildName = "WebGL_Build";
      string mobileBuildName  = "WebGL_Mobile";

      string desktopPath = Path.Combine(dualBuildPath, desktopBuildName);
      string mobilePath  = Path.Combine(dualBuildPath, mobileBuildName);
      string[] scenes = new string[] {"Assets/scene.unity"};

      EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.DXT;
      BuildPipeline.BuildPlayer(scenes, desktopPath, BuildTarget.WebGL, BuildOptions.Development); 

      EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.ASTC;
      BuildPipeline.BuildPlayer(scenes,  mobilePath, BuildTarget.WebGL, BuildOptions.Development); 

      // Copy the mobile.data file to the desktop build directory to consolidate them both
      FileUtil.CopyFileOrDirectory(Path.Combine(mobilePath, "Build", mobileBuildName + ".data"), Path.Combine(desktopPath, "Build", mobileBuildName + ".data"));
    }  
}
```

如果支持纹理压缩格式扩展名，可以修改 [Web 模板的](/engine/6000.0/manual/platform-specific/webgl/building-distribution/templates.md) `index.html` 文件以选择适当的数据文件：

```
// choose the data file based on whether there's support for the ASTC texture compression format
      var dataFile = "/{{{ DATA_FILENAME }}}";                                  
      var c = document.createElement("canvas");                                 
      var gl = c.getContext("webgl");                                      
      var gl2 = c.getContext("webgl2");                                    
      if ((gl && gl.getExtension('WEBGL_compressed_texture_astc')) || (gl2 &&   
              gl2.getExtension('WEBGL_compressed_texture_astc'))) {             
        dataFile =  "/WebGL_Mobile.data";                                       
      }                                                                         

      var buildUrl = "Build";
      var loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";                    
      var config = {                                                            
        dataUrl: buildUrl + dataFile,                                           
        frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}",                 
#if USE_WASM                                                                    
        codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",                           
#endif                                                                          
#if MEMORY_FILENAME                                                             
        memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",                       
#endif
#if SYMBOLS_FILENAME                                                            
        symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",                     
#endif                                                                          
        streamingAssetsUrl: "StreamingAssets",                                
        companyName: {{{ JSON.stringify(COMPANY_NAME) }}},
        productName: {{{ JSON.stringify(PRODUCT_NAME) }}},
      productVersion: {{{ JSON.stringify(PRODUCT_VERSION) }}},                
        showBanner: unityShowBanner,                                            
     };  
```

## 其他资源##additional-resources

* [构建 Web 应用程序](/engine/6000.0/manual/platform-specific/webgl/building-distribution/building.md)
* [播放器设置](/engine/6000.0/manual/platform-specific/webgl/develop/class-player-settings-web-gl.md)
