Skip to content Skip to sidebar Skip to footer

Run A Python Script From Unity, To Use Its Output (text File) In My Game Later

I'm trying to run a python script from unity (C# script) to use its output which is a text file in my game later, the thing is that when I run the C# script in unity nothing happen

Solution 1:

Instead of using a process which can be unreliable outside your controlled development environment (you don't know if your users will even have Python installed and which version) you could try to run Python directly in your code using IronPython, IronPython is a Python interpreter for the CLR so it doesn't even require Python to be installed to execute your scripts.

To use it you need to download the compiled binaries from http://ironpython.net/download/

Then copy all the required assemblies in your resources folder:

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

Then you will have access to the Python Engine, you can initialize it as follows:

PythonEngine engine = new PythonEngine();  
engine.LoadAssembly(Assembly.GetAssembly(typeof(GameObject)));         
engine.ExecuteFile("Project1.py");

You can see more info here: http://ironpython.net/documentation/

References

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

Solution 2:

  • Download Unity Python package using the link Unity Python 0.4.1
  • Then, go to Edit > Project Settings > Player > Other Settings > Configuration and change Scripting Runtime Version to "Experimental (.NET 4.6 Equivalent)".

Assume you have a small code snippet test.py in Python like this:

classTest():def__init__(self, name):
        self.name = name
    defdisplay(self):
        return"Hi, " + self.name

You can use it from C# like this

using System.Collections;
using System.Collections.Generic;
using IronPython.Hosting;
using UnityEngine;

publicclassPythonInterfacer : MonoBehaviour {
 voidStart () {
        var engine = Python.CreateEngine();

        ICollection<string> searchPaths = engine.GetSearchPaths();

        //Path to the folder of greeter.py
        searchPaths.Add(@"C:\Users\Codemaker\Documents\PythonDemo\Assets\Scripts\Python\");
        //Path to the Python standard library
        searchPaths.Add(@"C:\Users\Codemaker\Documents\PythonDemo\Assets\Plugins\Lib\");
        engine.SetSearchPaths(searchPaths);

        dynamic py = engine.ExecuteFile(@"C:\Users\Codemaker\Documents\PythonDemo\Assets\Scripts\Python\test.py");
        dynamic obj = py.Test("Codemaker");
        Debug.Log(obj.display());
    }
}

Solution 3:

Here is mine that works. Hope this helps.

var psi = new ProcessStartInfo();
// point to python virtual env
psi.FileName= @"C:\Users\someone\Documents\git-repos\PythonVenvs\venv\Scripts\python.exe";

// Provide argumentsvar script = @"Assets\Scripts\somecoolpythonstuff\cool.py";
var vidFileIn ="111";
var inputPath = @"Assets\input";
var outputPath = @"Assets\output";

psi.Arguments= string.Format("\"{0}\" -v \"{1}\" -i \"{2}\" -o \"{3}\"", 
                               script, vidFileIn, inputPath, outputPath);

// Process configuration
psi.UseShellExecute=false;
psi.CreateNoWindow=false;
psi.RedirectStandardOutput=true;
psi.RedirectStandardError=true;

// Execute process and get outputvar errors ="nothing";
var results ="nothing";

using (var process =Process.Start(psi))
{
    errors = process.StandardError.ReadToEnd();
    results = process.StandardOutput.ReadToEnd();
}

// grab errors and display them in UIStringBuilder buffy = new StringBuilder();
buffy.Append("ERRORS:\n");
buffy.Append(errors);
buffy.Append("\n\n");
buffy.Append("Results:\n");
buffy.Append(results);

// ui text object
responseText.Text= buffy.ToString();

Solution 4:

You can try the following github repository code. You will get a basic idea of using python code in unity from a file

Unity Python Demo

Post a Comment for "Run A Python Script From Unity, To Use Its Output (text File) In My Game Later"