从unity运行python脚本,以便稍后在我的游戏中使用其输出(文本文件)

Ran*_*Ran 7 c# python unity-game-engine

我试图从Unity(C#脚本)运行一个python脚本来使用它的输出,这是我游戏中的文本文件,事情就是当我在一致运行C#脚本时没有任何事情发生(Python脚本在其上工作正常)拥有).谁能告诉我我错过了什么?谢谢.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.IO;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using System.Diagnostics;

using System.Runtime.InteropServices;
public class PyCx : MonoBehaviour {
    public Text Message;
    public GameObject OpenPanel1 = null;
    // Use this for initialization
    void Start () {
        python ();
        read ();
        ShowMessage ();
    }
    public void python(){
        ProcessStartInfo pythonInfo = new ProcessStartInfo ();
        Process python;
        pythonInfo.FileName=@"C:\Users\HP\AppData\Local\Programs\Python\Python36-32\python.exe";
        pythonInfo.Arguments=@"C:\Users\HP\Documents\projet1.pyw";
        pythonInfo.CreateNoWindow = false;
        pythonInfo.UseShellExecute = false;
        python = Process.Start (pythonInfo);
        python.WaitForExit ();
        python.Close ();
    }
    public void read(){
        using (var reader = new StreamReader ("C://Users//HP//Documents//result.txt")) {
            string line = reader.ReadToEnd ();
            Message.text = (line);
        }
    }
    public void ShowMessage(){
        OpenPanel1.SetActive (true);
        Message.IsActive ();
    }
    // Update is called once per frame
    void Update () {

    }
}
Run Code Online (Sandbox Code Playgroud)

Ism*_*sma 6

您可以尝试使用IronPython直接在代码中运行Python,而不是使用在受控开发环境之外可能不可靠的进程(您不知道您的用户是否会安装Python以及使用哪个版本),IronPython是一个Python解释器对于CLR,它甚至不需要安装Python来执行脚本.

要使用它,您需要从http://ironpython.net/download/下载已编译的二进制文件

然后复制资源文件夹中的所有必需程序集:

  • IronPython.dll
  • IronPython.Modules.dll
  • Microsoft.Scripting.Core.dll
  • Microsoft.Scripting.dll
  • Microsoft.Scripting.Debugging.dll
  • Microsoft.Scripting.ExtensionAttribute.dll
  • Microsoft.Dynamic.dll

然后您将可以访问Python Engine,您可以按如下方式初始化它:

PythonEngine engine = new PythonEngine();  
engine.LoadAssembly(Assembly.GetAssembly(typeof(GameObject)));         
engine.ExecuteFile("Project1.py");
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到更多信息:http://ironpython.net/documentation/

参考

http://shrigsoc.blogspot.com.es/2016/07/ironpython-and-unity.html https://forum.unity.com/threads/ironpython-in-unity-a-good-idea.225544/