如果你想要修改unity 自带的webgl 启动样式,那么你需要在unity Assets 文件夹目录下添加如下目录
WebGLTemplates 目录是 unity 指定名称的目录,你可以在这个目录下新建你自己的模板目录 名字随意,然后在你需要找一下 你unity 的Default模板文件 ,在你unity 安装目录的 Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\WebGLTemplates\Default
其中 TemplateData 中放置的是WebGL 使用的图片 和 html 使用的样式文件
index.html 就是网页模板文件 修改网页模板样式 主要是修改这个文件
thumbnail.png 是在unity 模板选择 显示得缩略图,没有也不要紧,把 Default 文件夹里面的文件都复制到你自己的模板文件夹
接下来 主要说一下 index 文件
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--选项卡标题 可以随意修改 <title>xxxxxxxxxxxx</title> 这样都是可以的 -->
<title>Unity WebGL Player | {{{ PRODUCT_NAME }}}</title>
<!--选项卡icon 一般替换TemplateData 里面的 favicon.ico 图片就可以了-->
<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={{{ WIDTH }}} height={{{ HEIGHT }}}></canvas>
<!--这个是进度条和大logo部分-->
<div id="unity-loading-bar">
<!-- 这个是大logo 他的图片在TemplateData文件夹中 样式 在Style.css 中 下方所有的图片和这个同理 如果不想要logo 可以直接注释掉-->
<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>
<!--这个是页面下方的logo和全屏显示的按钮 不想要可以全部注释-->
<div id="unity-footer">
<div id="unity-webgl-logo"></div>
<div id="unity-fullscreen-button"></div>
<div id="unity-build-title">{{{ PRODUCT_NAME }}}</div>
</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");
//上面4条一般不做修改,下面这2条是处理全屏按钮事件和警告信息显示的 不想要可以删除 但是注意 下方代码中有引用 要一并都去掉
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.
//显示一个临时的消息窗口几秒钟,如果type=='error'显示一个永久的错误消息在画布顶部。
//如果type=='warning',则使用黄色高亮显示。
//修改或删除此功能,以自定义向用户显示非关键警告和错误消息的可视化显示方式。
// 这个方法可以删除 ,删掉后就不会再有讨厌的报错弹窗的 如果删除的话下方方法调用也一并删除
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 }}}",
#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: "{{{ COMPANY_NAME }}}",
productName: "{{{ PRODUCT_NAME }}}",
productVersion: "{{{ PRODUCT_VERSION }}}",
showBanner: unityShowBanner,
};
// 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.
//默认情况下,Unity保持WebGL画布渲染目标大小与画布元素的DOM大小相匹配(由window.devicePixelRatio缩放),如果你想从引擎内部分离这种同步
//要自己调整画布DOM大小和WebGL渲染目标大小,将此设置为false。
// config.matchWebGLToCanvasSize = false;
if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
// Mobile device style: fill the whole browser client area with the game canvas:
var meta = document.createElement('meta');
meta.name = 'viewport';
meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
document.getElementsByTagName('head')[0].appendChild(meta);
container.className = "unity-mobile";
// To lower canvas resolution on mobile devices to gain some
// performance, uncomment the following line:
// config.devicePixelRatio = 1;
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
//如果想要在手机上运行要删除掉下面这条报错的调用 其实这个放在这就是为了不让手机用户使用
//unityShowBanner('WebGL builds are not supported on mobile devices.');
} else {
// Desktop style: Render the game canvas in a window that can be maximized to fullscreen:
//这个是unity里设置的宽高
//canvas.style.width = "{{{ WIDTH }}}px";
//canvas.style.height = "{{{ HEIGHT }}}px";
//这样写可实现窗口自适应浏览器大小,不需要再关注unity设置的宽高
canvas.style.width = "99vw";
canvas.style.height = "99vh";
}
#if BACKGROUND_FILENAME
canvas.style.background = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover";
#endif
loadingBar.style.display = "block";
//在这可以声明一个GameInstance变量来留作unity和js交互
var GameInstance = null;
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";
GameInstance = unityInstance;
//绑定全屏按钮的事件
fullscreenButton.onclick = () => {
unityInstance.SetFullscreen(1);
};
}).catch((message) => {
alert(message);
});
};
document.body.appendChild(script);
</script>
</body>
</html>
最后看一下style.css文件
body { padding: 0; margin: 0 }
#unity-container { position: absolute }
#unity-container.unity-desktop { left: 50%; top: 50%; transform: translate(-50%, -50%) }
#unity-container.unity-mobile { width: 100%; height: 100% }
#unity-canvas { background: {{{ BACKGROUND_COLOR }}} }
.unity-mobile #unity-canvas { width: 100%; height: 100% }
#unity-loading-bar { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); display: none }
#unity-logo { width: 154px; height: 130px; background: url('unity-logo-{{{ SPLASH_SCREEN_STYLE.toLowerCase() }}}.png') no-repeat center }
#unity-progress-bar-empty { width: 141px; height: 18px; margin-top: 10px; margin-left: 6.5px; background: url('progress-bar-empty-{{{ SPLASH_SCREEN_STYLE.toLowerCase() }}}.png') no-repeat center }
#unity-progress-bar-full { width: 0%; height: 18px; margin-top: 10px; background: url('progress-bar-full-{{{ SPLASH_SCREEN_STYLE.toLowerCase() }}}.png') no-repeat center }
#unity-footer { position: relative }
.unity-mobile #unity-footer { display: none }
#unity-webgl-logo { float:left; width: 204px; height: 38px; background: url('webgl-logo.png') no-repeat center }
#unity-build-title { float: right; margin-right: 10px; line-height: 38px; font-family: arial; font-size: 18px }
#unity-fullscreen-button { float: right; width: 38px; height: 38px; background: url('fullscreen-button.png') no-repeat center }
#unity-warning { position: absolute; left: 50%; top: 5%; transform: translate(-50%); background: white; padding: 10px; display: none }
里面都是每块布局的位置 和图片引用信息