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 ?

more ▼

asked Mar 30, 2010 at 09:29 AM

BlobbyPops gravatar image

91 7 6 16

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You should be able to use:

string[] System.Environment.GetCommandLineArgs()


more ▼

answered Mar 30, 2010 at 05:39 PM

Joachim Ante gravatar image

Joachim Ante
1.1k 10 13 65

That call returns null on both iOS and Android devices. On iOS the command line arguments can be obtained in Objective-C code (main.mm) and pulled back across to C# code. On Android apps are started in an unusual way, which doesn't use the traditional argc/argv parameters. Instead they are started with "Intents" which can have additional typed key/value pairs. It should be possible to obtain those in Java code and pull them back to C# code, but I haven't actually attempted that.

Nov 24 at 01:03 PM mark_madatom
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Mar 30, 2010 at 09:59 AM

duck gravatar image

duck ♦♦
48.2k 137 266 514

This can already be done from C# using string[] System.Environment.GetCommandLineArgs()

Mar 30, 2010 at 05:40 PM Joachim Ante

Oh, nice. - Please mark Joachim's answer as "accepted", not mine!

Mar 30, 2010 at 05:55 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

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();


more ▼

answered May 11, 2010 at 05:00 AM

Amir Ebrahimi gravatar image

Amir Ebrahimi
71 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 30, 2010 at 09:29 AM

Seen: 6149 times

Last Updated: Nov 24 at 01:03 PM