How to manually start a localhost version of WebGL build

Clicking “build and run” on a WebGL game opens http://localhost:52334/ and allows me to play test.
If i try to run the index file directly i get:
“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.”
What is Unity doing with the “Build and run” button and how do i replicate it so i can run folders with old builds?

6 Likes

Unity is starting a simple local webserver when you us build&run. Most browsers restrict loading from file:// for security reasons.
You can server old builds from a local server yourself. Try IIS or the free apache or the quick http.server that comes with a python install.

2 Likes

Thanks for the info Schbkraft. Setting it up and working through all the little issues now.
There is no way to get Unity to call its simple server functionality on demand rather than only at the end of a build and run command?

1 Like

No, there is no button for that, sorry.

1 Like

You could probably make an editor script to fire it up. I think the win executable is located at Unity\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\SimpleWebServer.exe

6 Likes

Firefox let’s you use “file://” by default iirc and you can do the same in chrome with setting a flag.

In general you can get IIS setup locally fairly easily (included with Windows, just not enabled by default). I personally use mamp (https://www.mamp.info/en/) when I’m abroad and don’t have access to our servers.

2 Likes

I tried this method and it not working. When I start webserver with arguments:
SimpleWebServer.exe “path to build index.html” “port”
and then go to url localhost: port I got 403 Forbidden response.

Also got messages in the console:
Starting web server at http://localhost:58505/
Handling request: /
Forbidden.
Handling request: /favicon.ico
Forbidden.

firefox => url => about:config
change
“privacy.file_unique_origin” to “false”
chrome add to link
–allow-file-access-from-files
=> “C:\Program Files (x86)\Google\Chrome\Application\chrome.exe” --allow-file-access-from-files

Then open index.html

6 Likes

Thanks. But I took this code SimpleHTTPServer in C# · GitHub and run my own web server while testing webgl builds

Another way to run an ad hoc web server is to use Emscripten’s “emrun” tool, which is designed to host WebAssembly-compiled pages.

  1. Download https://raw.githubusercontent.com/emscripten-core/emscripten/incoming/emrun.py and https://raw.githubusercontent.com/emscripten-core/emscripten/incoming/emrun.bat and place them to a directory you have in PATH environment variable.
  2. Navigate on command line to a directory you want to host, and type

emrun --no_browser .

to host that directory. Note the “.” as the last argument, which means “current directory”.

If you want to also automate launching the page in a browser, you can execute e.g.

emrun --browser=“c:\program files\mozilla firefox\firefox.exe” .

you can actually decompile SimpleWebServer.exe with dnspy and see why it gives 403 error

i removed this part of code and it start to work

bool flag4 = !context.Request.IsLocal;
                if (flag4)
                {
                    Console.WriteLine("Forbidden.");
                    msg = "<HTML><BODY>403 Forbidden.</BODY></HTML>";
                    response.StatusCode = 403;
                }

Tested on Unity 2019.4.7f1. Create a batch file with content below, adapting for path differences:

C:
cd "C:\Program Files\Unity\Hub\Editor\2019.4.7f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten_Win\python\2.7.5.3_64bit"
python.exe "C:\Program Files\Unity\Hub\Editor\2019.4.7f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emrun.py" "[PATH TO index.html GENERATED DURING BUILD]"
2 Likes

Poindexter’s approach may work if you have a 32-bit version of Python installed separately, but in my case I get “ModuleNotFoundError: No module named ‘win32api’”

What I found works better is one of the below, where <old_WebGL_output_dir> is the path to the previous WebGL build you want to look at…

If you do not have Python installed separately from Unity, you can use the included version which will look something like this (you may need to adjust the path to python.exe for your current version of Unity and the version of Python bundled with it):

cd /d "<old_WebGL_output_dir>"
"C:\Program Files\Unity\Hub\Editor\2019.4.7f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten_Win\python\2.7.5.3_64bit\python.exe" -m SimpleHTTPServer

If you have Python 2.x installed separately (check in CMD with python --version) you can simply do

cd /d "<old_WebGL_output_dir>"
python -m SimpleHTTPServer

If you have Python 3.x installed separately then it’s even simpler:

python -m http.server --directory "<old_WebGL_output_dir>"
1 Like

The method that seems to be the simplest is
Install this app on Chrome.

After launching the app, click the “CHOSE FOLDER” button to set the Unity WebGL build folder, then go to http:// localhost: 8887 (if default settings) in Chrome.

20 Likes

does anyone know why running the included SimpleWebServer.exe manually doesnt work (403 forbidden),
but if you de-compile & build new exe yourself in VS, then it works fine…? (has exact same c# code)

1 Like

For Windows, I’ve been using WampServer for at least a decade with everything imaginable, and it has always worked great for me, including for Unity WebGL builds. You install it, add a folder like C:\wamp64\www\unity, then build your WebGL project right into a folder there (or if it’s somewhere else just cut/paste it to there). Then if your project build folder is called “my_project”, your files locally would be at C:\wamp64\www\unity\my_project. And you would open it in a web browser at http://localhost/unity/my_project :slight_smile:

i’ve always been a Xampp guy :smile: that of course work great, (theres portable Xampp too),
but trying to make tool to run web server manually.

*and got it working, runs fine from commandline through mono:

"C:/Program Files/Unity2019_4/Editor/Data/MonoBleedingEdge/bin/mono.exe" "C:/Program Files/Unity2019_4/Editor/Data/PlaybackEngines/WebGLSupport/BuildTools/SimpleWebServer.exe" "X:/YourProj/Builds/" 60606
2 Likes

Tried it today, and it was working fine, no need to recompile (2021). I used Edge (the newer chromium one) and

SimpleWebServer.exe "Path to FOLDER containing index.html" port
4 Likes

Hi! This conv has been super helpful. I do have a question, is there any way to create a permanent server/link from a webGL build? Hope my question makes sense. If only 3rd party’s can do that I would prefer it if it can use something that can only be accessible through a private link rather than a public server, I just want to create a link/server similar to how nunu studio does it with their exports. If anyone knows anything about that I would appreciate the help on that! Thanks

Here is my simple solution for you guys.
I am using windows 10 and unity 2021.1.16f1

Voila your game is live in local server.

43 Likes