How can the Unity standalone player read command line parameters ?


I need to start my standalone application with given parameters. Something like :

applicationName -s 1 -e 100 -b 2

Is there any way of doing this ?

You should be able to use:

string[] System.Environment.GetCommandLineArgs()

Unity's API currently doesn't expose this information for use in scripting (although there are some pre-set command line options which Unity builds respond to).

The only method I've seen suggested (on this forum post) to achieve what you want is to use a C++ plugin, which uses:

LPSTR pCommandLine = ::GetCommandLine(); 

which would get the entire command line as a string, and then you'd have to parse the string yourself. I haven't tried this though, so I don't even know whether it actually works.

I also had a quick look on the Unity UserVoice feature request site, to see whether this has already been requested, but I couldn't see any evidence of it. There are a few requests which mention the term "command line", but none seem to be what you want - which I find surprising, since it seems a very useful feature.

Perhaps you could start a new feature request, and post a link to your new request here and to any of the old forum threads which mention this issue too?

If you're on OSX, then you can use this as a workaround:

string[] GetCommandLineArgs()
    Process proc = Process.GetCurrentProcess();
    ProcessStartInfo si = new ProcessStartInfo("ps", "-p" + proc.Id + " -xwwocommand=");
    si.RedirectStandardOutput = true;
    si.RedirectStandardError = true;
    si.UseShellExecute = false;
    si.CreateNoWindow = true;

    Process psProc = new Process();
    psProc.StartInfo = si;
    string result = psProc.StandardOutput.ReadToEnd();
    return result.Split();

Amir 's answer is not work for me, the result return an empty string and I still cannot access the file path