4

When you right click a file, you can select the option to "open with..." any program/executable, which launches that program and automatically opens the file within that program. I guess everyone knows that.

Now I'm building a game in unity (scripts are in c#) and I want my finished build to support this functionality, so that my user/player can right click, say, a savegame file, select open with, and then select my game executable. The game should then start and load the saved game.

How do I implement this functionality?What happens under the hood when this option is used from the file system, and how do I handle it inside my game?

I'm not asking about how to actually load the game (that's different for every game anyway), I just need to know how I "catch" a case in which a user opens a file with my game.exe.

scenia
  • 1,609
  • 14
  • 33
  • 1
    What the OS does is just runs your executable with the first argument being the path/name of the file you clicked on. In the startup routine of your game you'll need to see if there were any arguments passed in (`Environment.GetCommandLineArgs()`) and get the file name and process it. – Ron Beyer Jun 24 '15 at 12:57
  • So basically I should have an empty scene that does prep work including checking for command line args? Does it just pass the entire path as plain text? – scenia Jun 24 '15 at 13:00
  • I'm not sure from the standpoint of Unity, but thats the basics. Yes the entire path is passed as plain text. You can test this out easily by creating a new console application (in MonoDevelop, not Unity) and doing a Console.WriteLine of the function above. – Ron Beyer Jun 24 '15 at 13:01
  • I'll do that and play around with it a little, thanks :) go ahead and post this as an answer so I can accept it! – scenia Jun 24 '15 at 13:05
  • I'll post that this afternoon if nobody gives you an answer specific to how to do it in Unity. Thanks. – Ron Beyer Jun 24 '15 at 13:06

1 Answers1

5

If you want to add your program to "Open With" context menu (What you need to do is in your installer, add a registry key, pointing to your application, as explained here If you don't have an installer (I think that unity 5 can build you one), you can simply do it in game, by calling

Microsoft.Win32.Registry.SetValue(key, valueName, value, Microsoft.Win32.RegistryValueKind.String);

with a script inside your scene. Or you can write an installer program in C# that does this.)

If you just want to handle a file opened with your application, you need to get the command line arguments, like the filepath, inside the unity player. Write a script in the scene that you load 1st, which reads the array of strings returned by System.Environment.GetCommandLineArgs(). The 1st argument is the executable's name, so you need to do the following:

string filepath;
string arguments = System.Environment.GetCommandLineArgs();
if (arguments.Length >= 2)
{
    filepath = arguments[1];
    DoYourThing(filepath);
}
return;

If you then right click your file, open it with your app, the OS will pass the filepath to the app, as the 2nd command line argument, and using the code above, you will read it.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
RaidenF
  • 3,411
  • 4
  • 26
  • 42