Skip to content

Instantly share code, notes, and snippets.

@MerlinTwi
Created December 20, 2017 17:30
Show Gist options
  • Select an option

  • Save MerlinTwi/cadcfcb34bda4ca1ad97b1560c771850 to your computer and use it in GitHub Desktop.

Select an option

Save MerlinTwi/cadcfcb34bda4ca1ad97b1560c771850 to your computer and use it in GitHub Desktop.
Execute third-party programs from Unity
using System.IO;
public static class ProcessExecute {
public static int Execute(string command, string arguments, out string output, out string error, string input = null) {
var proc = new System.Diagnostics.Process();
proc.StartInfo.UseShellExecute = false;
if (!string.IsNullOrEmpty(input))
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = command;
proc.StartInfo.Arguments = arguments;
proc.Start();
if (!string.IsNullOrEmpty(input)) {
// Unity's old Mono runtime writes a BOM to the input stream,
// tripping up the command. Ceate a new writer with an encoding
// that has BOM disabled.
var writer = new StreamWriter(proc.StandardInput.BaseStream, new System.Text.UTF8Encoding(false));
writer.Write(input);
writer.Close();
}
proc.WaitForExit();
output = proc.StandardOutput.ReadToEnd();
error = proc.StandardError.ReadToEnd();
return proc.ExitCode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment