How to run your Python code in unity
Python is one of the top 10 popular programming languages of 2022. It is a general-purpose high-level programming language. You can use Python for developing desktop GUI applications, and web applications. Also, you can use Python for developing complex scientific and numeric applications. Python is designed with features to facilitate data analysis and visualization.
Currently, Unity 3D developers used to code in C# because Unity 3D supports C# by default. But python is known for simplicity and is rich in library support for data science. Today we are going to run our python codes in Unity 3D.
Getting Started
There are different ways to integrate python into Unity. Here we are going to explore python integration in two ways.
1. Unity Python Package
UnityPython is a plugin for Unity3D that provides support for running Python 2.x code in Unity3D on any platform which supports System.Reflection.Emit
Requirements
The build target must support System.Reflection.Emit
. Here is a chart of platform support for the same.
Setup
Download IronPython dll files and place them into Assets/Plugins
directory.
Then, go to Edit > Project Settings > Player > Other Settings > Configuration
and change Api Compatability Level
to .NET 4.x
Sample Program
Assume you have a small code snippet test.py in Python like this:
import random
class Test():
def __init__(self, name):
self.name = name
def display(self):
return "Hi, " + self.name
def random_number(self, start, end):
return random.randint(start, end)
You can use it from C# like this
var engine = Python.CreateEngine ();
ICollection<string> searchPaths = engine.GetSearchPaths ();
//Path to the folder of test.py
searchPaths.Add (Application.dataPath);
//Path to the Python standard library
searchPaths.Add (Application.dataPath + @"\StreamingAssets" + @"\Lib\");
engine.SetSearchPaths (searchPaths);
dynamic py = engine.ExecuteFile (Application.dataPath + @"\StreamingAssets" + @"\Python\test.py");
dynamic test = py.Test ("Codemaker");
greeting.text = "Greeting: " + test.display ();
randomNumber.text = "Random Number: " + test.random_number (1, 5);
Output
2. Python for Unity 2.0.1 preview package
Python for Unity is an experimental feature that helps you to integrate python code into your unity project seamlessly. It allows access from Python to the full C# API of UnityEngine, UnityEditor, as well as running Python code from your C# script.
Requirements
- Python 2.7.5 (64 bits) or later. The package does not work with Python 3.
- Unity 2019.3+. We recommend installing the latest version of Unity 2019 via the Unity Hub
Windows
- You must use Windows 10, patched to build 1803 or later.
- Install the software listed above in the default locations. When installing Python, make sure to check the option to add to the path is on.
Setup
Python for Unity requires some manual installation:
- First, follow the installation instructions in the manual
- Next, install Python for Unity itself: Edit your Unity manifest.json file (in the Packages folder of your project) so that it starts with
{
"dependencies": {
"com.unity.scripting.python": "2.0.1-preview.2",
...
}
}
Sample Program
Create a script named HelloWorld.cs
and attach it to the Main Camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelloWorld : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Unity: Hello World");
print ("Python: Hello World");
}
// Update is called once per frame
void Update()
{
}
}
Output
Also, you can try like this,
- Run your application.
- Go to
Window -> General -> Python Console
- Type the following code in the bottom window and click on the Execute button:
import UnityEngine
print ("Python: Hello World from python console")
UnityEngine.Debug.Log("Unity: Hello World from unity console")
Output
The main disadvantage of this package is that it supports only on Unity Editor. It won’t add any builds.
There you have it! Your own Unity app with python support :)
Thanks for reading this article.
If you enjoyed this article, please click on the clap button 👏 and share to help others find it!
If you are interested in further exploring, here are some resources I found helpful along the way: