这是对我以前的帖子的略微更新。我仍在尝试在Unity中使用C#脚本来调用python脚本。跟随这两个链接之后:
How do I run a Python script from C#?
Process.Start() not starting the .exe file (works when run manually)
我无法获得C#脚本来调用python程序。我知道这不是我的python脚本问题,因为我可以在终端中运行该程序而不会出现问题。
public class JsonStream : MonoBehaviour
{
private string GetStream()
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3";
//start.FileName = "\"/Library/Frameworks/Python.framework/Versions/3.6/bin/python3\"";
start.Arguments = Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch/stream.py";
//start.Arguments = "\"" + Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch/stream.py" + "\"";
start.WorkingDirectory = Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch";
//start.WorkingDirectory = "\"" + Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch" + "\"";
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.ErrorDialog = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
Process[] pname = Process.GetProcessesByName("python");
if (pname.Length == 0) {
UnityEngine.Debug.Log("python not running");
} else {
UnityEngine.Debug.Log("python is running");
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
return result;
}
}
}
public void DoEverythingStream()
{
UnityEngine.Debug.Log("Doing Stream");
string json_text = GetStream();
string[] games = json_text.Trim().Split('\n');
foreach (string game in games)
{
UnityEngine.Debug.Log(game);
}
}
}
运行该程序不会引起任何错误,但也不会输出任何内容。 start.FileName
和start.Arguments
和start.WorkingDirectory
的确切值为:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
/Users/brian/Documents/Unity Projects/Project 1/Assets/googlesheetspyprojectbatch/stream.py
/Users/brian/Documents/Unity Projects/Project 1/Assets/googlesheetspyprojectbatch/stream.py
我知道一个python进程正在启动,但是它没有运行我的python脚本,因为控制台记录“ python正在运行”。我目前的假设是start.Arguments
方法中的目录存在问题,但不确定它在哪里或由什么引起。附带说明的是,使用true
时未注释的变量返回File.Exists
,而注释的变量返回false
。此外,在运行脚本时,注释掉的变量记录为“ python not running”。有人对我的程序有什么想法吗?谢谢!
注意:如果有帮助,我将在10.13.1 Macbook Pro上运行它