Execute an exe on Windows from the editor and read its return code

Not sure if this is actually possible, but I would like to run from Unity editor, an exe that I wrote, and read its return value.

Basically I wrote a small automation in python that read and convert some assets in a different format; and I want to run this exe from Unity, for testing purpose (so I do not have to do it by hand, and can just do it from the editor, it is kinda like running automation tests in Unity for my code, but without write code in C#).

I read that I can trigger exe from Unity code; although I was not able to find an example about how you do that; and also I am trying to figure out if after running my exe file, I can read its return code. My exe return a string that contain “pass” or “fail” if the conversion of the assets is successful or not, so I would like to read that returned value in Unity, so I know if the application should continue, or should stop, since the conversion failed.

Any suggestions or examples I can look at? Assuming this is actually feasible of course. I wonder if I have to re-write my asset conversion again in C#, although it feels reductive to have to write your tools in C#, because Unity can’t interface with external EXE.

You can do this using the ProcessStartInfo class. Here’s an example that runs a command and outputs the return values and errors (run this with ‘runShell=false’ to get output to your Unity code or ‘runShell=true’ to run it through the dos prompt).

static void RunProcess(string command, bool runShell, string args = null)
{
        string projectCurrentDir = Directory.GetCurrentDirectory();
        command = projectCurrentDir + "/" + command;

        UnityEngine.Debug.Log(string.Format("{0} Run command: {1}", DateTime.Now, command));

        ProcessStartInfo ps = new ProcessStartInfo(command);
        using (Process p = new Process())
        {
                ps.UseShellExecute = runShell;
                if (!runShell)
                {
                        ps.RedirectStandardOutput = true;
                        ps.RedirectStandardError = true;
                        ps.StandardOutputEncoding = System.Text.ASCIIEncoding.ASCII;
                }
                if (args != null && args != "")
                {
                        ps.Arguments = args;
                }
                p.StartInfo = ps;
                p.Start();
                p.WaitForExit();
                if (!runShell)
                {
                        string output = p.StandardOutput.ReadToEnd().Trim();
                        if (!string.IsNullOrEmpty(output))
                        {
                                UnityEngine.Debug.Log(string.Format("{0} Output: {1}", DateTime.Now, output));
                        }

                        string errors = p.StandardError.ReadToEnd().Trim();
                        if (!string.IsNullOrEmpty(errors))
                        {
                                UnityEngine.Debug.Log(string.Format("{0} Output: {1}", DateTime.Now, errors));
                        }
                }
        }
}
1 Like

If it’s a python script, you can do what @tonemcbride is suggesting, but use “python” for the command and “-m path-to-script” for the args. That is, assuming that python is on your PATH.

That’ll allow you to not have to compile the python code every time you make a change.

1 Like

Or, if it’s editor only, you could just run the python code .

1 Like