:zap: A very fast, simple, and general inter-process communication example between Unity3D C# and Python, using ZeroMQ
⚡️ A very fast, simple, and general inter-process communication example between Unity3D C# and Python, using ZeroMQ.
PS. It looks slow in the GIF above because I put a delay of one second between each message so that you can see it
working.
.dllfrom python or even rewrite everything in
C#because you don't know how to communicate between python and C# processes?
IronPythonor
Python.NETinside Unity but it doesn't allow you to install your amazing external python libraries? (And its minimal power is pretty ridiculous compared to your external python)
TensorFlow Protobuf Graph(Deep learning model) and use
TensorFlowSharpor
OpenCVForUnityto import the graph inside Unity because you want to use the model to predict stuff in Unity, but it doesn't allow you to use/utilize your new NVIDIA GeForce RTX 2080Ti, and it's also hard to code?
MLAgents, anyone?
If you answer Yes to any of these questions but it seems you have found no solutions, then this repository is definitely for you! (If you answered Yes to all questions, you and me are brothers! 😏)
I've tried a lot. With a lot of searching on the internet, I've found no solutions that is simple, fast, and general enough that I can apply to any kind of communication between Python and Unity3D. All I've done in the past were simply a hack to either get my scientific computation work in Unity instead of python, or communicate between the processes painfully.
Until I found ZeroMQ approach from this repository (and some head scratching).
I've built a
request-replypattern of ZeroMQ where Python (server) replies whenever Unity (client) requests a service from Python.
The idea is to create a separate thread inside Unity that will send a request to python, receive a reply and log the reply to the console.
git clone https://github.com/off99555/Unity3D-Python-Communication.gitcommand.
dllfiles are targeting .NET 4.x version) and run
Assets/NetMQExample/Scenes/SampleScene.
PythonFiles/server.pyusing command
python server.pyon a command prompt.
Specifically, Unity will send request with a message
Hello10 times, and Python will simply reply
World10 times. There is a one second sleep between each reply on the server (to simulate long processing time of the request).
Please read the comments inside
PythonFiles/server.pyand
UnityProject/Assets/NetMQExample/Scripts/and you will understand everything more deeply.
The most important thing is that you should follow the 4 getting started steps first. Don't skip it! ❣️
After you've understood most of the stuff but it's not advanced enough, you should consult the official ØMQ - The Guide.
pip install pyzmqcommand or see more installation options here or here.
NuGetpackage manager inside
Visual Studiowhen you want to build a .NET application, or you could install using
.NET CLI. But for this repository here, you don't need to do any of the installation because we've already included
AsyncIO.dlland
NetMQ.dllfor you inside
UnityProject/Assets/NetMQExample/Plugins/directory. If you want to build your own
dllfiles, please take a look at this issue.
Based on this issue, the NetMQ implementation is not working nicely with Unity. If you create more than one ZeroMQ client in Unity, the Unity editor will freeze.
No, this is the expected behavior of ZeroMQ because of the simplicity of the code. It's mentioned in the guide
here. If you want to make the code better, which is not
the focus of this example, you can learn more about ZeroMQ as suggested in the screenshot below.
The problem is that when you restart the server, the server won't reconnect to the old client anymore. You have to restart the client also.
This repository is designed to be a minimal learning resource for getting started. It's not a fully working high-level package. After you understand the example, my job is done.
Most of the code are just copies from the official ZeroMQ tutorial. I try to make this as simple to grasp as possible, so I only log the message to the console and nothing fancy. This is to minimize the unnecessary learning curve.
BestHTTPpackage in Unity for SocketIO client and use
python-socketioas SocketIO server. And it does not have the issue of making Unity editor freezes.