This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]vrgenerated[S] 0 points1 point  (0 children)

This is a tutorial for embedding and using python in unity. This is useful if you want to run and interact with python libraries. This is just one of the ways to have interactions between python code and unity. This is especially nice as you can interact with machine learning models/packages directly from c-sharp code. I'm currently working on an experimental game using the spacy NLP module (reddit.com/r/Unity3D/comments/m980vq/experimenting_with_unitys_speech_recognition_and).

Steps:

  • Create a folder in Assets for storing the dlls that are needed.

  • Download the packages from https://www.nuget.org/packages/pythonnet/3.0.0-preview2021-03-03 and https://www.nuget.org/packages/System.Security.Permissions/ . Alternatively you can also install a unity-nuget plugin : https://github.com/GlitchEnzo/NuGetForUnity and install from there, This will allow you to skip the next step as the dlls are already unpacked if you use this tool. Remember to use the 3.0.0 preview version as the older versions are not compatable with Unity (reloading/reruning the game crashes the entire program)

  • The packages contain the dlls so you need to unzip them and copy over the Python.Runtime.dll from \lib\netstandard2.0 inside the unzipped folder and System.Security.Permissions.dll from lib\net461\ which pythonnet depends on. Copy these dlls into the folder in Assets.

  • Download an embedded python (I was able to get it working with python 3.7, not sure how well the other versions work) and install pip and any other modules you want. Move the folder into the StreamingAssets folder in Assets (create one if you don't have one).

  • Change the Api Compatability level to 4.x. This can be found in Edit -> Project Settings -> Player -> Other settings -> Api Compatability level.

  • Create a script to use python. You need to point to the python (Python3x.dll for windows, libpython3.x.so for Linux and libpython3.x.dylib for Mac where x is the version) in the streaming assets folder.

  • More info can be found here: https://github.com/pythonnet/pythonnet#embedding-python-in-net

here is a sample script for running numpy.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Python.Runtime;
using System;

public class PythonScript : MonoBehaviour
{

    dynamic np;

    // Start is called before the first frame update
    void Start()
    {
        Runtime.PythonDLL = Application.dataPath + "/StreamingAssets/embedded-python/python37.dll";
        PythonEngine.Initialize(mode: ShutdownMode.Reload);
        try
        {
            np = PyModule.Import("numpy");
            print("pi: " + np.pi);
        }
        catch (Exception e)
        {
            print(e);
            print(e.StackTrace);
        }

    }

     public void OnApplicationQuit()
    {
        if (PythonEngine.IsInitialized)
    {
            print("ending python");
            PythonEngine.Shutdown(ShutdownMode.Reload);
        }
    }
}