[How-to?] Custom protocol / URL scheme to open game from browser

Hi all!

I’m getting on unfamiliar terrain here for me, but here I go anyway :slight_smile: .

My desired end goal: being able to open my game from a browser via a custom protocol, for example mygame://blablabla . Actually very similar to how Unity opens unityhub from the browser.

Been reading about windows registry etc. (which I don’t know much about), but all my users have to be able to do this of course, so I read somewhere that I need an install wizard probably that registers this protocol and then Windows will know to open my game.

That’s about all I could find and understand… but how to proceed now?

Can I do this from within Unity? Or do I need to export it the usual way and use a install wizard creator thing? How would that work? Any recommendations which one to use and how?

Any help is much appreciated!

1 Like

Unity doesn’t provide any kind of installer or publishing mechanism at this time. So you’ll have to figure out the best way to distribute your game, which depends on which OS or platform you’re developing for: Windows, iOS, Xbox, etc. They all have different packaging and installation requirements.

Have you considered building your game for WebGL? Since you goal is run the game from the browser this is one possible solution. Although WebGL has restrictions/complications compared to native desktop builds.

Hello timke, suppose we manage to register a custom protocol to launch our app, how can we query the data sent to it? we want to do something like in the following post but within a Unity app, how can we achieve the same?

Any help is much appreciated!

  • Sal

You can use Environment.GetCommandLineArgs to retrieve the arguments.

1 Like

Any chance you would like to share how you accomplished this?

I know this is a very old thread, but how do I get the updated args when the user clicks on the link and the app is already running? I mean, the system tries to run the exe, and as it’s forcing single instance it will only regain the focus, but when I query for the command line args they are still the same and the information in the second link is lost

Just wondering if you ever find a solution to this?

I did some Googling on this question, and it appears the way to get “updated” args in an app launched via Protocol is through the Windows.ApplicationModel.AppInstance.GetActivatedEventAgs API. This is a WinRT API requiring Windows 10 17134 or later.

One simple way to utilize this API is through a Unity UWP XAML build, in which you can modify the OnActivated event to handle custom protocol activation and retrieve updated activated args. Here’s a basic sample I found (not using Unity).
Note: Unity XAML apps are in C++ and you’ll need to modify App.xaml.h and App.xaml.cpp.

For a Win32 app, i.e. Unity WindowsStandalone, it’s a bit more complicated because you’ll have to manually invoke WinRT API through COM from within a Unity script. Here’s a Microsoft tutorial on how to call this API within a standalone Win32 app (in C++).

The important bit is this call:
AppActivationArguments args = AppInstance::GetCurrent().GetActivatedEventArgs(); to retrieve the activation args.

Keep in mind, this API is still only supported on reasonably current Windows 10 builds; it won’t work on Windows 7.

1 Like

I was facing this problem too, but I was able to solve it by taking a different approach, which I note here.
As far as I could find, I could not find a way to pass command line arguments to a running Windows process. It is probably not possible.
I gave up on the method of passing command line arguments, and used the method of setting up a local server, and it worked fine.

A brief description of the process flow is as follows.

  1. Set up a local server with System.Net.HttpListener. Entry point can be “http://localhost:8080?yourapp/” or other appropriate entry point.
  2. Browser sends a request to “http://localhost:8080?yourapp/?key=value”.
    The application side receives the response, parses the query parameters, and executes arbitrary processing.

This is similar to the implementation of OAuth1.0 where the request token is received in a callback from the authentication page. For mobile apps, use the URL scheme, and for UnityEditor and PC (Standalone), go through a local server.