Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

How to manually start a localhost version of WebGL build

Discussion in 'WebGL' started by douglassophies, Aug 5, 2019.

  1. douglassophies

    douglassophies

    Joined:
    Jun 17, 2012
    Posts:
    141
    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?
     
    NoSpoonLab, Kronoss2015 and rccDave like this.
  2. Schubkraft

    Schubkraft

    Unity Technologies

    Joined:
    Dec 3, 2012
    Posts:
    1,038
    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.
     
    russisunni likes this.
  3. douglassophies

    douglassophies

    Joined:
    Jun 17, 2012
    Posts:
    141
    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?
     
    pragmascript likes this.
  4. Schubkraft

    Schubkraft

    Unity Technologies

    Joined:
    Dec 3, 2012
    Posts:
    1,038
    No, there is no button for that, sorry.
     
    douglassophies likes this.
  5. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    633
    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
     
    dev-4015, Rallix and douglassophies like this.
  6. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    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.
     
    Hypertectonic and douglassophies like this.
  7. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    55
    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.
     
  8. ivanngoc

    ivanngoc

    Joined:
    Nov 9, 2018
    Posts:
    9
    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
     
    smaggams, ravenpuff78, Ehredt and 3 others like this.
  9. vizgl

    vizgl

    Joined:
    Nov 4, 2014
    Posts:
    55
    Thanks. But I took this code https://gist.github.com/aksakalli/9191056 and run my own web server while testing webgl builds
     
  10. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    827
    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" .
     
  11. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    304
    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

    Code (CSharp):
    1. bool flag4 = !context.Request.IsLocal;
    2.                 if (flag4)
    3.                 {
    4.                     Console.WriteLine("Forbidden.");
    5.                     msg = "<HTML><BODY>403 Forbidden.</BODY></HTML>";
    6.                     response.StatusCode = 403;
    7.                 }
     
    Last edited: Jun 3, 2020
  12. Poindexter

    Poindexter

    Joined:
    Aug 16, 2011
    Posts:
    19
    Tested on Unity 2019.4.7f1. Create a batch file with content below, adapting for path differences:

    Code (batch):
    1. C:
    2. cd "C:\Program Files\Unity\Hub\Editor\2019.4.7f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten_Win\python\2.7.5.3_64bit"
    3. 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]"
     
    Last edited: Aug 31, 2020
  13. Koyae

    Koyae

    Joined:
    Feb 5, 2015
    Posts:
    3
    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):
    Code (batch):
    1. cd /d "<old_WebGL_output_dir>"
    2. "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
    Code (batch):
    1. cd /d "<old_WebGL_output_dir>"
    2. python -m SimpleHTTPServer
    If you have Python 3.x installed separately then it's even simpler:
    Code (batch):
    1. python -m http.server --directory "<old_WebGL_output_dir>"
     
  14. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,736
  15. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    220
  16. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    7,849
    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)
     
    Tymianek likes this.
  17. polemical

    polemical

    Joined:
    Jun 17, 2019
    Posts:
    1,244
    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 :)
     
  18. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    7,849
    i've always been a Xampp guy :D 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:
    Code (CSharp):
    1. "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
     
    cxode likes this.
  19. RealityDotStop

    RealityDotStop

    Joined:
    Mar 4, 2013
    Posts:
    42
    Tried it today, and it was working fine, no need to recompile (2021). I used Edge (the newer chromium one) and

    Code (CSharp):
    1. SimpleWebServer.exe "Path to FOLDER containing index.html" port
     
  20. e20lazaro

    e20lazaro

    Joined:
    Feb 6, 2018
    Posts:
    1
    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
     
    Last edited: May 3, 2021
  21. Lz_Yahya

    Lz_Yahya

    Joined:
    Jan 24, 2020
    Posts:
    7
    Here is my simple solution for you guys.
    I am using windows 10 and unity 2021.1.16f1
    1. Open CMD : HOW TO OPEN CMD
    2. Write this line : Cd C:\Program Files\Unity\Hub\Editor\2021.1.16f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools 1.jpg
    3. Then Write : SimpleWebServer.exe "HERE THE PATH FOLDER OF YOUR WEBGL BUILD" 8000 2.jpg 3.jpg
    4. Now open your browser and Write : localhost:8000 4.jpg
    Voila your game is live in local server.
     
    Next1on, syedmuaz, yssoh23 and 14 others like this.
  22. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    7,849
    also added quick webgl server start context menu to custom launcher
    (works per project, no need to select or type build folder)
     
    radiantboy and jRocket like this.
  23. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,502
    Man that is so useful thanks!
     
    Next1on and munkiki7 like this.
  24. zodok

    zodok

    Joined:
    Jan 12, 2019
    Posts:
    2
    hot it on wampserver www folder and set it as a new virtualHost
     
  25. gchetan15294

    gchetan15294

    Joined:
    Jul 7, 2021
    Posts:
    2
    Tried but its showing as Access is denied
     
  26. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    7,849
  27. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    105
  28. yssoh23

    yssoh23

    Joined:
    Aug 3, 2022
    Posts:
    1
    Here's an easy way. Just two steps. No command typing.

    1. Open the WebGL build output directory with VS Code.
    2. Right-click index.html, and select "Open with Live Server". (Install the "Live Server" extension first.)
     
    gasppol likes this.
  29. Lz_Yahya

    Lz_Yahya

    Joined:
    Jan 24, 2020
    Posts:
    7
    Hello again!
    I developed a windows application for the local hosting problem, The app you'll have just to give it your WebGL game path and it will host your game locally with one click.

    you can find the app here: https://uusoft.itch.io/unity-webgl-local-hoster
     

    Attached Files:

    radiantboy likes this.
  30. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,502
    AWESOME! thanks!!
     
  31. gasppol

    gasppol

    Joined:
    Feb 16, 2020
    Posts:
    31
    Thanks, best way for me
     
unityunity