Version: 6000.3
语言: 中文
在 Web 中播放视频
Web 中的嵌入式资源

Web 中的纹理压缩

使用纹理压缩一种存储数据的方法,可减少所需的存储空间量。请参阅纹理压缩动画压缩音频压缩构建压缩
请参阅术语表
在 Web 平台中创建基于纹理压缩3D图形硬件要求将纹理压缩为专用格式,这些格式针对快速纹理采样进行了优化。更多信息
请参阅术语表
它们支持的格式。

纹理压缩格式

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

要在具有压缩纹理的桌面和移动浏览器上运行游戏,您可能需要创建两个面向:

  • 将 DXT 设置为纹理压缩格式的桌面浏览器。
  • 具有自适应可缩放纹理压缩 (ASTC) 的移动浏览器设置为纹理压缩格式。

压缩格式设置的优先级

您可以从 Web 生成设置或 Web 中为 Web 应用程序设置默认纹理压缩格式玩家设置设置,可让您为 Unity 构建的最终游戏设置各种特定于玩家的选项。更多信息
请参阅术语表
.在设置纹理压缩格式之前,请务必确定哪些设置优先。在构建设置中设置的纹理压缩格式值优先于在 玩家设置(Player Settings) 中设置的值。默认情况下,Unity 编辑器将构建设置值设置为“使用播放器设置”。

注意:编辑器在构建设置中序列化纹理压缩Library文件夹。这意味着它不受版本控制用于管理文件更改的系统。您可以将 Unity 与最常见的版本控制工具结合使用,包括 Perforce、Git、Mercurial 和 PlasticSCM。更多信息
请参阅术语表
.

您还可以自定义单个纹理的纹理压缩格式。您为个人设置的值texture 覆盖特定于平台的设置,允许你为每个目标平台设置分辨率、文件大小以及相关的内存大小要求、像素尺寸和纹理质量。更多信息
请参阅术语表
默认纹理压缩格式值。有关如何更改纹理格式一种文件格式,用于在 3D 图形硬件(例如显卡或移动设备)实时渲染期间处理纹理。更多信息
请参阅术语表
单个纹理,请参阅纹理导入设置

设置默认压缩格式

您可以使用构建设置或播放器设置为 Web 应用程序设置默认纹理压缩格式。在构建设置中设置的纹理压缩格式值优先于在播放器设置中设置的值。默认情况下,Unity 编辑器将构建设置值设置为“使用播放器设置”。

要使用构建设置选择默认纹理压缩格式,请执行以下作:

  1. 选择“文件”>“生成配置文件”
  2. “平台”面板的平台列表中,选择“Web”或为 Web 平台创建构建配置文件
  3. 纹理压缩(Texture Compression) 下拉菜单中选择一种压缩格式。

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

  1. 选择“文件”>“生成配置文件”
  2. 平台面板的平台列表中,选择 Web
  3. 选择播放器设置>其他设置
  4. 纹理压缩格式(Texture compression format) 下拉菜单中选择一种压缩格式。

有关如何同时为桌面浏览器和移动浏览器创建构建版本及其相应的纹理压缩格式的示例,请参阅从脚本为桌面浏览器和移动浏览器创建构建版本

从脚本为桌面和移动浏览器创建构建

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

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 模板的 index.htmlfile 以选择适当的数据文件(如果支持纹理压缩格式扩展):

// 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,                                            
     };  

其他资源

在 Web 中播放视频
Web 中的嵌入式资源