Search Unity

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

Discussion in 'Windows' started by unity3dat, Apr 30, 2019.

  1. unity3dat

    unity3dat

    Joined:
    Aug 9, 2017
    Posts:
    88
    Hi all!

    I'm getting on unfamiliar terrain here for me, but here I go anyway :) .

    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!
     
    salgue likes this.
  2. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    408
    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.
     
  3. salgue

    salgue

    Joined:
    Jul 26, 2017
    Posts:
    2
    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?
    https://stackoverflow.com/a/38205984/877225

    Any help is much appreciated!

    - Sal
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    salgue likes this.
  5. unity3dat

    unity3dat

    Joined:
    Aug 9, 2017
    Posts:
    88
    Any chance you would like to share how you accomplished this?
     
  6. luiziv

    luiziv

    Joined:
    Aug 31, 2016
    Posts:
    23
    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
     
  7. johnalcherdoloiras

    johnalcherdoloiras

    Joined:
    Sep 4, 2021
    Posts:
    1
    Just wondering if you ever find a solution to this?
     
  8. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    408
    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.
     
    Maeslezo likes this.
  9. SenaJP

    SenaJP

    Joined:
    Jan 31, 2016
    Posts:
    4
    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.