UnityWebGl网页全屏显示

2019及以下版本:

修改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 | Project Name</title>
  <link rel="shortcut icon" href="TemplateData/favicon.ico">
  <link rel="stylesheet" href="TemplateData/style.css">
  <script src="TemplateData/UnityProgress.js"></script>
  <script src="Build/UnityLoader.js"></script>
  <script>
    UnityLoader.instantiate("unityContainer", "Build/Temp.json");
    function ChangeCanvas(){
      document.getElementById("unityContainer").style.width = window.innerWidth + "px";
      document.getElementById("unityContainer").style.height = window.innerHeight + "px";
      document.getElementById("#canvas").style.width = window.innerWidth + "px";
      document.getElementById("#canvas").style.height = window.innerHeight + "px";
    }
  </script>
</head>

<body onload="ChangeCanvas()" onresize="ChangeCanvas()">
  <div class="webgl-content">
    <div id="unityContainer" style="width:100%; height:100%; overflow:hidden;"></div>
  </div>
</body>

</html>

2020及以上版本:

修改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 | Panorama</title>
  <link rel="shortcut icon" href="TemplateData/favicon.ico">
  <link rel="stylesheet" href="TemplateData/style.css">
</head>
<body onload="Reset()" onresize="Reset()">
  <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-mobile-warning">
      WebGL builds are not supported on mobile devices.
    </div>
  </div>
  <script>

    function Reset(){
      var canvas = document.getElementById("#canvas");
      canvas.height = document.body.clientHeight;
      canvas.width = document.body.clientWidth;
    }
    function Reset(){
      var canvas = document.getElementById("#canvas");
      canvas.height = document.documentElement.clientHeight;
      canvas.width = document.documentElement.clientWidth;
    }

    //这部分内容不要盲目替换
    var buildUrl = "Build";
    var loaderUrl = buildUrl + "/Test.loader.js";
    var config = {
      dataUrl: buildUrl + "/Test.data",
      frameworkUrl: buildUrl + "/Test.framework.js",
      codeUrl: buildUrl + "/Test.wasm",
      streamingAssetsUrl: "StreamingAssets",
      companyName: "DefaultCompany",
      productName: "Test project",
      productVersion: "0.1",
    };

    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 mobileWarning = document.querySelector("#unity-mobile-warning");

    // 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";
      config.devicePixelRatio = 1;
    } else {
      container.className = "unity-mobile";
      config.devicePixelRatio = 1;
    }

    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";
        fullscreenButton.onclick = () => {
          unityInstance.SetFullscreen(1);
        };
      }).catch((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: #231F20
}

.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-dark.png') no-repeat center
}

#unity-progress-bar-empty {
    width: 141px;
    height: 18px;
    margin-top: 10px;
    background: url('progress-bar-empty-dark.png') no-repeat center
}

#unity-progress-bar-full {
    width: 0%;
    height: 18px;
    margin-top: 10px;
    background: url('progress-bar-full-dark.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-mobile-warning {
    position: absolute;
    left: 50%;
    top: 5%;
    transform: translate(-50%);
    background: white;
    padding: 10px;
    display: none
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

.webgl-content {
    width: 100%;
    height: 100%;
}

.unityContainer {
    width: 100%;
    height: 100%;
}

  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unitywebgl默认情况下是不会自动全屏的。如果你想让unitywebgl默认全屏,你可以通过以下两种方法实现: 方法一:在HTML文件中设置默认全屏。你可以在`<div>`标签中设置`id`为`webgl-content`,并给它添加`class="webgl-content"`属性。然后,在JavaScript文件中使用`document.getElementById("webgl-content").requestFullscreen()`方法来请求全屏。这样一来,当用户访问网页时,unitywebgl会自动以全屏模式加载。 方法二:使用Unity的`Screen.fullScreen`属性来设置默认全屏。在Unity中,你可以使用`Screen.fullScreen = true`来将unitywebgl设置为默认全屏。然后,将修改后的代码重新构建和发布,以确保更改生效。 需要注意的是,不同的Unity版本可能会有不同的全屏设置方式。所以,在具体实施时,请确认你所使用的Unity版本,并查阅相关的官方文档或讨论社区来获取更准确的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Unity WEBGL设置全屏](https://blog.csdn.net/qq_33994566/article/details/103601093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [UnityWebGL2021 全屏--新版本](https://blog.csdn.net/weixin_42198742/article/details/124502410)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值