I’m trying to build a script that can start a python application, then read and write to the console for that application (I want to do this because there is a certain library in python that I need to use for Unity). I have a script in my main scene, attached to an empty GameObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
public class PythonSetup : MonoBehaviour {
public string pythonExe; // Set this to your python.exe file
// Use this for initialization
void Start () {
SetupPython ();
}
// Update is called once per frame
void Update () {
}
void SetupPython() {
string fileName = @"C:\sample_script.py";
Process p = new Process();
p.StartInfo = new ProcessStartInfo(pythonExe, fileName)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
Console.InputEncoding = Encoding.UTF8;
p.Start();
UnityEngine.Debug.Log (p.StandardOutput.ReadToEnd ());
p.StandardInput.WriteLine ("\n hi \n");
UnityEngine.Debug.Log(p.StandardOutput.ReadToEnd());
p.WaitForExit();
}
}
In the C drive I have sample_script.py:
print("Input below:")
i = input()
print(i)
I am running Windows 10 with the latest Unity.
When I run my game, it is stuck (I think on the line p.WaitForExit()). Thanks for your help ahead of time!