Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity c# run shell script

Using Unity3D and from editor script trying to run a script in the terminal on osx.

When running test.sh from terminal the GDCL application does its thing and then outputs the arguments. But if I run the script from Unity3D editor I only get the arguments in the output. GDCL doesn't run.

How can I get Unity3D to run terminal scripts?

C# script that runs test.sh (gives only output)

ProcessStartInfo psi = new ProcessStartInfo(); 
psi.FileName = Application.dataPath+"/test.sh";
psi.UseShellExecute = false; 
psi.RedirectStandardOutput = true;
psi.Arguments = "arg1 arg2 arg3";

//psi.Arguments = "test"; 
Process p = Process.Start(psi); 
string strOutput = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 
UnityEngine.Debug.Log(strOutput);

The test.sh script has chmod 777 (GDCL works only from terminal)

#!/bin/sh
GDCL ~/Documents/Unity/testproject/Assets/Font\ Normal.GlyphProject ~/Documents/Unity/testproject/Assets/Textures/fontNormal/font -fo PlainText-txt
for arg in $*
do
    echo $arg
done
like image 224
roady Avatar asked Nov 01 '22 16:11

roady


1 Answers

Try setting UseShellExecute to true or try running your shell directly and passing the script as the first argument.

psi.UseShellExecute = true; 

Or

ProcessStartInfo psi = new ProcessStartInfo(); 
psi.FileName = "/bin/sh";
psi.UseShellExecute = false; 
psi.RedirectStandardOutput = true;
psi.Arguments = Application.dataPath + "/test.sh" + " arg1 arg2 arg3";

Don't forget to import:

using System.Diagnostics;
like image 169
ayvazj Avatar answered Nov 15 '22 04:11

ayvazj