Python is one of the top 10 popular programming languages of 2021. Python is a general purpose and high level programming language. You can use Python for developing desktop GUI applications, websites 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. You can take advantage of the data analysis features of Python to create custom big data solutions without putting extra time and effort.
Currently Unity 3D developer used to code in C# because Unity 3D supports C# by default. But python is known for simplicity and rich in library support for data science. Today we are going to explore python in Unity 3D.
There are different ways to integrate python in 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
The build target must support
System.Reflection.Emit. here is a chart of platform support for
Download IronPython dll files and place them into Assets/Plugins directory. unity-python
Then, go to
Edit > Project Settings > Player > Other Settings > Configuration and change
Api Compatability Level to ".NET 4.x"
Assume you have a small code snippet test.py in Python like this:
import randomclass Test():
def __init__(self, name):
self.name = namedef display(self):
return "Hi, " + self.namedef 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 greeter.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);
2. Python for Unity 2.0.1 preview package
Python for Unity facilitates Unity’s interaction with various media and entertainment industry applications and ensures that you can integrate Unity into a broader production pipeline seamlessly. It allows access from Python to the full C# API of UnityEditor, UnityEngine, as well as your own C# APIs added to your Unity projects, and running Python code from within C#.
Python for Unity
Python for Unity facilitates Unity’s interaction with various media and entertainment industry applications and ensures…
- 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; 2019.3 is the minimum.
- 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.
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
Create a script named HelloWorld.cs and attach it to the Main Camera.
using UnityEngine;public class HelloWorld : MonoBehaviour
// Start is called before the first frame update
Debug.Log("Unity: Hello World");
print ("Python: Hello World");
}// Update is called once per frame
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:
print ("Python: Hello World from python console")
UnityEngine.Debug.Log("Unity: Hello World from unity console")
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 :)
I hope you had fun reading and/or following along. If you are interested in further exploring, here are some resources I found helpful along the way:
First you will have to download IronPython dll files and place them into Assets/Plugins directory. unity-python…