How to manually start a localhost version of WebGL build

As we had the needs for our webGL project, we created and released a package that improves Unity’s default behavior by allowing control of the web server directly from the editor.

Let us know what you guys think!
https://assetstore.unity.com/packages/tools/utilities/editor-web-server-263933

1 Like

its odd, i was reading back and i did simply simplewebserver and tada http://localhost port 80 was fired up and there was my game… I really am surprised unity say they cant just do this as part of the editor really am… the 403s for favicon etc are normal so never worry about those

Is the web server available within Wi-Fi network from Computer for Mobiles, as it shown in my Video?

Ps. For those who want a simple life - there is huge room for improvement. However this uses unitys simplewebserver and your build folder and fires it up on port 8080 and then fires a browser.

using UnityEditor;
using UnityEngine;
using System.IO;
public class ApplicationPathExample
{
    static System.Diagnostics.Process p = new System.Diagnostics.Process();
    [MenuItem("Build/Launch Last Built")]
    static void AppPath()
    {
        try
        {
            if (p.HasExited)
            {
               StartServer();
            }
        }
        catch(System.Exception e)
        {
            StartServer();
        }
        System.Diagnostics.Process b = new System.Diagnostics.Process();
        b.StartInfo.FileName = "http://localhost:8080";
        b.Start();
    }
  
    [MenuItem("Build/EndServer")]
    static void EndServer()
    {
        try
        {
            p.Kill(); 
        }
        catch (System.Exception e)
        {
            Debug.Log(e.Message);
        }
    }
    static void StartServer()
    {
        Debug.Log("Starting server");
        string apppath =  Path.GetDirectoryName(EditorApplication.applicationPath);
        string lastBuildPath = EditorUserBuildSettings.GetBuildLocation(EditorUserBuildSettings.activeBuildTarget);
      
        p.StartInfo.FileName = Path.Combine(apppath, @"Data\PlaybackEngines\WebGLSupport\BuildTools\SimpleWebServer.exe");
        p.StartInfo.Arguments = lastBuildPath + " 8080";
        p.StartInfo.UseShellExecute = false;
        p.Start();
    }
}
4 Likes

Anyone figure out how to configure CORS with SimpleWebServer.exe?

no but you can probably write one one that does the job

If I wanted to do that I would have already. I just want Build and Run to work with CORs config

i understand, but when unity dont offer, sometimes we end up creative… and making your own simplewebserver.exe which does do cors would be one way

1 Like

For real lol. I ended up just going a completely different way anyway.

1 Like

its why i wrote the script to reshow the last built version, number of times you’re working on something and i dont need the 15 minute wait while it rebuilds it to remind me a couple of other things i was going to change…

1 Like

ok but where do I put this code?

Well its for your IDE not your game, so a folder called Editor, call it whatever you like…

https://docs.unity3d.com/Manual/SpecialFolders.html

Thanks to bugfinders, now I create a plugin for it (UPM Compatible)

GitHub - StinkySteak/unity-webgl-server: Unity Plugin to Host WebGL. Run build locally without "Build and Run" with zero dependencies Enjoy!

1 Like

Cheers for the credit :smile:

what about https?

Unitys local thing never did https. You are welcome to download something else and configure it to do a https environment and run it in place of the unity one.

The approach of invoking SimpleWebServer from the Unity editor folder has been working for us, but when we updated to Unity 6 it stopped working. The same code running in Unity 6 can invoke the SimpleWebServer from Unity 2022.3.7 and it works. So something changed with SimpleWebServer itself, but since there is no documentation for it, we have no idea how to fix it. The error we get is "Only Uri prefixes starting with ‘http://’ or ‘https://’ are supported’. This happens even if the arguments for the process are set to something like “https://www.google.com”.

I figured it out 10 seconds after posting of course. :slight_smile:

The new version requires you to specify the source folder and the URL to serve it from as arguments.

          s_webglProcess.StartInfo.Arguments = $"\"{buildSelection}\" http://localhost:8000/";

Odd @chris1P mine always needed that if you look at my code you’ll see i always had that.