2
\$\begingroup\$

SUMMARY

I am looking for a straightforward way to get the port number that the Unity editor is using to serve WebGL test builds after the build's browser window has been closed.

I'm supporting Unity students who are not computer savvy. I'd like to know if there's an easy way to get this information from the editor itself, short of rebuilding the project.

DETAILS

To test my Unity WebGL project from the Unity Editor, I select the menu "File > Build and Run."

Menu

Once the project is built, a Chrome window opens with a location of the form "http://localhost:53587/", where 53587 is a random port number.

enter image description here

WHAT I HAVE TRIED

Google searches haven't given me an easy way to discover this port.

Using Window's Resource Monitor, I can find all the ports unity test servers are listening on, but this method requires many steps and external tools. I'd like know if there is a way within Unity to discover this information.

Complicated Method

  1. Press Ctrl+R to open the Run window.
  2. Type resmon.exe and press ENTER
  3. Select the Network tab
  4. Select the Listening Ports bar
  5. Sort on image by clicking on the image column header
  6. Search for image mono.exe
  7. Get port number(s) from Port column.

enter image description here

This is relatively complicated, Windows-specific method and it's inexact; it can return multiple instances of test builds being served. I'd like an easy way of discovering the port being served for the project currently being edited.

\$\endgroup\$
2
\$\begingroup\$

There doesn't seem to be a way to do this. I did a bit of digging on a fresh install of Unity 202.1.1f1. Note that on different versions the behaviour might be different, but I would assume that at least for reasonably recent versions the following holds:

A forum thread here mentioned that Unity starts <Version>\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\SimpleWebServer.exe to run the game locally. This executable accepts the port to start the server on is passed via a command line parameter, so we need to find the place where this is generated.

This happens to be in <Version>\Editor\Data\PlaybackEngines\WebGLSupport\UnityEditor.WebGL.Extensions.dll, under which a class called HttpServerEditorWrapper can be found via decompilation. This contains code for launching SimpleWebServer.exe, but unfortunately, it turns out that it uses a function named GetRandomUnusedPort(), which essentially uses the C# TcpListener to open a temporary connection with the default port 0, which the C# documentation says the following:

In this case, the service provider will assign an available port number between 1024 and 65535.

After opening the temporary connection, the opened port is queried, temporary connection is closed, and the available port is passed as a command line parameter to SimpleWebServer.exe.

As a result, it seems that you are at the mercy of the operating system. Perhaps you could look into hosting the built project via an alternative custom method where you can configure the port yourself? Search engines know more about this, but for an example Python comes with a ready to use HTTP server that one can start in a directory to serve its contents.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ ...or get the command-line arguments of the started process? Just throwing an idea here... \$\endgroup\$ – Vaillancourt Aug 6 at 18:58
  • \$\begingroup\$ Oh wow, I had no idea that was possible! That indeed seems like a fairly reasonable solution. \$\endgroup\$ – Tyyppi_77 Aug 6 at 19:01
  • \$\begingroup\$ My reasoning is that if the Process Monitor/Task Manager can do it, there are probably Windows calls that can achieve it too! \$\endgroup\$ – Vaillancourt Aug 6 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.