Hi
How can I start a comand line utility with Diagnostics.Process with 3 arguments and how can I get the standard output of the process?
Thanks
By
Hi
How can I start a comand line utility with Diagnostics.Process with 3 arguments and how can I get the standard output of the process?
Thanks
By
Like this:
using UnityEngine;
using System;
using System.IO;
using System.Diagnostics;
Process process = null;
StreamWriter messageStream;
public class AppLauncher : MonoBehaviour
{
void StartProcess()
{
try
{
process = new Process();
process.EnableRaisingEvents = false;
process.StartInfo.FileName = Application.dataPath + "/path/to/The.app/Contents/MacOS/The";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;
process.OutputDataReceived += new DataReceivedEventHandler( DataReceived );
process.ErrorDataReceived += new DataReceivedEventHandler( ErrorReceived );
process.Start();
process.BeginOutputReadLine();
messageStream = process.StandardInput;
UnityEngine.Debug.Log( "Successfully launched app" );
}
catch( Exception e )
{
UnityEngine.Debug.LogError( "Unable to launch app: " + e.Message );
}
}
void DataReceived( object sender, DataReceivedEventArgs eventArgs )
{
// Handle it
}
void ErrorReceived( object sender, DataReceivedEventArgs eventArgs )
{
UnityEngine.Debug.LogError( eventArgs.Data );
}
void OnApplicationQuit()
{
if( process != null !process.HasExited )
{
process.Kill();
}
}
}
Hi
how can I send the 3 arguments to the process.
When I try this code I get the error
error CS0246: The type or namespace name `DataReceivedEventArgs’ could not be found. Are you missing a using directive or an assembly reference?
Funky. It should be in Diagnostics: DataReceivedEventArgs Class (System.Diagnostics) | Microsoft Learn
Regarding sending three arguments to the process I imagine you mean command-line arguments? Just append that to process.StartInfo.FileName.
Hi
I use it with Unity iPhone could this be the reason why he didn’t find it.
He doesn’t find DataReceivedEventHandler and BeginOutputReadLine() too.
What is wrong with this?
By
Hi
How can I handle the arguments.
I need to transfer 3 arguments to the program. But what ever I do it never gets the three arguments.
(In my program I check if argc = 4 (the first is the startpath) )
How can I do this?
By
unity iPhone uses an older version of mono. Its possible that this version doesn’t include those parts of the runtime.
Regarding the arguments - it should just work like this:
process.StartInfo.FileName = Application.dataPath + "/path/to/The.app/Contents/MacOS/The index1 index2 index3";
AngryAnt,
Thank you so much! This really helped me make serial communication with threading.
This works well, but as soon as you attach the monodevelop debugger to Unity, and try to hit a break point. Monodevelop and Unity both freeze because the debugger is hung up while waiting for input from your process. Once you kill your process, Monodevelop will hit your break point. Does anybody have a work around for this?
if this is not an issue with the settings of the process object, maybe you could send regular empty data from your process (it could be only in you debug build for it…)
This is great Emil thanks!
I was seeing unity hang indefinitely trying to start a command line tool from unity editor on OSX, and this code fixes the hang. I think it might be the EnableRaisingEvents = false line as I wasn’t do that before.
It’s working in editor when i use context menu
but process not run when application is playing any idea about why
thank’s !
For those stumbling upon this from Google; note that the posted solution does not work with IL2CPP and Unity is not planning to address this so you’ll have to make your own. Closest solution would be this thread .