Search Unity

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

Discussion in 'Scripting' started by rapidrunner, Oct 8, 2020.

  1. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    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.
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    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).

    Code (CSharp):
    1. static void RunProcess(string command, bool runShell, string args = null)
    2. {
    3.         string projectCurrentDir = Directory.GetCurrentDirectory();
    4.         command = projectCurrentDir + "/" + command;
    5.  
    6.         UnityEngine.Debug.Log(string.Format("{0} Run command: {1}", DateTime.Now, command));
    7.  
    8.         ProcessStartInfo ps = new ProcessStartInfo(command);
    9.         using (Process p = new Process())
    10.         {
    11.                 ps.UseShellExecute = runShell;
    12.                 if (!runShell)
    13.                 {
    14.                         ps.RedirectStandardOutput = true;
    15.                         ps.RedirectStandardError = true;
    16.                         ps.StandardOutputEncoding = System.Text.ASCIIEncoding.ASCII;
    17.                 }
    18.                 if (args != null && args != "")
    19.                 {
    20.                         ps.Arguments = args;
    21.                 }
    22.                 p.StartInfo = ps;
    23.                 p.Start();
    24.                 p.WaitForExit();
    25.                 if (!runShell)
    26.                 {
    27.                         string output = p.StandardOutput.ReadToEnd().Trim();
    28.                         if (!string.IsNullOrEmpty(output))
    29.                         {
    30.                                 UnityEngine.Debug.Log(string.Format("{0} Output: {1}", DateTime.Now, output));
    31.                         }
    32.  
    33.                         string errors = p.StandardError.ReadToEnd().Trim();
    34.                         if (!string.IsNullOrEmpty(errors))
    35.                         {
    36.                                 UnityEngine.Debug.Log(string.Format("{0} Output: {1}", DateTime.Now, errors));
    37.                         }
    38.                 }
    39.         }
    40. }
    41.  
     
    rapidrunner likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    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.
     
    rapidrunner likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    tonemcbride likes this.