打包webgl后,发现每次加载资源特别慢,查看了浏览器的DevTools,发现有一条日志:
[UnityCache] Failed to load 'http://192.168.0.191:3000/Build/public.data' from indexedDB cache due to the error: Error: Could not connect to cache: Cache API is not supported.
查了官方文档:
Cache behavior in WebGL - Unity 手册
Data Caching
To access Data Caching, open the Publishing Setings for Web from File > Build Settings > Player Settings. This enables the browser to cache the main data files into the IndexedDB database.
Using the default browser HTTP cache doesn’t guarantee that the browser caches a particular response. This is because the browser HTTP cache has limited space, and the browser might not be able to cache files that are too large.
To improve your loading speed,
IndexedDB
allows you to cache files above the browser limit. When you cache more files, you increase the chance that downloaded content is available on the user’s machine during the next run of the build.Data Caching only caches the
.data
files in the IndexedDB cache for HTTP responses. To cache AssetBundles, you need to enable Data Caching and overrideunityInstance.Module.cacheControl()
. To do this, make sureModule.cacheControl(url)
returnsmust-revalidate
for the requested AssetBundle URL. For example, you can override theunityInstance.Module.cacheControl()
function in the fulfillment callback of the Promise thatcreateUnityInstance()
returns. For further information oncreateUnityInstance()
, see Compressed builds and server configuration.
发现是要在模板的index.html里的config配置中增加cacheControl的配置,加上就好了。
如下图
模板长这样:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{{{ PRODUCT_NAME }}}</title>
<link rel="shortcut icon" href="TemplateData/favicon.ico">
<link rel="stylesheet" href="TemplateData/style.css">
</head>
<body>
<div id="unity-container" class="unity-desktop">
<canvas id="unity-canvas" width=960 height=600></canvas>
<div id="unity-loading-bar">
<div id="unity-logo"></div>
<div id="unity-progress-bar-empty">
<div id="unity-progress-bar-full"></div>
</div>
</div>
<div id="unity-warning"> </div>
</div>
<script>
var container = document.querySelector("#unity-container");
var canvas = document.querySelector("#unity-canvas");
var loadingBar = document.querySelector("#unity-loading-bar");
var progressBarFull = document.querySelector("#unity-progress-bar-full");
var fullscreenButton = document.querySelector("#unity-fullscreen-button");
var warningBanner = document.querySelector("#unity-warning");
// Shows a temporary message banner/ribbon for a few seconds, or
// a permanent error message on top of the canvas if type=='error'.
// If type=='warning', a yellow highlight color is used.
// Modify or remove this function to customize the visually presented
// way that non-critical warnings and error messages are presented to the
// user.
function unityShowBanner(msg, type) {
function updateBannerVisibility() {
warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';
}
var div = document.createElement('div');
div.innerHTML = msg;
warningBanner.appendChild(div);
if (type == 'error') div.style = 'background: red; padding: 10px;';
else {
if (type == 'warning') div.style = 'background: yellow; padding: 10px;';
setTimeout(function() {
warningBanner.removeChild(div);
updateBannerVisibility();
}, 5000);
}
updateBannerVisibility();
}
var buildUrl = "Build";
var loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";
var config = {
dataUrl: buildUrl + "/{{{ DATA_FILENAME }}}",
frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}",
codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",
#if MEMORY_FILENAME
memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",
#endif
#if SYMBOLS_FILENAME
symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",
#endif
streamingAssetsUrl: "StreamingAssets",
companyName: "{{{ COMPANY_NAME }}}",
productName: "{{{ PRODUCT_NAME }}}",
productVersion: "{{{ PRODUCT_VERSION }}}",
showBanner: unityShowBanner,
#if USE_DATA_CACHING
cacheControl: function (url) {
// Caching enabled for .data and .bundle files.
// Revalidate if file is up to date before loading from cache
if (url.match(/\.data/) || url.match(/\.bundle/)) {
return "must-revalidate";
}
// Caching enabled for .mp4 and .custom files
// Load file from cache without revalidation.
if (url.match(/\.mp4/) || url.match(/\.custom/)) {
return "immutable";
}
// Disable explicit caching for all other files.
// Note: the default browser cache may cache them anyway.
return "no-store";
},
#endif
};
// By default Unity keeps WebGL canvas render target size matched with
// the DOM size of the canvas element (scaled by window.devicePixelRatio)
// Set this to false if you want to decouple this synchronization from
// happening inside the engine, and you would instead like to size up
// the canvas DOM size and WebGL render target sizes yourself.
// config.matchWebGLToCanvasSize = false;
if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
container.className = "unity-mobile";
// Avoid draining fillrate performance on mobile devices,
// and default/override low DPI mode on mobile browsers.
config.devicePixelRatio = 1;
unityShowBanner('WebGL builds are not supported on mobile devices.');
} else {
canvas.style.width = "100vw";
canvas.style.height = "100vh";
}
loadingBar.style.display = "block";
var script = document.createElement("script");
script.src = loaderUrl;
script.onload = () => {
createUnityInstance(canvas, config, (progress) => {
progressBarFull.style.width = 100 * progress + "%";
}).then((unityInstance) => {
loadingBar.style.display = "none";
window.unityInstance = unityInstance; // <-- this
fullscreenButton.onclick = () => {
unityInstance.SetFullscreen(1);
};
}).catch((message) => {
// alert(message);
});
};
document.body.appendChild(script);
</script>
</body>
</html>
问题:
加上之后还是提示不支持,GoogleChrome浏览器测试的,后来在同事的浏览器测试没问题了
正常的情况是这样