Use Python With Unity 3D - The Definitive Guide - Mika Likes
Có thể bạn quan tâm
Python is a great programming language due to a multitude of reasons such as a long list of libraries ready to solve almost any problem you might encounter. For me, however, the biggest reason to prefer coding in Python with Unity over C# is that I simply don't have to type as much; no type declarations, semicolons or curly brackets, not to mention return types, scopes and so on… 😴
Yet, online I can only find outdated guides or worse yet "why don't you just learn C#, it's easy and blah blah" comments. 😫 But I like Python. 🥺Therefore, this post teaches you how to use IronPython with Unity. Just remember that IronPython only supports Python 2.
Use Python with Unity
First you will have to download IronPython dll files and place them into Assets/Plugins directory. The easiest way to do this is by downloading my Unity package.
Import my Unity package to your game project

IronPython requires .NET 4.5, and Unity doesn't like this by default. That's why you will have to change the .NET version by going to Edit -> Project Settings -> Player. Make sure that the Scripting Runtime Version is .NET 4.x (this requires you to restart Unity) and that API Compatibility level is .NET 4.x

How to code in Python
Assume you have a small code snippet greeter.py in Python like this:
class Greeter(): def __init__(self, name): self.name = name def greet(self): return "Hi, " + self.nameYou can use it from C# like this
using System.Collections; using System.Collections.Generic; using IronPython.Hosting; using UnityEngine; public class PythonInterfacer : MonoBehaviour { void Start () { var engine = Python.CreateEngine(); ICollection<string> searchPaths = engine.GetSearchPaths(); //Path to the folder of greeter.py searchPaths.Add(@"C:\Users\Mika\Documents\MyPythonScripts\"); //Path to the Python standard library searchPaths.Add(@"C:\Users\Mika\Documents\MyUnityGame\Assets\Plugins\Lib\"); engine.SetSearchPaths(searchPaths); dynamic py = engine.ExecuteFile(@"C:\Users\Mika\Documents\MyPythonScripts\greeter.py"); dynamic greeter = py.Greeter("Mika"); Debug.Log(greeter.greet()); } }For a similar example, see the PythonExample prefab in my Unity package.
As you can see, the Python class can be directly used as an object in C#, and better yet, if you provide the path to the Assets\Plugins\Lib folder included in my Unity package, you can use anything in Python standard library! In addition, if you also provide the path to your Python script, you can import anything in that path as usual.
Next steps
If you want to use any external Python libraries (the ones you install with pip), you can just download and extract them to theAssets\Plugins\Lib folder. This will make them available for IronPython.
Note: Using absolute paths is fine when you are doing development, but if you ever decide to publish your game, you will need to resolve the paths dynamically and distribute the standard library in Assets\Plugins\Lib and your Python scripts with the game executable.
Finally you can have the best of two worlds: Unity's dead easy world editor and Pythonic simplicity. ☺️
Related posts:
A Fix for a VPN Internet Killswitch on WindowsVirtualBox: access from the host and other virtual machinesProcess dialectal Finnish in PythonTừ khóa » How To Use Python In Unity
-
Python For Unity | 2.0.1-preview.2
-
Python For Unity | 4.0.0-pre.1 - Unity - Manual
-
Python For Unity 3D 2020!! - YouTube
-
Python In Unity - Unity Forum
-
How Do To Use A Python Script In Unity? - Stack Overflow
-
Learning Python In Unity! Create Awesome Games And Apps! - Udemy
-
Start To Finish Unity® Games And Python Coding - Udemy
-
Does Unity Support Python? - Quora
-
How To Run Your Python Code In Unity | By Vishnu Sivan - Medium
-
Using Python Libraries/modules In Unity Code. More Details In ... - Reddit
-
Exodrifter/unity-python: Python Plugin For Unity3D. - GitHub
-
Running Your Python Code In Unity - Morioh
-
Using The Package With Unity Game Engine
-
Unity Python Code Example