C# system.process stdin/stdout handling with Unity

2019-09-06 10:33发布

问题:

I asked a very similar question one week ago and I still try to find an answer.

I setup a complete new Unity3D project, put an object in it, create a C# script and put the following code in the Start() method.

void Start() {

    Process myProcess = new Process();

    myProcess.StartInfo.FileName = "gnome-terminal";
    myProcess.StartInfo.UseShellExecute = false;
    myProcess.StartInfo.RedirectStandardInput = true;
    myProcess.StartInfo.RedirectStandardOutput = true;
    //myProcess.StartInfo.CreateNoWindow = true; for testing
    myProcess.Start();
    //Thread.Sleep(6000); somebody suggest wait some time, but it make no difference
    myProcess.StandardInput.WriteLine("sensible-browser");

}

I build the project for my Linux Ubuntu system and execute it. A new terminal appears and that's it. Normally, now the browser should be open but it doesn't.

That's only my test program. In my origin one I have to read/write from/into the stdin/stdout.

I'm fully desperate and hopefully anyone of you can help me

Thanks

回答1:

Use bash instead of gnome-terminal.

Process myProcess = new Process();
myProcess.StartInfo.FileName = "bash";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.Start();
myProcess.StandardInput.WriteLine("sensible-browser");
myProcess.StandardInput.Close();