LoginSignup
5
8

【Unity】WebGLで画面比固定でウインドウに合わせて表示サイズを可変させる

Posted at

はじめに

UnityのWebGLで画面のアスペクト比を固定しつつ、画面サイズをWebブラウザーの表示に合わせて変更する処理の紹介です。

実行サンプル

DemoURL

作業環境

  • Unity 2021.3.30f1
  • Windows 10

作業準備

  1. WebGLをターゲットにしたUnityプロジェクトを作成する
  2. 以下のWebGLTemplateのフォルダをコピー
    C:\Program Files\Unity\Hub\Editor\2021.3.30f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\WebGLTemplates\Default
    ※Unityのバージョンでパスが異なります
  3. コピーしたWebGLTemplateをAsset内に貼り付ける
  4. 貼り付けたWebGLTemplateは任意の名前に変更する

コード

WebGLTemplate内の変更するスクリプトは以下です。

  • index.html
  • TemplateData\style.css

以下に変更後のコードを記載します。

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 | {{{ 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={{{ WIDTH }}} height={{{ HEIGHT }}}></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 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");
      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();
      }
      //ウインドウサイズに合わせキャンバスサイズを変更
      function fitWindowCanvasSize(){
        
        var winHeight = window.innerHeight;
        const isViewUnderBar = true;  //falseにすると下部のバーが画面外に表示
        if (isViewUnderBar){
	        const barHeight = 18*2;            //下部のバーサイズ
	        const barHeightMargin = 10*2;      //下部の追加余白サイズ
	        winHeight = winHeight - barHeight - barHeightMargin;
        }
        const winWidth = window.innerWidth;
        const appWidth = "{{{ WIDTH }}}";
        const appHeight = "{{{ HEIGHT }}}";
        const scale = Math.min((winWidth / appWidth), (winHeight / appHeight));
        const fixWidth = (appWidth * scale);
        const fixHeight = (appHeight * scale);

        canvas.style.width = fixWidth + 'px';
        canvas.style.height = fixHeight + 'px';
      }
      //ウインドウのリサイズ時の処理
      function windowResizeEvent(){
        var timeoutId = 0;
        const resizeDelay = 300;  //リサイズが完了したと判断する時間
        window.onresize = () => {
          clearTimeout(timeoutId);
          timeoutId = setTimeout(()=>{
            //リサイズ終了時の処理
            fitWindowCanvasSize();
          }, resizeDelay);
        };
      }

      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: {{{ JSON.stringify(COMPANY_NAME) }}},
        productName: {{{ JSON.stringify(PRODUCT_NAME) }}},
        productVersion: {{{ JSON.stringify(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.
      // 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";
        canvas.className = "unity-mobile";

        // To lower canvas resolution on mobile devices to gain some
        // performance, uncomment the following line:
        // config.devicePixelRatio = 1;

        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:
        
        fitWindowCanvasSize();
        
      }

#if BACKGROUND_FILENAME
      canvas.style.background = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover";
#endif
      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) => {
          alert(message);
        });
        windowResizeEvent();
      };
      document.body.appendChild(script);
    </script>
  </body>
</html>

style.css
body { padding: 0; margin: 0 }
#unity-container { position: absolute }
#unity-container { left: 50%; top: 0%; transform: translate(-50%, 0%) }
#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 }

コード解説

index.htmlの変更箇所

Webブラウザの表示サイズに合わせてアプリの表示サイズを変更する関数fitWindowCanvasSizeを追加しました。
画面下部にロゴや拡大表示ボタンを表示するために、縦の表示サイズに下部の表示サイズが追加されてます。

index.html
//ウインドウサイズに合わせキャンバスサイズを変更
function fitWindowCanvasSize(){
  
  var winHeight = window.innerHeight;
  const isViewUnderBar = true;  //falseにすると下部のバーが画面外に表示
  if (isViewUnderBar){
    const barHeight = 18*2;            //下部のバーサイズ
    const barHeightMargin = 10*2;      //下部の追加余白サイズ
    winHeight = winHeight - barHeight - barHeightMargin;
  }
  const winWidth = window.innerWidth;
  const appWidth = "{{{ WIDTH }}}";
  const appHeight = "{{{ HEIGHT }}}";
  const scale = Math.min((winWidth / appWidth), (winHeight / appHeight));
  const fixWidth = (appWidth * scale);
  const fixHeight = (appHeight * scale);

  canvas.style.width = fixWidth + 'px';
  canvas.style.height = fixHeight + 'px';
}

Webブラウザの表示サイズが変更された場合実行するイベント処理関数windowResizeEventを追加しました。
リサイズを検知したとき、fitWindowCanvasSize()が実行されます。

index.html
//ウインドウのリサイズ時の処理
function windowResizeEvent(){
  var timeoutId = 0;
  const resizeDelay = 300;  //リサイズが完了したと判断する時間
  window.onresize = () => {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(()=>{
      //リサイズ終了時の処理
      fitWindowCanvasSize();
    }, resizeDelay);
  };
}

起動時のアプリの表示サイズを変更するために、テンプレートのコードを以下の変更を行なってます。また今回はPC想定なのでここを変更しました。

index.html
} else {
  // Desktop style: Render the game canvas in a window that can be maximized to fullscreen:

- canvas.style.width = "{{{ WIDTH }}}px";
- canvas.style.height = "{{{ HEIGHT }}}px";
+ fitWindowCanvasSize();

}

Webブラウザの表示サイズ変更時の処理追加するため、テンプレートのコードを以下の変更を行なってます。

index.html
script.onload = () => {
  createUnityInstance(canvas, config, (progress) => {
    progressBarFull.style.width = 100 * progress + "%";
  }).then((unityInstance) => {
    loadingBar.style.display = "none";
    fullscreenButton.onclick = () => {
      unityInstance.SetFullscreen(1);
    };
  }).catch((message) => {
    alert(message);
  });
+ windowResizeEvent();
};

style.cssの変更箇所

テンプレートのコードを以下に変更しました。
この変更は、Unityアプリの表示起点をブラウザの表示領域の上部の横中央にしてます。

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 { position: fixed; width: 100%; height: 100% }
+ #unity-container { left: 50%; top: 0%; transform: translate(-50%, 0%) }
#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 { cursor:pointer; 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 }

実行方法

  1. プロジェクト設定をWebGLにする
  2. Project SettingsでResolutinの想定の解像度の設定をする
  3. WebGLTemplateを今回実装したWebGLTemplateに変更する
  4. Build And Runを実行して、WebGLで出力して動作確認をする
5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8