LoginSignup
4
5

More than 3 years have passed since last update.

UnityでWebGL向けにビルドしたゲームをlocalhostで立ち上げてブラウザで動作確認する

Last updated at Posted at 2019-07-22

概要

Unityでゲームを作ってWebGLでビルドすると、簡単に配布できて便利ですよね。
このビルド結果について、ブラウザで立ち上げてカジュアルに動作確認したい場合があると思います。

しかし、WebGLでビルドしたディレクトリにあるindex.htmlをそのままブラウザで開くと

It seems your browser does not support running Unity WebGL
content from file:// urls. Please upload it to an http server, or try a
different browser.

となり、起動できません。これは、httpサーバーを用意してそこに配置してねという意味ですが、正直めんどすぎます。

一般的な方法

XAMPPをインストールしてhtdocsを設定して確認する方法があるようです。

【Unity】WebGL でビルドしたゲームを起動する方法 - コガネブログ
http://baba-s.hatenablog.com/entry/2017/11/01/093000

UnityでWebGL出力 – それは悲しみ(˘ω˘ )
https://boccilog.wordpress.com/2016/05/08/unitywebglbuild/

ただ、これも正直めんどすぎます。もっとお手軽にやりたいです。

静的ファイルにアクセスするWebServerを立てる

こう書くと大げさに見えますが、goだとかなり簡単にかけます。

SimpleWebServer
https://gist.github.com/xshoji/19dbd8bf35ab37950680d8016084f1a0#file-00_simplewebserver-md

  • Go入ってるひと:↑のmain.goをそのままgo run main.goで動かしてもらって問題ないです。
  • Go入ってない人:mac, linux, windows向けにビルド済みのバイナリも用意しておきました。DLしてお使いください。
    • macの人
      • macだとterminalからの初回実行時に「開発元を検証できません」と出てしまうので、DLしたバイナリを右クリックして「開く」で開くと、次回以降問題なく実行できます。
    • windowsの人
    • linuxの人

使い方は(READMEにも書いてますが)

$ ./webserver_mac -h
Usage of ./webserver_mac:
  -d string
        [require] Document root ( Directory path )
  -h
        help
  -p string
        [optional] Port (default "8080")

$ ./webserver_mac -d /tmp/unity/webgl_app_dir/ -p 9000
Document root : /tmp/unity/webgl_app_dir/
URL           : http://localhost:9000

-dで、UnityでビルドしたWebGLのアプリのDirectoryを指定してください。ポート番号はお好みで。

これで、http://localhost:9000にアクセスすれば、ビルドしたWebGLのゲームが起動します。お手軽です。

4
5
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
4
5