On a standalone build with forceSingleInstance enabled, how do I get the most recent CLI args?

Hello everyone, I hope you all are having a great day so far.

I currently have a standalone game that I package with InnoSetup, in which part of the installation process is the registration of my custom protocol that redirects the URL to the .exe as a parameter. It looks something like this:

  1. The user runs the installer
  2. The protocol myprotocol is installed in the registry
  3. When the user opens a link that uses that protocol, myprotocol://?foo=bar as an example, it runs as MyGame.exe myprotocol://?foo=bar
  4. My Unity code (both on Start() and Update()) then handles this just fine by using Environment.GetCommandLineArgs()

Unfortunately, when you repeat step #3 (say myprotocol://?foo=new-value), the call to Environment.GetCommandLineArgs() doesn’t fetch the updated URL even though it’s running the single instance of the game.
Here’s the code that I have:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class DeepLinkTest : MonoBehaviour
    {
        public Text cliArgs;
        // Start is called before the first frame update
        void Start()
        {
            var args =  System.Environment.GetCommandLineArgs();
            cliArgs = GameObject.Find("CLIArgs").GetComponent<Text>();
            cliArgs.text = args[1];
        }
        // Update is called once per frame
        void Update()
        {
            var args =  System.Environment.GetCommandLineArgs();
            cliArgs = GameObject.Find("CLIArgs").GetComponent<Text>();
            cliArgs.text = args[1];
        }
    }

If this is not the way to do this, I’ll be happy to hear your suggestions. Thanks!

Update would not be expected to retrieve command line arguments. They are retrieved only when you start your game. And is what you are seeing.

That’s what I thought as well. Is there an event that I can hook in to fetch the subsequent calls to the already running executable?

Not that I’m aware of. One idea might be to read from a text file every few seconds (not in update as it might slow down your game) that you write to. You could also send in a UDP, TCIP or ROS message. But why are your users trying to launch the game again if it’s already running? Wouldn’t you want to allow multiple instances if so?

My use case is that I have a catalog of items listed on a website and clicking these items would display them inside the game, essentially the same way how the Steam Community Marketplace does it with their item previews.

You would not need to launch the game again to do this. Just have a button inside your game that hits your website with a web request. Or you could use Unity IAP and define your products on Google and Apple for use inside your game. IAP allows you to add new products while your game is running with FetchAdditionalProducts. Unfortunately IAP doesn’t support Steam quite yet, hopefully later this year https://docs.unity3d.com/Packages/com.unity.purchasing@4.1/manual/UnityIAPGoogleConfiguration.html

Sorry for the misunderstanding, let me put it in another way. Let’s say I want to re-create this website: CSGOStash. If you click one of the items there, there’s an “Inspect” button that has a custom protocol for launching CSGO via steam. For example, this knife: Bowie Knife | Gamma Doppler - CS2 Stash

… can be viewed inside the game by accessing this steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M3299440059602080792A23394349277D9964828333873228414

… now if you go and try to inspect another item, say this one:
steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M3294936459970541768A23393390307D7387601115487150174

… CSGO just displays it happily without restarting/launching another instance of the game. Essentially the same concept as how Zoom does for meeting links, Bittorrent for magnet links, or even the Unity Asset Store when you click “Open in Unity”.

1 Like

Got it! This may help https://docs.unity3d.com/Manual/enabling-deep-linking.html

Thank you for this one, but unfortunately the requirements dictate that it has to be a standalone build. We’ve explored UWP before but it’s a pain to deploy an exe to our customers.

Maybe Branch supports standalone https://branch.io/what-is-deep-linking/