一般来说,使用 Unity WebGL 构建时,默认模板如下所示
来自 documentation ,我们可以看到,当我们想在Unity中使用WebGL模板时,我们必须在Assets中创建一个名为的文件夹。 WebGL 模板 , 和一个名为 New Template(或任何你想要的名字)的文件夹并添加一个 index.html 那里。
此外, index.html 应该有一个类似于此的代码
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Unity WebGL Player | %UNITY_WEB_NAME%</title>
<script src="%UNITY_WEBGL_LOADER_URL%"></script>
<script>
var unityInstance = UnityLoader.instantiate("unityContainer", "%UNITY_WEBGL_BUILD_URL%");
</script>
</head>
<body>
<div id="unityContainer" style="width: %UNITY_WIDTH%px; height: %UNITY_HEIGHT%px; margin: auto"></div>
</body>
</html>
然后,在播放器设置下,选择该模板问题是,这并没有增加到全尺寸的选项。
最佳答案
您可以简单地在 index.html 模板中添加一个具有特定高度和宽度的 div,该 div 具有 onclick 事件 unityInstance.SetFullscreen(1)
, 喜欢
<div style="height:20px; width: 960px; background: green;" onclick="unityInstance.SetFullscreen(1)"><b>Click here to make it full screen.</b></div>
因此,将代码更改为(我决定将其放在 Unity Canvas 上方<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Unity WebGL Player | %UNITY_WEB_NAME%</title>
<script src="%UNITY_WEBGL_LOADER_URL%"></script>
<script>
var unityInstance = UnityLoader.instantiate("unityContainer", "%UNITY_WEBGL_BUILD_URL%");
</script>
</head>
<body>
<div style="height:20px; width: %UNITY_WIDTH%px; background: green;" onclick="unityInstance.SetFullscreen(1)"><b>Click here to make it full screen.</b></div>
<div id="unityContainer" style="width: %UNITY_WIDTH%px; height: %UNITY_HEIGHT%px; margin: auto"></div>
</body>
</html>
这将输出以下内容并在游戏加载时单击绿色区域将使其全屏显示。
关于css - 在新的 Unity WebGL 模板中添加全屏显示方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63307533/