Game Development - How could I pass 'command line arguments' to a Unity standalone build?

21,454

Solution 1

Answered here: http://answers.unity3d.com/questions/366195/parameters-at-startup.html

And from here: https://effectiveunity.com/articles/making-most-of-unitys-command-line/

// Helper function for getting the command line arguments
private static string GetArg(string name)
{
    var args = System.Environment.GetCommandLineArgs();
    for (int i = 0; i < args.Length; i++)
    {
        if (args[i] == name && args.Length > i + 1)
        {
            return args[i + 1];
        }
    }
    return null;
}

You can use System.Environment.GetCommandLineArgs() to access the commandline args in a Unity build.

Solution 2

Not the exact answer, but I needed a launcher for my recent project too. So I designed my own communication system between the launcher app and the game.
As we open the launcher (a C# Windows Forms Application, looks like a Bethesda game launcher) it begins to listen to a specific port (e.g. 23076 TCP). Then as we open the game, it connects to the launcher server and the launcher will send some data to the game, such as: Is the launcher running? Chosen Resolution, Chosen Quality, Chosen Language, Controls settings, etc.
So we can simply set our options in the launcher and send them to the game trough a TCP connection.


Edit:
If you're looking for more info about this approach, take a look at LitenetLIB library. It supports Unity and Windows Forms, and I use it to make a connection between my launcher and my game. I also check the validity of the license using my launcher before entering the game.

Share:
21,454
Firedan1176
Author by

Firedan1176

Updated on July 09, 2022

Comments

  • Firedan1176
    Firedan1176 almost 2 years

    Standalone/Client/Exe/Player/Build all refer to the executable the end user will use to run my game.

    I'm using Unity as my choice of environment. I'm creating a launcher to start the game. This sounds very redundant, as unity has a "launcher" window, for their standalone builds, but this purpose is to ensure the game is up-to-date by checking for the latest version on a server. I've built the client using a simple WPF application. At this point, I'm trying to think about my various options for making sure the executable for the game is started only from the launcher. However, Unity's standalone builds only accept a few range of arguments. Here's what I'm considering:

    • Keep the executable hidden as two separate non executable files that are temporarily combined using copy /b in command line, and outputting as an exe (then deleting after startup). The exe's are no more than 17mb, so it shouldn't take too long.
    • Find some way to modify the .exe to check for a specific command-line argument to be passed (Unity's runtime doesn't let the developer have access to the arguments after being built. 'Security issues'???)
    • Create a temp file that stores either a timestamp or some unique identifier that the standalone game will check to make sure it's launched from the launcher (Only problem is throwing an error for Windows to catch that will say 'You must start the game from the launcher.' or something similar)

    I really want to use this launcher because:

    • It will ensure players' game is updated, so they cannot cheat/exploit
    • It will allow the game to be installed using a small 5-10mb launcher instead of downloading the whole game
    • It's cool

    If you have any other suggestions, or maybe could guide me the right way, let me know. Thank you.

  • Programmer
    Programmer over 7 years
    I don't think you understand the question. It says "How could I pass 'command line arguments' to a [Unity standalone build]" not "How could I pass 'command line arguments' to a [Unity Editor]". The question is about standalone build not the editor. Maybe I read this wrong....
  • David
    David over 7 years
    @Programmer, there is another argument for such purpose, -buildLinux32Player <pathname>, -buildWindowsPlayer <pathname> etc. All you need do is to add such argument to it as you like. I think the link in the answer has all info the OP needs. Just have a read on it, you will see this answer pertinent. Thanks anyway.
  • Programmer
    Programmer over 7 years
    No David. What you linked and talked about in your answer is about the Editor. buildWindowsPlayer is used to build a project for Windows 32 bit. buildLinux32Player to build for Linux. All those command line arguments are used to perform automated tasks in Unity Editor only. This has nothing to do with what OP is asking.
  • David
    David over 7 years
    @Programmer, not sure. Let's wait the OP to clarify, ;)
  • Firedan1176
    Firedan1176 over 7 years
    Thanks @David for the comments, but yes, the information you linked me is about the editor. The standalone builds for a Unity game do have some command line arguments, but again, they're very limited. But the information you gathered for me was great. I'm thinking it'd be easiest to just do all my checks with the build and not worry about splitting it up as I mentioned. After all, the general target audience isn't concerned about how to hack into the game, but playing it. The launcher's main purpose is to keep users up-to-date. Thanks guys!
  • Alpha Gabriel V. Timbol
    Alpha Gabriel V. Timbol about 4 years
    how exactly did you achieve this code-wise? could you please provide an example? or atleast some reference material to look into