Web 中的纹理压缩
有关在 Web 中使用纹理压缩的信息。
阅读时间6 分钟最后更新于 5 天前
在 Web 平台中使用纹理压缩来基于 Target 平台支持的纹理压缩格式创建构建。
纹理压缩格式
桌面设备和移动端DevicesSupport不同的纹理压缩格式。为了获得最佳结果,请针对每种格式提供优化的 Format。
要使用压缩纹理ON桌面和移动端端浏览器上运行游戏,可能需要创建两个定向构建:
- DXT 设置为纹理压缩格式的桌面浏览器。
- 将自适应可扩展纹理压缩 ASTC 设置为纹理压缩格式的移动端浏览器。
平台兼容性和回退
选择平台支持的Format可提供最佳性能和最少的内存。
如果设备本身不Support您选择的纹理压缩格式,Unity 会使用软件Fallback在运行时解压缩Textures。使用 Fallback 后,应用程序仍然有效,但会使用更多内存,并且运行速度可能会变慢。
压缩格式 Settings 的优先级
可从 Web 构建设置 Window 或 Web Player Settings Window 为 Web 应用程序设置默认纹理压缩格式。在设置纹理压缩格式之前,确定这些 Settings 中的哪一个优先非常重要。在构建设置中设置的纹理压缩值Priority于在播放Player Settings中设置的值。默认情况下,Unity Editor 将构建设置值设置为 使用 Player Settings。
还可以自定义单个 Textures 的纹理压缩格式。为单个纹理设置的值覆盖默认纹理压缩格式值。ON 有关如何更改单个 Textures 的纹理格式的信息,请参阅纹理 Import Settings。
设置默认压缩格式式
您可以使用构建设置或播放Player Settings来设置 Web 应用程序程序的默认纹理压缩格式式。在构建设置中设置的纹理压缩值Priority于在播放Player Settings中设置的值。默认情况下,Unity Editor 将构建设置值设置为 使用 Player Settings。
要使用构建设置选择默认纹理压缩格式:
- 选择 文件 > 生成配置文件。
- 从平台面板中的平台列表中,选择 Web。
- 从 下拉菜单中选择压缩格式。Texture Compression
?
要使用 Player Settings 默认理压缩格式式,请执行以下操作:
- 选择 文件 > 生成配置文件。
- 从平台面板中的平台列表中,选择 Web。
- 选择 > Other Settings。Player Settings(播放器设置)
?
- 从纹理压缩格式下拉菜单中选择压缩格式。
ON 如何同时为桌面端浏览器和移动端浏览器使用相应纹理压缩格式创建构建的示例,请参阅从脚本为桌面端和移动端浏览器创建构建。
通过脚本为桌面端和移动端浏览器创建版本
您可以使用脚本同时为桌面端浏览器和移动端浏览器运行具有相应纹理压缩格式的版本。例如:
using UnityEditor;using System.IO;using UnityEngine;public class ComboBuild{ // This creates a menu item to trigger the dual builds. [MenuItem("游戏 版本 菜单/Dual 版本")] public static void BuildGame() { // Define the output folder structure and names for the builds. string dualBuildPath = "WebGLBuilds"; string desktopBuildName = "WebGL_Build"; string mobileBuildName = "WebGL_Mobile"; string desktopPath = Path.Combine(dualBuildPath, desktopBuildName); string mobilePath = Path.Combine(dualBuildPath, mobileBuildName); 字符串[] 场景 = 新建字符串[] { " Assets/ Scene.Unity" }; // Desktop build with DXT texture compression: BuildPlayerOptions desktopBuildPlayerOptions = new BuildPlayerOptions(); desktopBuildPlayerOptions.scenes = scenes; desktopBuildPlayerOptions.locationPathName = desktopPath; desktopBuildPlayerOptions.target = BuildTarget.WebGL; desktopBuildPlayerOptions.options = BuildOptions.Development; desktopBuildPlayerOptions.subtarget = (int)WebGLTextureSubtarget.DXT; BuildPipeline.BuildPlayer(desktopBuildPlayerOptions); // Mobile build with ASTC texture compression: BuildPlayerOptions mobileBuildPlayerOptions = new BuildPlayerOptions(); mobileBuildPlayerOptions.scenes = scenes; mobileBuildPlayerOptions.locationPathName = mobilePath; mobileBuildPlayerOptions.target = BuildTarget.WebGL; mobileBuildPlayerOptions.options = BuildOptions.Development; mobileBuildPlayerOptions.subtarget = (int)WebGLTextureSubtarget.ASTC; BuildPipeline.BuildPlayer(mobileBuildPlayerOptions); // Copy the mobile.data file to the desktop build directory. FileUtil.CopyFileOrDirectory(Path.Combine(mobilePath, "Build", mobileBuildName + ".data"), Path.Combine(desktopPath, "Build", mobileBuildName + ".data")); }}
您可以修改 Web 模板的文件以选择适当的数据文件(如果支持纹理压缩格式扩展):
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、 };
其他资源
- 生成 Web 应用程序
- Player Settings(播放器设置)