在新的 Unity WebGL 模板中添加全屏显示的方式

Tia*_*ica 6 css unity-game-engine unity-webgl

一般来说,使用 Unity WebGL 构建时,默认模板如下所示

Unity 默认 WebGL 构建

文档中,我们可以看到,当我们想要在 Unity 中使用 WebGL 模板时,我们必须在 Assets 中创建一个名为WebGLTemplates的文件夹,以及一个名为 New Template(或任何您想要的名称)的文件夹,并在其中添加一个index.html

Unity WebGL 新模板

另外,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>
Run Code Online (Sandbox Code Playgroud)

然后,在播放器设置下,选择该模板

Unity WebGL 构建新模板

问题是,这并没有增加到全尺寸的选项。

Unity 新模板没有将尺寸增加到全屏的按钮

Tia*_*ica 4

您可以简单地在index.html模板中添加一个具有特定高度和宽度且具有onclick事件的div 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>
Run Code Online (Sandbox Code Playgroud)

所以,将代码更改为(我决定将其放在Unity画布上方

<!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>
Run Code Online (Sandbox Code Playgroud)

这将输出以下内容

WebGL Unity 全屏

游戏加载时单击绿色区域将使其全屏显示。