在 Web 平台中使用纹理压缩可根据平台支持的纹理压缩格式创建面向平台的构建。
桌面设备和移动设备支持不同的纹理压缩格式。如果希望 Web 应用程序在这两种类型的浏览器上使用压缩纹理,必须首先选择支持的纹理压缩格式。
要使用压缩纹理在桌面端和移动端浏览器上运行游戏,可能需要针对以下内容创建两个构建:
可从 Web 构建设置 (Web Build Settings) 窗口或 Web 播放器设置 (Player Settings) 窗口为 Web 应用程序设置默认纹理压缩格式。在设置纹理压缩格式之前,必须确定这些设置中的哪一个优先。在构建设置 (Build Settings) 中设置的纹理压缩格式值优先于在播放器设置 (Player Settings) 中设置的值。默认情况下,Unity 编辑器将构建设置 (Build Settings) 值设置为使用播放器设置 (Use Player Settings)。
注意:编辑器在 Library 文件夹中的构建设置 (Build Settings) 中序列化纹理压缩。这意味着它不受 Version Control 管理。
您还可以自定义单个纹理的纹理压缩格式。为单个纹理设置的值会覆盖默认纹理压缩格式值。有关如何更改单个纹理的纹理格式的信息,请参阅纹理导入设置。
您可以使用构建设置 (Build Settings) 或播放器设置 (Player Settings) 为 Web 应用程序设置默认纹理压缩格式。在构建设置 (Build Settings) 中设置的纹理压缩格式值优先于在播放器设置 (Player Settings) 中设置的值。默认情况下,Unity 编辑器将构建设置 (Build Settings) 值设置为使用播放器设置 (Use Player Settings)。
要使用构建设置 (Build Settings) 选择默认纹理压缩格式,请执行以下操作:
要使用播放器设置选择默认纹理压缩格式,请执行以下操作:
有关如何同时为桌面端浏览器和移动端浏览器使用相应纹理压缩格式创建构建的示例,请参阅通过脚本为桌面端和移动端浏览器创建构建。
您可以使用脚本同时为桌面端浏览器和移动端浏览器运行具有相应纹理压缩格式的构建。例如:
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.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,
};