Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 365
Firedan1176 Avatar asked Oct 04 '16 01:10

Firedan1176


2 Answers

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.

like image 73
Geordie Avatar answered Nov 14 '22 23:11

Geordie


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.

like image 35
Bamdad Avatar answered Nov 14 '22 22:11

Bamdad