3

I'm trying to run a face detection model in Unity. It gets input from the webcam, then spits out a face. But trying to make this work with C# has been an absolute nightmare. And despite all my suffering, I still haven't been able to make it work!

If I could use python, I'd be able to get it done easily. So, obviously, I want to find a way to get a python script working in Unity. But IronPython is the only thing I've been able to find, and it's outdated.

I need either knowledge of how to make IronPython work in spite of being outdated, or some other method. Please.

2
  • "If I could use Python, I'd be able to get it done easily" - as a programmer who has used over 20 languages in their career and uses several on a daily basis today, C# and Python included, I have to tell you: you're wrong. It is not really about the language, but about the libraries and underlying framework that you're trying to use. Watch a few OpenCV in Unity tutorials, start small. There are ways to integrate Python scripts in Unity applications, but at its core, you'll need and want to learn C# and/or C++. Rust and IronPython are just distractions at your level, and IronPython a dead end. Commented Feb 18, 2022 at 20:21
  • Like Grismar I’ve learned many languages over my years and once your brain truly understands breaking down what you want to logic it’s just a matter of picking how to write it whether that’s forth, c#, python or something else. C# is not hes to learn but will be invaluable if you want to use unity. And c# way more forgiving than unreal and c++ Commented Feb 19, 2022 at 10:03

5 Answers 5

2

I try to use python once on Unity and I found a few ways:

  1. There is a package call "IronPython" where you can add a python file to your unity project and then call a function from C# to your python code, to do that you should follow this:

We already know that we can use python to use .net internal calls. Now we may use the same to start a console that can accept a scripting language in Unity engine. To do this we have to include certain dll files. These dll files must be present in Assets>plugins

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

Once the Plugins are in place.

Initiate the Cs code

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

Where test.py is the python code.

Initiate python side:

import UnityEngine from UnityEngine 
import * 
Debug.Log("Hello world from IronPython!")

References: https://github.com/cesardeazevedo/Unity3D-Python-Editor http://techartsurvival.blogspot.in/2013/12/embedding-ironpython-in-unity-tech-art.html IronPython in Unity3D

the issue with this way is that most of the python module are not supported.

2.the second way is to create a file like json that contain the data you want to send to the json and then create an output json that send the output from the python script, this way is very limited with what you can send because the data must be contain in your json.

  1. the last way that work for me is to install the Nuget package and copy the script from python to c# line by line with the relevent module installed in Unity and it's work for me, but copy a long code can take time. this is a reference to the package: https://github.com/GlitchEnzo/NuGetForUnity and then to install the relevent package you should press on NuGet → Manage NuGet Packages and the choose the relevent package(for me it was Numpy and it work grate).

hope it will help you

Sign up to request clarification or add additional context in comments.

2 Comments

Help - my pip doesn't find UnityEngine. What can I do?
there is 3 options in my comment, what option did you try?
2

We are thrilled to announce that Python for Unity 4.0.0-exp.5 is now available!

4.0.0-exp.5 is a major upgrade from our last public release, and incorporates a large number of changes. In summary: Based on Python 3.7; scripts based on Python 2.7 will need to be ported. Users no longer need to install Python on their system. In-process Python is no longer reinitialized when the Unity domain reloads. Removed the out-of-process API. The PySide example now runs in-process and is much simpler. Limited support for a virtual environment workflow via the ProjectSettings/requirements.txt file. Many bug fixes. Documentation for the Python for Unity package is available here, and the full changelog can be found here.

This is an experimental release, and thus is not visible in Package Manager. To install this package, open Package Manager, click the + at the top left and select Add package by name.... Enter com.unity.scripting.python as the name and and 4.0.0-exp.5 as the version and click Add. Alternatively, you may edit Packages/manifest.json and add "com.unity.scripting.python": "4.0.0-exp.5", to the list of dependencies, or edit the existing entry for Python for Unity to update the version.

Soursce: https://forum.unity.com/threads/python-for-unity-release-announcements.1084688/

Documentation: https://docs.unity3d.com/Packages/[email protected]/manual/index.html

3 Comments

Note that a) support for this will end soon .. and b) this was ever only thought for inside the Editor itself, not for a build application
could you share link to information about support ending?
Tbh I don't exactly remember anymore what the reason for my comment was but it was stated somewhere in the API .. see e.g. discussions.unity.com/t/… .. however, today I didn't find this statement anymore so maybe they changed their mind in the meantime idk
1

Unfortunately, Unity at this time does not support Python. Although, there is an asset that you can use a bit of Python with. I am not sure what you can do with this asset but I know it could help a minimal amount:https://assetstore.unity.com/packages/tools/integration/python-interpreter-645

Quick Note: Most programming languages work about the same way. If you figure out the documentation and grammar/punctuation for C#/UnityC#, you should be off just fine.

5 Comments

The problem I'm having is with the input/output images of my model. Python uses numpy arrays. They're clean, simple, and straightforward. C# uses ten different flavors of texture2D, color structs, and twenty types of arrays. I just can't seem to wrap my tiny little brain around all these wacky concepts.
So, as detailed as you can get, what exactly are you trying to accomplish as of now that you are looking to do in Python? I may be able to help with putting it into C# or something.
I get the webcam's feed as a webcamtexture (variation of texture2D). I convert it into a texture2D. I feed this into the face detection model. It spits out the faces, which I then feed into another model (the emotion recognition model). This emotion recognition model gives the final output - the detected emotion.
For reference, that Unity store asset is no longer available there.
@PhilippLenssen yikes, just saw that. Keep in mind that you can use small bits of python, but Unity will not prioritize compiling it especially for your game. But you can use variables and basic functions from python.
1

I don't know how recent it is but there is a Unity package for python available on unity 2019.3 and further versions.

Warning the first versions of this package can't use Python3.

You can see more for yourself by the following link. https://docs.unity3d.com/Packages/[email protected]/manual/index.html

I hope this may help you.

Comments

0

Unity not supported python, But you Can write Python Code and run it by Socket programing, Create Server with python and send data,in C# Connect to server and use data sended with python.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.